How to get autofill working in VBA? - vba

I'm trying to get the autofill option to work. For one situation I only need to copy the values in a cell. In the other the formulas (but I guess this works the same as copying a value?).
This selects the cell I want to copy all the way down to the last filled cell in column B
Range("A1048576").End (xlUp)
Just adding the filldown option does not work. Any combination of .Select or .Value doesn't work either.
When I'm trying to simplify it by using this code, the filldown option doesn't work either.
Range("A11").Select
Selection.Filldown
Anybody got an idea?

It's always good if you can use the macro recorder for such tasks and then edit the result to remove the Select and ActiveCell:
Option Explicit
Sub Makro1()
Range("A1").Select
ActiveCell.FormulaR1C1 = "123"
Range("A1").Select
Selection.AutoFill Destination:=Range("A1:A11"), Type:=xlFillDefault
End Sub

Related

Excel Macro Copy Paste REF! Error

I am currently new to excel macros and trying to figure out why this is not working. Basically, I want Cell "D22" from "Sheet1" to be copied and then pasted to "sheet2" Cell "A2". The problem is on sheet1 I have a formula in cell D22 so when I copy and paste it into the new sheet, I get a #REF! ERROR :(
I have looked it up and tried fixing it but still no luck. Below is my basic code. I know there is probably a simple solution but if someone could please advise me in the right direction much would be appreciated! xD
Sub Insert()
Sheets("Sheet1").Select
Range("D22").Select
Selection.Copy
Sheets("Sheet2").Select
Range("A2").Select
ActiveSheet.Paste
End Sub
If you just want to assign the value of one cell to another, don't use copy/paste, just use the Value property:
Sub Insert()
Sheets("Sheet2").Range("A2").Value = Sheets("Sheet1").Range("D22").Value
End Sub

Runtime error 1004 when using Cut and PasteSpecial

I have to cut the contents from active cell for a specific range in sheet 1 and paste it into the active cell of that range in sheet 2. Cutting the specific range of contents is working fine, but in the paste options I am getting runtime error 1004 as application defined or object defined error.
Here is the code I am using:
Sub sheet1_sheet2_copy_click()
Sheets("sheet1").Activate
ActiveCell.Resize(1, 26).Cut
Sheets("sheet2").Activate
ActiveCell.Resize(1, 26).PasteSpecial
End Sub
Your question is a little unclear, but you can try doing this:
Sub sheet1_sheet2_copy_click()
Sheets("sheet1").Cells(1, 26).Cut
Sheets("sheet2").Cells(1, 26).PasteSpecial Paste:=xlPasteValues
End Sub
Better yet, you can just do this:
Sub sheet1_sheet2_copy_click()
Sheets("sheet2").Cells(1, 26).value = Sheets("sheet1").Cells(1, 26).Value
End Sub
In general, it is better to not use .Activate or .Select, but to instead explicitly define your references like I showed. Modify the Paste:= to whatever parameter you are trying to use for PasteSpecial.
See https://msdn.microsoft.com/VBA/Excel-VBA/articles/range-pastespecial-method-excel for more information on this command.
Personally, I think using the active cell as a source and destination is rather unreliable, but if you change the paste line to activesheet.paste it should work fine.
I have tested your code and it looks like PasteSpecial only works if you use Copy instead of Cut, so you should modify your code this way:
Sheets("sheet1").Activate
ActiveCell.Resize(1, 26).Copy
Sheets("sheet2").Activate
ActiveCell.Resize(1, 26).PasteSpecial
If you need to delete the data from sheet1, then do it after the PasteSpecial.

VBA Code to copy and paste active column with merged cells inside

I'm trying to copy the active column and paste it next to it but the code selects the entire worksheet because it has merged cells in.
Sub CopyPaste()
Columns(ActiveCell.Column).Selection
Selection.Copy
ActiveCell.Offset(0,1).PasteSpecial Paste:=xlPasteAll
End Sub
Could you please help me adding the missing code to ignore merged cells?
This is yet another reason to avoid using Select in VBA for Excel. Your selection will expand with the merged cells. You can try this:
ActiveCell.EntireColumn.Copy ActiveCell.Offset(0, 1).EntireColumn
And again, you should find some way to avoid counting on the ActiveCell in your code, and use some fully qualified range.

Macro Copy&Paste

I'm trying to create a macro that will copy data from one worksheet and place into another. This I can do with no problem. But, when I want to use the same macro in another row is where I have my problem. Basically what I want to do is copy cell D11 from sheet1 and place that in cell B4 on sheet2, etc (What I'm doing is obviously more complicated than that, but that doesn't matter here).
My problem is when I want to now run this macro and copy cell D12 from sheet1 and paste into B5 on sheet2 the value pasted jumps to B4. I understand that this happens because of where the VBcode is saying to paste the copied value.
My question is how to I just have it paste in whatever row I choose? Maybe based on what row/cell I have selected.
Current code, written by recording the macro
Sheets("sheet1").Select
Range("D11").Select
Selection.Copy
Sheets("sheet2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B4").Select
I'm assuming the last line is where I need to make the change, but I'm not sure what to change.
Thank you! Any and all help is greatly appreciated.
As a general rule, try to avoid Selection Copy-Paste (detailed discussion is provided in: "Application.Calculation = xlCalculationManual" statement causing run-time error 1004 in VBA Copy-Paste procedure). Instead, use direct copy statement, which will solve you issue and significantly improve performance:
Listing 1.
Sub DirectCopySample()
Application.ScreenUpdating = False
Sheets("Sheet1").Range("D11").Copy Destination:=Sheets("Sheet2").Range("B5")
Application.ScreenUpdating = True
End Sub
Sub in Listing 1 performs direct copy from Cell: Sheets("Sheet1").Range("D11") into cell: Sheets("Sheet2").Range("B5").
Also, your initial Copy-Paste Sub could be simplified (it will also make it work, though Listing 1 is preferred)
Listing 2.
Sub CopyPasteSample()
Sheets("sheet1").Range("D11").Copy
Sheets("sheet2").Range("B5").PasteSpecial Paste:=xlPasteValues
End Sub
Hope this will help. Best regards,
You seem to have recorded a Macro and are trying to replay it. Here is a real VBA code (not a Macro recording type):
Sheets("sheet2").Range("B5") = Sheets("sheet1").Range("D11").Value
This is all!
BTW, your predicament comes from the fact that the PasteSpecial method copies into the currently selected cell. You've tried running this Macro several times and the Range("B4").Select line did the trick. If you insist on your approach the insert Range("B5").Select BEFORE the PasteSpecial.

Copy Cell To Another Sheet, one after another

I have been trying to find the solution to this for a while now and I do not believe I am searching for the right thing.
Basically what I want to do is copy a cell contents and paste it to another sheet and then delete the contents of the cell that was copied from.
I have one button that corresponds to each cell and I want to write a bit of code which enables you to do this at the click of a button.
Another issue is I don't know the code that would not constantly paste in the same cell over and over, but rather go to the next row and paste there.
What I have at the moment is the following:
Sub Macro1()
Range("I2").Select
Selection.Copy
Sheets("Completed Tasks").Select
Range("A72").Select
ActiveSheet.Paste
Sheets("Projects Live!").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("H2").Select
End Sub
This only refers to the top cell and i have recorded a macro to show essentially what i want.
Hope you can help or point me in the right direction.
Consider:
Sub luxation()
Range("I2").Copy Sheets("Completed Tasks").Range("A72")
Range("I2").Clear
End Sub
This is for a single cell...............you would embed this kind of syntax in a loop.