Copying information from a separate workbook in Excel VBA - vba

I want to copy the information from cells A2 to A4 into my current workbook and am using the following code to do so, however, when I run the macro I get the message: Run-time error '9', subscript out of range. How do I make it in the range?
Application.Workbooks("Client and Project Droplist").Worksheets("Sheet1").Range("A2:A4").Select
Selection.Copy
Me.Range("A1").Select
ActiveSheet.Paste
UPDATE
I've made it so that it will select the range but now I am getting run-time error 13: Type mismatch. Here is the new code
Private Sub Macro_Click()
Application.Workbooks("Client and Project Droplist").Activate
Application.Workbooks("Client and Project Droplist").Worksheets("Sheet1").Select
Application.Workbooks("Client and Project Droplist").Worksheets("Sheet1").Range("A2:A4").Select
Selection.Copy
Workbooks("VBA Exercises").Worksheets(Sheet1).Select
'Workbooks("VBA Exercises").Worksheets(Sheet1).Range("A1").Select
'ActiveSheet.Paste
End Sub
I commented the last two lines because I haven't quite gotten there yet.

This worked for me. It looks like you have to specify the actual filename, and when you tried to paste it to your active workbook, it wasn't truly active.
Application.Workbooks("client and Project Droplist.xlsx").Worksheets("Sheet1").Range("A2:A4).Select
Selection.Copy
Application.Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("A1").Select
Selection.Paste

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

Excel VBA - runtime error 1004 when simplifying recorded code

I've got a recorded macro that I tried to simplify by getting a number of activate and select statements onto a single line, but that results in a runtime error.
This is not a critical problem, but I'm just curious to understand what is going on. This is my initial code snippet (it is preceded by a Copy snippet in the procedure):
ThisWorkbook.Activate
Sheets("MS Act Report").Select
Range("G1").Select
ActiveSheet.Paste
this is my simplified code:
ThisWorkbook.Activate
Sheets("MS Act Report").Range("G1").Select
ActiveSheet.Paste
When running this I get a
runtime error '1004': Select method of Range class failed
You can only select ranges on the active sheet. If "MS Act Report" is not the active sheet, you can't issue a .select command against its cells. To simplify the code, instead of copy-pasting, just make the ranges equal.
Thisworkbook.WorkSheets("MS Act Report").Range("G1:I5").Value= _
ActiveWorkbook.Worksheets("Whatever").Range("a1:c5").Value
Some recommended reading: How to avoid using select

How do I implement the VBA module to copy filtered data

I need to implement a VBA Macro that copies data from one excel worksheet and puts certain column into another one.
Sub sbCopyRangeToAnotherSheet()
'Method 1
Sheets("Sheet1").Range("A1:B10").Copy Destination:=Sheets("Sheet2").Range("E1")
'Method 2
'Copy the data
Sheets("Sheet1").Range("A1:B10").Copy
'Activate the destination worksheet
Sheets("Sheet2").Activate
'Select the target range
Range("E1").Select
'Paste in the target destination
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
This wont work for new sheets named for example Sheet 3 neither will it run effectively for filtered data. I need a code that can copy the filtered data and paste certain column into another worksheet.
This simplest way is copyas commend from oryginal file "with code" and delete this, what you do not want to share.
Another one relies on building string with code lines using VBComponents
There you can check the code: http://vbatools.pl/tworzenie-linii-kodu-makrem-z-dodatku/
After that you file should to save as xlsm ora xls (old school) to save makros.

Run time Error - 438

I have the following code in which I am trying to copy data from one sheet to another in same workbook. When I run the code I get Runtime error -438
Sub Copy()
Sheets("Sheet1").Range("A1:D20").Copy
Sheets("Sheet2").Activate
Range("E1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
Try the following code. You should not rely on Activate and Select.
Sub ZCopy()
Sheets("Sheet1").Range("A1:D20").Copy
Sheets("Sheet1").Paste Destination:=Worksheets("Sheet2").Range("E1")
Application.CutCopyMode = False
End Sub
Interesting Reads
MSDN
How to avoid using Select in Excel VBA macros
Do you have a particular need for copy and paste? This can be slow and inefficient. If you're just copying data from one sheet to another, you can set the values of one range equal to the values of another range and avoid the whole thing.
Sheets("Sheet2").Range("E1:H20").Value = Sheets("Sheet1").Range("A1:D20").Value
This will set the range from cells E1:H20 on Sheet2 to the same values as those in the range A1:D20 on Sheet1, which is effectively a copy and paste. I should add that this will work only for the values themselves.
If there is specific formatting (or formulas) that you need copied and pasted, this method won't work.

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.