Using VBA to reference a sheet in the same file that the macro is run from

prupert

New Member
Joined
Aug 31, 2011
Messages
5
Hello

I have a macro that I have created from lots and lots of Googling and taking ideas and snippets from various post, mostly from this very forum, so first a bigh THANKS!

I sadly know next to nothing about VBA, as I only really have BASH experience and a bit of Python, so I get lost very quickly.

My code that I have created works fine, except that the file the macro is contained within can't be renamed as my current macro references a sheet within the file. I would like the excel file that contains the macro to be called anything and the macro to still work regardless, but I can't seem to find a way to refernece the sheet within the file, I know there are various options, but none of them seem to work with the code I have.

Here is bit of code that is causing the issue:

Code:
' With Sheets("Criteria")
    With Workbooks("MyCleverMacros.xlsm").Worksheets("Library")
        Set CriteriaRng = .Range("A1", .Cells(Rows.Count, "A").End(xlUp))
    End With
    'Loop through the cells in the Criteria range
    For Each cell In CriteriaRng
        WordListSheet.Activate
        With ActiveSheet
            'Firstly, remove the AutoFilter
            .AutoFilterMode = False
            'Apply the filter
            .Range("A1:A" & .Rows.Count).AutoFilter Field:=1, Criteria1:=cell.Value
            With .AutoFilter.Range
                Set rng = Nothing
                On Error Resume Next
                Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
                          .SpecialCells(xlCellTypeVisible)
                On Error GoTo 0
                If Not rng Is Nothing Then rng.EntireRow.Delete
            End With
            'Remove the AutoFilter
            .AutoFilterMode = False
        End With
    Next cell

Basically, what this part of the code does is it reads through the words listed in the sheet called "Library" and deletes them from the selected cells if they occur. The problem is this line: With Workbooks("MyCleverMacros.xlsm").Worksheets("Library") as you can see it references a specific workbook called MyCleverMacro and a sheet within it called Library. I want it to reference the sheet called Library only and not care about the actual name of the XLSM file. The macro that runs this bit of code will always be contained within the XLSM file that contains the Library sheet, so that isn't the problem, I just can't work out how to refence the actual Sheet only based on the code I am using. I know it is something along the lines of Sheets("Library").Select or simply Worksheets("Library"), but that doesn't seem to work with the way my code is written.


Can anyone help?
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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