Import Data Starting from next empty row

Tanne

New Member
Joined
May 12, 2024
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi
I'm new to VBA and I'm trying to append 2 excel data files into the same worksheet.
I'm using the following coding for the 1st excel data file and it works great. I select the file path I want, data imports and the curser ends up on the next empty row.

**************************************
Sub Get_Data_From_File()
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files(*.xlsx*),*xlsx*")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
OpenBook.Sheets(1).Range("A6:S700").Copy
ThisWorkbook.Worksheets("Authorised Work").Range("A2").PasteSpecial xlPasteValues
OpenBook.Close False

End If
Application.ScreenUpdating = True

' Select Cell
Range("A1").End(xlDown).Offset(1).Select

End Sub
**************************************



I'm trying the use the same coding to import and append the 2nd excel data file, however I don't want to hard code the Range("A700") as this row changes daily depending on the number of data records that load each night.

***************************************
Sub Import_Data2()

' Import_Data2 Macro

Dim FileToOpen As Variant
Dim OpenBook As Workbook

Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files(*.xlsx*),*xlsx*")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
OpenBook.Sheets(1).Range("A4:S300").Copy
ThisWorkbook.Worksheets("Authorised Work").Range("A700").PasteSpecial xlPasteValues
OpenBook.Close False

End If
Application.ScreenUpdating = True


End Sub
**********************************

I would like to import the 2nd excel data file from the next empty row which has been selected from the 1st lot of coding. This will always start in Column A but the row will always change. Instead of noting Range("A700) can this 1) be replace with some sort of ActiveCell Code or 2) be replaced with this code Range("A1").End(xlDown).Offset(1).Select within this line ThisWorkbook.Worksheets("Authorised Work").Range("A700").PasteSpecial xlPasteValues

Help Please :) I have no idea
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Find the last used row in column A and increment by 1.
Why is this in the Access subforum?
 
Upvote 0
Try this:

VBA Code:
Sub Import_Data2()

' Import_Data2 Macro

    Dim FileToOpen As Variant
    Dim OpenBook As Workbook
    Dim NewRow As Long
    Application.ScreenUpdating = False
    FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files(*.xlsx*),*xlsx*")
    If FileToOpen <> False Then
        NewRow = Worksheets("Authorised Work").Range("A2").End(xlDown).Row + 1
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        OpenBook.Sheets(1).Range("A4:S300").Copy
        ThisWorkbook.Worksheets("Authorised Work").Range("A" & NewRow).PasteSpecial xlPasteValues
        OpenBook.Close False
    End If
    Application.ScreenUpdating = True
End Sub

As noted above, should be in Excel questions not Access.
 
Upvote 0
Solution
Try this:

VBA Code:
Sub Import_Data2()

' Import_Data2 Macro

    Dim FileToOpen As Variant
    Dim OpenBook As Workbook
    Dim NewRow As Long
    Application.ScreenUpdating = False
    FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files(*.xlsx*),*xlsx*")
    If FileToOpen <> False Then
        NewRow = Worksheets("Authorised Work").Range("A2").End(xlDown).Row + 1
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        OpenBook.Sheets(1).Range("A4:S300").Copy
        ThisWorkbook.Worksheets("Authorised Work").Range("A" & NewRow).PasteSpecial xlPasteValues
        OpenBook.Close False
    End If
    Application.ScreenUpdating = True
End Sub

As noted above, should be in Excel questions not Access.
Thank you for the advice and apologies it's in the wrong area. I'm new to Mr Excel and finding my way around.
 
Upvote 0

Forum statistics

Threads
1,224,810
Messages
6,181,079
Members
453,021
Latest member
Justyna P

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