Use File Browser to Import Data from Another Workbook

talkinggoat

New Member
Joined
Feb 1, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
I found the thread below, which works great, but I need to modify it to incorporate a variable range. In other words, my source range length will always change.

I know I can use the following to determine the number of rows and columns, but how would I apply that to the source document? Sometimes I might need to select 300 lines, sometimes 30 or maybe 5 columns today and 9 tomorrow, depending on the document.

VBA Code:
'// Determine the number of columns. 
    lcol = Cells.Find(what:="*", _ 
        after:=Range("a1"), _ 
        lookat:=xlPart, _ 
        LookIn:=xlFormulas, _ 
        searchorder:=xlByColumns, _ 
        SearchDirection:=xlPrevious, _ 
        MatchCase:=False).Column
VBA Code:
'// Determine number of rows 
    lrow = Cells.Find(what:="*", _ 
        after:=Range("a1"), _ 
        lookat:=xlPart, _ 
        LookIn:=xlFormulas, _ 
        searchorder:=xlByRows, _ 
        SearchDirection:=xlPrevious, _ 
        MatchCase:=False).Row
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Sometimes I might need to select 300 lines, sometimes 30 or maybe 5 columns today and 9 tomorrow, depending on the document.
Does this mean you prefer a manual selection of the source range to be copied?
 
Upvote 0
VBA Code:
Sub insertFile() 
    Dim strFile As String 
    Dim wb As Workbook 
     
    With Application.FileDialog(3) 
        .AllowMultiSelect = False 
        If .Show Then 
            fullPath = .SelectedItems.Item(1) 
            Set wb = Workbooks.Open(fullPath) 
        End If 
        If wb Is Nothing Then Exit Sub 
         
        Set src = wb.Sheets(1) 
 
        lrow = src.Cells.Find(what:="*", _ 
        after:=Range("a1"), _ 
        lookat:=xlPart, _ 
        LookIn:=xlFormulas, _ 
        searchorder:=xlByRows, _ 
        SearchDirection:=xlPrevious, _ 
        MatchCase:=False).Row 
         
        lcol = src.Cells.Find(what:="*", _ 
        after:=Range("A1"), _ 
        lookat:=xlPart, _ 
        LookIn:=xlFormulas, _ 
        searchorder:=xlByColumns, _ 
        SearchDirection:=xlPrevious, _ 
        MatchCase:=False).Column 
         
        colLett = Split(Cells(1, lcol).Address, "$")(1) 
         
        src.Range("a1:" & colLett & lrow).Copy 
        ThisWorkbook.Worksheets("Sheet1").Activate 
        ActiveSheet.Range("A1").PasteSpecial 
        Application.CutCopyMode = False 
        wb.Close False 
    End With 
End Sub

Please let me know if there is a better way to do this.
 
Upvote 0
Hi,
try following update to your code & see if does what you want

VBA Code:
Sub InsertFile()
    Dim RowCol(1 To 2)  As Long, i As Long
    Dim fullPath        As String
    Dim wb              As Workbook
    Dim src             As Worksheet
    Dim PasteRange      As Range
 
    Set PasteRange = ThisWorkbook.Worksheets("Sheet1").Range("A1")
 
    With Application.FileDialog(3)
        .AllowMultiSelect = False
        If .Show Then
            fullPath = .SelectedItems.Item(1)
            Application.ScreenUpdating = False
            Set wb = Workbooks.Open(fullPath)
        End If
    End With
 
    If wb Is Nothing Then Exit Sub
 
    Set src = wb.Sheets(1)
 
  
    For i = xlRows To xlColumns
        With src.Cells.Find(what:="*", After:=src.Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, _
             SearchOrder:=i, SearchDirection:=xlPrevious, MatchCase:=False)
            RowCol(i) = Choose(i, .Row, .Column)
        End With
    Next
 
    src.Cells(1, 1).Resize(RowCol(1), RowCol(2)).Copy PasteRange
 
    wb.Close False
 
    Application.ScreenUpdating = True
 
End Sub

Dave
 
Upvote 0
Doesn't xlRows and xlColumns only work if the data is continuous (no spaces)?
 
Upvote 0
Both xlRows and xlColumns are Excel constants (with successively the values 1 and 2) and probably used to make the code more readable.
Perhaps you should try the code provided by @dmt32 first and see if it solves your issue.
 
Upvote 0
Hi,

As already explained by @GWteB xlRows & xlColumns are just constants with values 1 & 2 respectively and is same as doing this:

Code:
For i = 1 to 2

Next i

Using constants in place of their numeric value makes code a little more readable.
By the creation of a simple loop using these values, we can store Row & Column values in an array
which allows more compact code to get both the row & column values.

I overlooked including some error handling if there is possibility of the sheet being blank

Code:
Sub InsertFile()
    Dim RowCol(1 To 2)  As Long, i As Long
    Dim fullPath        As String
    Dim wb              As Workbook
    Dim src             As Worksheet
    Dim PasteRange      As Range
 
    Set PasteRange = ThisWorkbook.Worksheets("Sheet1").Range("A1")
 
    With Application.FileDialog(3)
        .AllowMultiSelect = False
        If .Show Then
            fullPath = .SelectedItems.Item(1)
            Application.ScreenUpdating = False
            Set wb = Workbooks.Open(fullPath)
        End If
    End With
 
    If wb Is Nothing Then Exit Sub
 
    Set src = wb.Sheets(1)
 
    On Error Resume Next
    For i = xlRows To xlColumns
        With src.Cells.Find(what:="*", After:=src.Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, _
             SearchOrder:=i, SearchDirection:=xlPrevious, MatchCase:=False)
            RowCol(i) = Choose(i, .Row, .Column)
        End With
        If RowCol(i) = 0 Then RowCol(i) = 1
    Next
    On Error GoTo 0

    src.Cells(1, 1).Resize(RowCol(1), RowCol(2)).Copy PasteRange
 
    wb.Close False
 
    Application.ScreenUpdating = True
 
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

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