vba to import data from another spreadsheet

steve2115

Board Regular
Joined
Mar 17, 2014
Messages
82
Looking for help with the following. Need to modify the below vba code so that when it imports the data it only imports all the rows with data on them. Currently when it imports the zzmr9820o.xlsx file it also is bringing in empty rows below the data. Need it to find the lastrow on zzmr9820o.xlsx and then import into my excel table.


VBA Code:
Sub Import_Click()

Dim OpenFileName As String
 Dim wb As Workbook
   
   'Select and Open workbook
 OpenFileName = Application.GetOpenFilename("zzmr9820o,*.xlsx")
 If OpenFileName = "False" Then Exit Sub
 Set wb = Workbooks.Open(OpenFileName)
 
 'Get data EXAMPLE
 ThisWorkbook.Sheets("Send Emails").Range("G:V").Value = wb.Sheets(1).Range("A:P").Value
  
 MsgBox ("Import Complete")
wb.Close
    
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I haven't tested this, but see if the following works for you:

VBA Code:
Sub Import_Click()
'
    Dim LastRowInwbSheet    As Long
    Dim OpenFileName        As String
    Dim wb                  As Workbook
'
    OpenFileName = Application.GetOpenFilename("zzmr9820o,*.xlsx")                              ' Select and Open workbook
'
    If OpenFileName = "False" Then Exit Sub
'
    Set wb = Workbooks.Open(OpenFileName)
'
    LastRowInwbSheet = wb.Sheets(1).Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row                      ' Returns a Row Number
    
    ThisWorkbook.Sheets("Send Emails").Range("G:V").Value = wb.Sheets(1).Range("A:P" & LastRowInwbSheet).Value     ' Get data EXAMPLE
'
    MsgBox ("Import Complete")
'
    wb.Close
End Sub
 
Upvote 0
Just tested - code is erroring at

VBA Code:
ThisWorkbook.Sheets("Send Emails").Range("G:V").Value = wb.Sheets(1).Range("A:P" & LastRowInwbSheet).Value
 
Upvote 0
Apologies!

How about:

VBA Code:
Sub Import_ClickV2()
'
    Dim LastRowInwbSheet    As Long
    Dim OpenFileName        As String
    Dim wb                  As Workbook
'
    OpenFileName = Application.GetOpenFilename("zzmr9820o,*.xlsx")                                                              ' Select workbook
'
    If OpenFileName = "False" Then Exit Sub                                                                                     ' Exit if user cancels
'
    Set wb = Workbooks.Open(OpenFileName)                                                                                       ' Open workbook selected
'
    LastRowInwbSheet = wb.Sheets(1).Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row                                   ' Returns a Row Number
    
    ThisWorkbook.Sheets("Send Emails").Range("G1:V" & LastRowInwbSheet) = wb.Sheets(1).Range("A1:P" & LastRowInwbSheet).Value   ' Get data EXAMPLE
'
    MsgBox ("Import Complete")                                                                                                  ' Let user know import is complete
'
    wb.Close                                                                                                                    ' Close user selected workbook
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,518
Messages
6,119,996
Members
448,935
Latest member
ijat

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