Use a variable Excel range to use in autofilter VBA
up vote
2
down vote
favorite
I've searched a lot and tried a lot of different things, but I can't get the following to work.
I have a range of IDs that I generate. I then want to filter a range of data to produce a filtered output of just the specified IDs. However, whatever I try, my filtered output just returns a blank range.
Please note: my range of IDs varies in size each time I run this process. I've included a very simplistic set of data to illustrate my point - my real data is far more complex than my example.
The code I have at the moment:
Sub Test()
Dim Template As Workbook
Set Template = ThisWorkbook
Dim IDs, Report As Worksheet
Set IDs = Template.Worksheets("IDs")
Set Report = Template.Worksheets("Report")
Dim LastRowIDs As Long
LastRowIDs = IDs.Range("A" & IDs.Rows.Count).End(xlUp).Row
Dim IDsArray As Variant
IDsArray = IDs.Range("A2:A" & LastRowIDs)
Dim LastRowReport As Long
LastRowReport = Report.Range("A" & Report.Rows.Count).End(xlUp).Row
Report.AutoFilterMode = False
Report.Range("A1:C" & LastRowReport).AutoFilter Field:=1, Criteria1:=Application.Transpose(IDsArray), Operator:=xlFilterValues
End Sub
I would say I'm a moderately basic user of VBA so please explain any answers.
Range to filter on
Data to be filtered
Desired output
excel vba
add a comment |
up vote
2
down vote
favorite
I've searched a lot and tried a lot of different things, but I can't get the following to work.
I have a range of IDs that I generate. I then want to filter a range of data to produce a filtered output of just the specified IDs. However, whatever I try, my filtered output just returns a blank range.
Please note: my range of IDs varies in size each time I run this process. I've included a very simplistic set of data to illustrate my point - my real data is far more complex than my example.
The code I have at the moment:
Sub Test()
Dim Template As Workbook
Set Template = ThisWorkbook
Dim IDs, Report As Worksheet
Set IDs = Template.Worksheets("IDs")
Set Report = Template.Worksheets("Report")
Dim LastRowIDs As Long
LastRowIDs = IDs.Range("A" & IDs.Rows.Count).End(xlUp).Row
Dim IDsArray As Variant
IDsArray = IDs.Range("A2:A" & LastRowIDs)
Dim LastRowReport As Long
LastRowReport = Report.Range("A" & Report.Rows.Count).End(xlUp).Row
Report.AutoFilterMode = False
Report.Range("A1:C" & LastRowReport).AutoFilter Field:=1, Criteria1:=Application.Transpose(IDsArray), Operator:=xlFilterValues
End Sub
I would say I'm a moderately basic user of VBA so please explain any answers.
Range to filter on
Data to be filtered
Desired output
excel vba
Could you please edit your question and add tags around the code.. Makes it easier to read and understand... or highlight the code and press ctrl + k
– alowflyingpig
Nov 21 at 5:58
This stackoverflow.com/questions/24153818/… should answer your question.
– Michal Rosa
Nov 21 at 7:00
I feel that my code is doing the exact same thing as the solution in your link - can you please explain why mine doesn't work the same way?
– Jordan1
Nov 21 at 7:10
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I've searched a lot and tried a lot of different things, but I can't get the following to work.
I have a range of IDs that I generate. I then want to filter a range of data to produce a filtered output of just the specified IDs. However, whatever I try, my filtered output just returns a blank range.
Please note: my range of IDs varies in size each time I run this process. I've included a very simplistic set of data to illustrate my point - my real data is far more complex than my example.
The code I have at the moment:
Sub Test()
Dim Template As Workbook
Set Template = ThisWorkbook
Dim IDs, Report As Worksheet
Set IDs = Template.Worksheets("IDs")
Set Report = Template.Worksheets("Report")
Dim LastRowIDs As Long
LastRowIDs = IDs.Range("A" & IDs.Rows.Count).End(xlUp).Row
Dim IDsArray As Variant
IDsArray = IDs.Range("A2:A" & LastRowIDs)
Dim LastRowReport As Long
LastRowReport = Report.Range("A" & Report.Rows.Count).End(xlUp).Row
Report.AutoFilterMode = False
Report.Range("A1:C" & LastRowReport).AutoFilter Field:=1, Criteria1:=Application.Transpose(IDsArray), Operator:=xlFilterValues
End Sub
I would say I'm a moderately basic user of VBA so please explain any answers.
Range to filter on
Data to be filtered
Desired output
excel vba
I've searched a lot and tried a lot of different things, but I can't get the following to work.
I have a range of IDs that I generate. I then want to filter a range of data to produce a filtered output of just the specified IDs. However, whatever I try, my filtered output just returns a blank range.
Please note: my range of IDs varies in size each time I run this process. I've included a very simplistic set of data to illustrate my point - my real data is far more complex than my example.
The code I have at the moment:
Sub Test()
Dim Template As Workbook
Set Template = ThisWorkbook
Dim IDs, Report As Worksheet
Set IDs = Template.Worksheets("IDs")
Set Report = Template.Worksheets("Report")
Dim LastRowIDs As Long
LastRowIDs = IDs.Range("A" & IDs.Rows.Count).End(xlUp).Row
Dim IDsArray As Variant
IDsArray = IDs.Range("A2:A" & LastRowIDs)
Dim LastRowReport As Long
LastRowReport = Report.Range("A" & Report.Rows.Count).End(xlUp).Row
Report.AutoFilterMode = False
Report.Range("A1:C" & LastRowReport).AutoFilter Field:=1, Criteria1:=Application.Transpose(IDsArray), Operator:=xlFilterValues
End Sub
I would say I'm a moderately basic user of VBA so please explain any answers.
Range to filter on
Data to be filtered
Desired output
excel vba
excel vba
edited Nov 21 at 6:04
Pang
6,8151563101
6,8151563101
asked Nov 21 at 5:49
Jordan1
111
111
Could you please edit your question and add tags around the code.. Makes it easier to read and understand... or highlight the code and press ctrl + k
– alowflyingpig
Nov 21 at 5:58
This stackoverflow.com/questions/24153818/… should answer your question.
– Michal Rosa
Nov 21 at 7:00
I feel that my code is doing the exact same thing as the solution in your link - can you please explain why mine doesn't work the same way?
– Jordan1
Nov 21 at 7:10
add a comment |
Could you please edit your question and add tags around the code.. Makes it easier to read and understand... or highlight the code and press ctrl + k
– alowflyingpig
Nov 21 at 5:58
This stackoverflow.com/questions/24153818/… should answer your question.
– Michal Rosa
Nov 21 at 7:00
I feel that my code is doing the exact same thing as the solution in your link - can you please explain why mine doesn't work the same way?
– Jordan1
Nov 21 at 7:10
Could you please edit your question and add tags around the code.. Makes it easier to read and understand... or highlight the code and press ctrl + k
– alowflyingpig
Nov 21 at 5:58
Could you please edit your question and add tags around the code.. Makes it easier to read and understand... or highlight the code and press ctrl + k
– alowflyingpig
Nov 21 at 5:58
This stackoverflow.com/questions/24153818/… should answer your question.
– Michal Rosa
Nov 21 at 7:00
This stackoverflow.com/questions/24153818/… should answer your question.
– Michal Rosa
Nov 21 at 7:00
I feel that my code is doing the exact same thing as the solution in your link - can you please explain why mine doesn't work the same way?
– Jordan1
Nov 21 at 7:10
I feel that my code is doing the exact same thing as the solution in your link - can you please explain why mine doesn't work the same way?
– Jordan1
Nov 21 at 7:10
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Excel likes it's autofilter arrays as strings. So you need to convert the array to string values. Then it works.
Sub Test()
Dim Template As Workbook
Set Template = ThisWorkbook
Dim IDs, Report As Worksheet
Set IDs = Template.Worksheets("IDs")
Set Report = Template.Worksheets("Report")
Dim LastRowIDs As Long
LastRowIDs = IDs.Range("A" & IDs.Rows.Count).End(xlUp).Row
Dim IDsArray As Variant
Dim sTemp As String, i As Integer
IDsArray = Application.Transpose(IDs.Range("A2:A" & LastRowIDs))
For i = LBound(IDsArray) To UBound(IDsArray)
sTemp = "," & IDsArray(i) & sTemp
Next i
IDsArray = Split(Mid(sTemp, 2), ",")
Dim LastRowReport As Long
LastRowReport = Report.Range("A" & Report.Rows.Count).End(xlUp).Row
Report.AutoFilterMode = False
Report.Range("A1:C" & LastRowReport).AutoFilter Field:=1, Criteria1:=IDsArray, Operator:=xlFilterValues
End Sub
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Excel likes it's autofilter arrays as strings. So you need to convert the array to string values. Then it works.
Sub Test()
Dim Template As Workbook
Set Template = ThisWorkbook
Dim IDs, Report As Worksheet
Set IDs = Template.Worksheets("IDs")
Set Report = Template.Worksheets("Report")
Dim LastRowIDs As Long
LastRowIDs = IDs.Range("A" & IDs.Rows.Count).End(xlUp).Row
Dim IDsArray As Variant
Dim sTemp As String, i As Integer
IDsArray = Application.Transpose(IDs.Range("A2:A" & LastRowIDs))
For i = LBound(IDsArray) To UBound(IDsArray)
sTemp = "," & IDsArray(i) & sTemp
Next i
IDsArray = Split(Mid(sTemp, 2), ",")
Dim LastRowReport As Long
LastRowReport = Report.Range("A" & Report.Rows.Count).End(xlUp).Row
Report.AutoFilterMode = False
Report.Range("A1:C" & LastRowReport).AutoFilter Field:=1, Criteria1:=IDsArray, Operator:=xlFilterValues
End Sub
add a comment |
up vote
0
down vote
Excel likes it's autofilter arrays as strings. So you need to convert the array to string values. Then it works.
Sub Test()
Dim Template As Workbook
Set Template = ThisWorkbook
Dim IDs, Report As Worksheet
Set IDs = Template.Worksheets("IDs")
Set Report = Template.Worksheets("Report")
Dim LastRowIDs As Long
LastRowIDs = IDs.Range("A" & IDs.Rows.Count).End(xlUp).Row
Dim IDsArray As Variant
Dim sTemp As String, i As Integer
IDsArray = Application.Transpose(IDs.Range("A2:A" & LastRowIDs))
For i = LBound(IDsArray) To UBound(IDsArray)
sTemp = "," & IDsArray(i) & sTemp
Next i
IDsArray = Split(Mid(sTemp, 2), ",")
Dim LastRowReport As Long
LastRowReport = Report.Range("A" & Report.Rows.Count).End(xlUp).Row
Report.AutoFilterMode = False
Report.Range("A1:C" & LastRowReport).AutoFilter Field:=1, Criteria1:=IDsArray, Operator:=xlFilterValues
End Sub
add a comment |
up vote
0
down vote
up vote
0
down vote
Excel likes it's autofilter arrays as strings. So you need to convert the array to string values. Then it works.
Sub Test()
Dim Template As Workbook
Set Template = ThisWorkbook
Dim IDs, Report As Worksheet
Set IDs = Template.Worksheets("IDs")
Set Report = Template.Worksheets("Report")
Dim LastRowIDs As Long
LastRowIDs = IDs.Range("A" & IDs.Rows.Count).End(xlUp).Row
Dim IDsArray As Variant
Dim sTemp As String, i As Integer
IDsArray = Application.Transpose(IDs.Range("A2:A" & LastRowIDs))
For i = LBound(IDsArray) To UBound(IDsArray)
sTemp = "," & IDsArray(i) & sTemp
Next i
IDsArray = Split(Mid(sTemp, 2), ",")
Dim LastRowReport As Long
LastRowReport = Report.Range("A" & Report.Rows.Count).End(xlUp).Row
Report.AutoFilterMode = False
Report.Range("A1:C" & LastRowReport).AutoFilter Field:=1, Criteria1:=IDsArray, Operator:=xlFilterValues
End Sub
Excel likes it's autofilter arrays as strings. So you need to convert the array to string values. Then it works.
Sub Test()
Dim Template As Workbook
Set Template = ThisWorkbook
Dim IDs, Report As Worksheet
Set IDs = Template.Worksheets("IDs")
Set Report = Template.Worksheets("Report")
Dim LastRowIDs As Long
LastRowIDs = IDs.Range("A" & IDs.Rows.Count).End(xlUp).Row
Dim IDsArray As Variant
Dim sTemp As String, i As Integer
IDsArray = Application.Transpose(IDs.Range("A2:A" & LastRowIDs))
For i = LBound(IDsArray) To UBound(IDsArray)
sTemp = "," & IDsArray(i) & sTemp
Next i
IDsArray = Split(Mid(sTemp, 2), ",")
Dim LastRowReport As Long
LastRowReport = Report.Range("A" & Report.Rows.Count).End(xlUp).Row
Report.AutoFilterMode = False
Report.Range("A1:C" & LastRowReport).AutoFilter Field:=1, Criteria1:=IDsArray, Operator:=xlFilterValues
End Sub
edited Nov 21 at 8:30
answered Nov 21 at 7:08
rohrl77
1,66942749
1,66942749
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405955%2fuse-a-variable-excel-range-to-use-in-autofilter-vba%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you please edit your question and add tags around the code.. Makes it easier to read and understand... or highlight the code and press ctrl + k
– alowflyingpig
Nov 21 at 5:58
This stackoverflow.com/questions/24153818/… should answer your question.
– Michal Rosa
Nov 21 at 7:00
I feel that my code is doing the exact same thing as the solution in your link - can you please explain why mine doesn't work the same way?
– Jordan1
Nov 21 at 7:10