VBA Advanced Filter works in local Workbook, but not when placed in Personal.xlsb

birosd

New Member
Joined
Jun 17, 2016
Messages
6
This code works in the local Workbook perfectly, but I can not figure out how (and I tried a couple of suggestions here on S.O.F.) to get it to work in the Personal.xlsb file. Any help is much appreciated.

Code:
'This macro works locally - need to get it to work at the Personal.xlsb level
Sub AdvLookupVals()

   Dim colA_Range As Range
   Dim rngLookupRange As Range
   Dim rngFoundRange As Range
   Dim wb As Workbook

  ThisWorkbook.Worksheets("Sheet1").Activate

   'when you do this, Lookup.xls becomes active and sets the range
   Set wb = Workbooks.Open("C:\Lists\Resource_Lookup.xlsx", True, True) ' open the source workbook, read only
   Set rngLookupRange = wb.Worksheets("Sheet1").Range("A1:A656")

   'Makes target workbook active again
    ThisWorkbook.Activate

   'Set rngFoundRange = wb.Worksheets("Sheet1").Range("A1:A653")
   'rngFoundRange.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= '_   'hoping this would make the macro work in Personal.xlsb

       Range("A1:A653").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
        rngLookupRange, Unique:=False                                               'rngLookupRange gets the criteria from above. .filters by name.

   'releases resources back to the system and close the other workbook
   Set rngLookupRange = Nothing
   wb.Close False ' close the source workbook without saving any changes
   Set wb = Nothing ' frees memory

 End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
This code works in the local Workbook perfectly, but I can not figure out how (and I tried a couple of suggestions here on S.O.F.) to get it to work in the Personal.xlsb file. Any help is much appreciated.

Code:
'This macro works locally - need to get it to work at the Personal.xlsb level
Sub AdvLookupVals()

   Dim colA_Range As Range
   Dim rngLookupRange As Range
   Dim rngFoundRange As Range
   Dim wb As Workbook

  ThisWorkbook.Worksheets("Sheet1").Activate

   'when you do this, Lookup.xls becomes active and sets the range
   Set wb = Workbooks.Open("C:\Lists\Resource_Lookup.xlsx", True, True) ' open the source workbook, read only
   Set rngLookupRange = wb.Worksheets("Sheet1").Range("A1:A656")

   'Makes target workbook active again
    ThisWorkbook.Activate

   'Set rngFoundRange = wb.Worksheets("Sheet1").Range("A1:A653")
   'rngFoundRange.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= '_   'hoping this would make the macro work in Personal.xlsb

       Range("A1:A653").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
        rngLookupRange, Unique:=False                                               'rngLookupRange gets the criteria from above. .filters by name.

   'releases resources back to the system and close the other workbook
   Set rngLookupRange = Nothing
   wb.Close False ' close the source workbook without saving any changes
   Set wb = Nothing ' frees memory

 End Sub
Hi birosd,

If you move this macro into a personal workbook instead of keeping it in the target workbook then you cannot use "ThisWorkbook" as a reference without the code trying to update the sheets in your personal workbook. I suspect you probably need to do something like:

Code:
Dim wb2 as Workbook
Set wb2 = Workbooks("Target Workbook Name.xlsx")

You would also need to replace any part of your code that says "ThisWorkbook" with wb2, so for example this line:

Code:
ThisWorkbook.Worksheets("Sheet1").Activate

Would become this:

Code:
wb2.Worksheets("Sheet1").Activate
 
Last edited:
Upvote 0
Thanks for the suggested changes, Fishboy. Although, 438 runtime errors came up with hitting the new updates at (both at the workbook and Personal.xlsb levels):
Code:
wb.Worksheets("Sheet1").Activate

and

Code:
wb2.Wooksheets("Sheet1").Active

in place of
Code:
ThisWorkbook.Worksheets(Sheet1).Activate
 
Upvote 0
Thanks for the suggested changes, Fishboy. Although, 438 runtime errors came up with hitting the new updates at (both at the workbook and Personal.xlsb levels):
Code:
wb.Worksheets("Sheet1").Activate

and

Code:
wb2.Wooksheets("Sheet1").Active

in place of
Code:
ThisWorkbook.Worksheets(Sheet1).Activate
Hi again birosd,

Are you able to share example copies of the 2 workbooks for us to test with (feel free to obscure any confidential data by either replacing it with xxxxxxx's or by swapping it out for made up data instead? If you can share then you will need to upload copies to a file hosting site such as Drop Box, One Drive, Google Drive or similar then share a link to the file(s) in a forum post.
 
Upvote 0
Thanks for the offer of additional help, Fishboy. I had to get the task done that this code was intended to do. So I had a little spark of inspiration and took a different route. Instead, used some code running from my Personal.xlsb that populated the first empty column of the active sheet with a Vlookup addressed to the closed workbook source. It then places an Autofilter on the data and filters for what I need, then it removes the additional column when finished.
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,658
Latest member
GStorm

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