Excel Macro Copy Paste REF! Error - vba

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

Related

How to get autofill working in 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

Excel Macro for checkbox if true

i need a macro or a line to add to my code for when the checkbox is true or "Checked" i need it to copy the content of a specific cell into another in a sheet in the same workbook what i was able to get to is this code
Sub macro2()
Range("A1:C4").Select
Selection.Copy
Sheets("sheet2").Select
Range("A1:C4").Select
ActiveCell.PasteSpecial
End Sub
but as you can see it only gets me to get this range to be copied and pasted once i have checked the box what i need is to copy it when checked and maybe not to let it get m to the cell in sheet2 to allow multiple selections to be pasted in some specified cells i know i have the code sheet2.select but i honestly don't know any other coding and i need to put that kind of coding for over hundred cells so do appreciate your assistance with this if anybody can please am dying here
If CheckBox1.Value = True Then
ThisWorkbook.Sheets("Sheet2").Range("A1:C4").Value = ThisWorkbook.Sheets("Sheet1").Range("A1:C4").Value
End if

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.