“DB” sheet contains 1 table with 3 Columns – Trade Date, ISIN, Trade ID. This is the output of a power query to pull data from the database (based on certain criteria) and display it here.
“Current” sheet contains a range from Column A to E.
The macro has been setup to copy data from “DB” sheet and paste it on the next empty row of “Current” sheet.
Sometimes “DB” sheet contains no data. And it leads to error 91 when i run my macro.
tabledb.
DataBodyRange.Copy –> this code is giving the error.
How to work around this?
Sub PullDBData()
Dim tabledb As ListObject
Dim tableisin As ListObject
Dim lr As Long
Set tabledb = Sheets("DB").ListObjects("DB")
tabledb.QueryTable.Refresh BackgroundQuery:=False
tabledb.DataBodyRange.Copy
lr = Sheets("Current").Cells(Rows.Count, 1).End(xlUp).Row + 1
Sheets("Current").Range("A" & lr).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
tabledb.QueryTable.Refresh BackgroundQuery:=False
Set tableisin = Sheets("Current").ListObjects("ISIN_2")
tableisin.QueryTable.Refresh BackgroundQuery:=False
End Sub
Raymond is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1