Use a variable Excel range to use in autofilter 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
add a comment |
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 '18 at 5:58
This stackoverflow.com/questions/24153818/… should answer your question.
– Michal Rosa
Nov 21 '18 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 '18 at 7:10
add a comment |
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 '18 at 6:04
Pang
6,9011664102
6,9011664102
asked Nov 21 '18 at 5:49
Jordan1Jordan1
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 '18 at 5:58
This stackoverflow.com/questions/24153818/… should answer your question.
– Michal Rosa
Nov 21 '18 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 '18 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 '18 at 5:58
This stackoverflow.com/questions/24153818/… should answer your question.
– Michal Rosa
Nov 21 '18 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 '18 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 '18 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 '18 at 5:58
This stackoverflow.com/questions/24153818/… should answer your question.
– Michal Rosa
Nov 21 '18 at 7:00
This stackoverflow.com/questions/24153818/… should answer your question.
– Michal Rosa
Nov 21 '18 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 '18 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 '18 at 7:10
add a comment |
1 Answer
1
active
oldest
votes
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 8:30
answered Nov 21 '18 at 7:08
rohrl77rohrl77
1,70442750
1,70442750
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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 '18 at 5:58
This stackoverflow.com/questions/24153818/… should answer your question.
– Michal Rosa
Nov 21 '18 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 '18 at 7:10