Use Variable to filter query data

APCaldwell

New Member
Joined
Dec 9, 2017
Messages
2
Hi, my name is Andrew and I am new to this forum, hope I do everything correctly,
I have a macro that extracts data from Access. before the data lands in the excel worksheet, I want to filter the data by a variable that is defined in a cell reference.

I have tried to use a dim option but I receive an error informing me that the variable text is unknown.

Please note my code, I have highlighted the variable in red, please can you assist me with what I am doing wrong?

Sheets("Transactions").Select
Dim mybrand As Range
Range("U1").Select
ActiveSheet.Calculate
Set mybrand = Range("U1")

Sheets("Metabase_Transactions").Select
ActiveWorkbook.Queries.Add Name:="Transactions_File", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Access.Database(File.Contents(""C:\MODZ\Kalbe\Meta_Base_Master\Meta_Base_Master v1.accdb""), [CreateNavigationProperties=true])," & Chr(13) & "" & Chr(10) & " _Transactions_File = Source{[Schema="""",Item=""Transactions_File""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Filtered Rows"" = Table.SelectRows(_Transactions_File, each ([Mas_Brand] =mybrand))" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Filtered Rows"""
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Transactions_File;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Transactions_File]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Transactions_File"
.Refresh BackgroundQuery:=False
End With

Hope you can help and again, please forgive me if I have said or done something wrong in this posting.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi, has anyone managed to look at this problem?

Do you need me to explain my query in more detail, please, I will be grateful for any assistance.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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