Help with VBA cell reference in loop

yits05

Board Regular
Joined
Jul 17, 2020
Messages
56
Office Version
  1. 2016
Platform
  1. Windows
Hello - I have the following VBA which is supposed to loop through a list of files and import tables from them. I did most of this via recording a macro, but am having trouble editing it so that when it loops, it references the cell in the next row. I get an error when I try to run the below. The "filepath" variable contains the file extension from which I extract the tables, and which I would like to change as the VBA loops. Many thanks!

VBA Code:
Dim reffile As Long
    Dim filepath As Range
    Dim i As Long
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    filepath = Range("A" & i)
    For i = 2 To FinalRow
        ActiveWorkbook.Queries.Add Name:="Table001 (Page 1)", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Pdf.Tables(File.Contents(""" & filepath & """), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & "    Table001 = Source{[Id=""Table001""]}[Data]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Table001"
        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table001 (Page 1)"";Extended Properties=""""" _
            , Destination:=Range("$P$2")).QueryTable
            .CommandType = xlCmdSql
            .CommandText = Array("SELECT * FROM [Table001 (Page 1)]")
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .ListObject.DisplayName = "Table001__Page_1"
        ActiveWorkbook.RefreshAll
        Application.Wait (Now + TimeValue("0:00:03"))
        ActiveWorkbook.Queries("Table001 (Page 1)").Delete
        Range("O1:s50").ClearContents

    
    End With
    Next i

  
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
to get rid of the error just swap these two lines around;
VBA Code:
   filepath = Range("A" & i)
    For i = 2 To FinalRow
should be
VBA Code:
    For i = 2 To FinalRow
   filepath = Range("A" & i)
Also I don't think you should define filepath as a range it should be a variant or a string
 
Upvote 0
Solution
to get rid of the error just swap these two lines around;
VBA Code:
   filepath = Range("A" & i)
    For i = 2 To FinalRow
should be
VBA Code:
    For i = 2 To FinalRow
   filepath = Range("A" & i)
Also I don't think you should define filepath as a range it should be a variant or a string
Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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