Macro to filter a query based on user input

suttorpa

New Member
Joined
Dec 18, 2015
Messages
10
Hello,

I never excel scripted before.. so I am looking for some help with a macro, I created a workbook with a company inventory of all IT items.. each sheet has a listing of a certain type of device (workstations/switchs/keyboards/monitors/printers ect). I have a query that is activated by a macro that pulls all devices into one sheet with just a few related columns such as "location, serial number, model number, ect". The query works, but I am now trying to create a macro that accepts a users input of location and filters the query based on that so I can easily see what devices belong in which location. Here is the macro VBA as it is now (everything in black works, color Items I have added, the orange script works to query input, but the red script is creating an error.. I think it may be incorrect sequence, can anyone help?:

Sub Macro3()
'
' Macro3 Macro
'
' Keyboard Shortcut: Ctrl+e
'
Dim myValue As Variant
myValue = InputBox("Give me some input")
Sheets.Add After:=ActiveSheet
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Query Entire Inventory""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Query Entire Inventory]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.ListObject.DisplayName = "Query_Entire_Inventory"
.Refresh BackgroundQuery:=False
.ActiveSheet.Range("$A$1:$Z$9359").AutoFilter Field:=Location.Column, Criteria1:="myValue"
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try using "myvalue" without the quotation marks:
Code:
.ActiveSheet.Range("$A$1:$Z$9359").AutoFilter Field:=Location.Column, Criteria1:=myValue
 
Upvote 0
That progresses me a bit, it is actually detecting the code to autofilter, but it is giving me an error now on my variable.

Run-time error '424': Object Required

the myValue I am guessing needs to be declared as an object..

I just tried this Dim myValue As Object
But that gives me run-time error '91': Object variable or With block variable not set.
 
Upvote 0
Try: Dim myValue as String
 
Upvote 0
I changed to String, and checked the datavalue for the Location field (entire column set as "text").. but I continue to get Run-time error '424': Object required..

Location column is just port numbers.. 2-3 characters.. so 01D, 02D, 03D, some are a bit vague as SE for server room, or LA for label printing area.. Text values worked for me.

maybe there is a different way I can prompt for location and filter a catagory with input? I tried making and adjusting the commandText:
.CommandText = Array("SELECT * FROM [Query Entire Inventory] WHERE Location = myValue")

But that also gives 424 object required..
 
Upvote 0
Instead of location.column, you should use the actual column number. For example, if the location is in column A, then use: Field:=1
 
Upvote 0
I feel soo close.. yet so far.. Making that adjustment gives us a different error..

Run-Time Error '438':
Object doesn't support this property or method

here is my current code, debug stops at the red:

Sub Macro1()
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+e
'
Dim myValue As String
myValue = InputBox("Give me some input")


Sheets.Add After:=ActiveSheet
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Query Entire Inventory""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Query Entire Inventory]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.ListObject.DisplayName = "Query_Entire_Inventory"
.Refresh BackgroundQuery:=False
.ActiveSheet.Range("$A$1:$Z$9359").AutoFilter Field:=1, Criteria1:=myValue
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
End Sub
 
Upvote 0
The highlighted line of code is used to filter an Excel worksheet. Are you trying to filter an Excel sheet?
 
Upvote 0
Yes, it is filtering a query, that pulls data from 5 other worksheets.. Each worksheet is an inventory item of a different type. The idea is to pull All items in a particular location into its own separate sheet.. each sheet has a Location column.
 
Upvote 0
Basically.. all I want to do is create a query that pulls 4-5 matching columns from each worksheet based on user input for the Location column. I do not know of another way to do this.
 
Upvote 0

Forum statistics

Threads
1,214,984
Messages
6,122,601
Members
449,089
Latest member
Motoracer88

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