VBA Macro Issue

kenderweasel

New Member
Joined
Feb 17, 2017
Messages
40
Hi,

I'm trying to crate a macro that will import multiple columns of data from a sheet on one workbook into the first empty row on a specific sheet on another. I've written the following code, however, this is currently only copying over the first cell (A2, as the donor sheet has columns). The data that needs to be imported is from columns A-E, and needs to be pasted into A-E on the recieving sheet. Note - once I have this working, I will need to repeat the code for another 8 sheets, which also have data that needs to be imported.

Sub Rectangle1_Click()

Dim OpenFileName As String
Dim wb As Workbook
'Select and Open Workbook
OpenFileName = Application.GetOpenFilename("VoiceID_Reports_,*.xl??")
If OpenFileName = "False" Then Exit Sub
Set wb = Workbooks.Open(OpenFileName)
'Get data
ThisWorkbook.Sheets("Train").Cells(Cells.Rows.Count, 1).End(xlUp).Offset(1, 0).Value = wb.Sheets("TrainSuccess").Range("$A2:$E1000").Value


MsgBox ("Done")
wb.Close
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try this.
Code:
wb.Sheets("TrainSuccess").Range("$A2:$E1000").Copy
ThisWorkbook.Sheets("Train").Cells(Cells.Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
 
Upvote 0
Here is an alternative option if you don't want to go with copy/paste approach. The reason your code is only copying the first cell is because you are telling excel to put range A2:E1000 into one cell so you have to "Resize" the range to cater the required range as shown below

Rich (BB code):
ThisWorkbook.Sheets("Train").Cells(Cells.Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(1000 - 1, 5).Value = wb.Sheets("TrainSuccess").Range("$A2:$E1000").Value
 
Upvote 0
Thank you so much Norie - that solution works perfectly :) And thanks mse330 for letting me know where my error was - learning VBA is challenging, so I did expect that I was making a very stupid mistake... :D
 
Upvote 0

Forum statistics

Threads
1,223,053
Messages
6,169,831
Members
452,284
Latest member
TKM623

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top