+ Reply to Thread
Results 1 to 6 of 6

How to use macros that can work on different Worksheets

Hybrid View

SteveBA How to use macros that can... 03-26-2017, 05:22 PM
JBeaucaire Re: How to use macros that... 03-26-2017, 05:31 PM
SocratesJC Re: How to use macros that... 03-27-2017, 07:42 AM
SteveBA Re: How to use macros that... 03-27-2017, 04:37 PM
JBeaucaire Re: How to use macros that... 03-27-2017, 05:06 PM
SocratesJC Re: How to use macros that... 03-28-2017, 03:00 AM
  1. #1
    Registered User
    Join Date
    09-02-2016
    Location
    Brooklyn, NY
    MS-Off Ver
    7
    Posts
    11

    How to use macros that can work on different Worksheets

    I'm pretty novice at programming Macros and coding VB so this is must be very basic question but I can't figuring it out by myself. I have recorded a Macro that moves and arranges some cells around my worksheet .it works wonderful until I tried to apply this Macro on another worksheet and then it stopped working right were the file name of the previous document was. My question is how to replace the filename in the macro with a veritable or current worksheet statement so it will work always with every file that I want.

    Example:

    Range("P2:P240").Select
        ActiveWorkbook.Worksheets("Download (6)").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Download (6)").Sort.SortFields.Add Key:=Range("P2" _
            ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Download (6)").Sort
            .SetRange Range("A2:AT17")
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Last edited by JBeaucaire; 03-26-2017 at 05:28 PM. Reason: Added missing CODE tags. Please read and follow the Forum Rules, link above in the menu bar. Thanks.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to use macros that can work on different Worksheets

    Remember CODE /CODE tagswhen posting code on the forum. Thanks. Fixed abive.

    Maybe like so:
        With ActiveSheet
            .Range("P2:P240").Select
            .Sort.SortFields.Clear
            .Sort.SortFields.Add Key:=Range("P2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            With .Sort
                .SetRange .Range("A2:AT17")
                .Header = xlNo
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        End With
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: How to use macros that can work on different Worksheets

    Range("P2:P240").Select
        ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
        ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("P2" _
            ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.ActiveSheet.Sort
            .SetRange Range("A2:AT17")
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Try this code? I just changed Worksheets("Download (6)") to ActiveSheet.
    Last edited by SocratesJC; 03-27-2017 at 07:46 AM.

  4. #4
    Registered User
    Join Date
    09-02-2016
    Location
    Brooklyn, NY
    MS-Off Ver
    7
    Posts
    11

    Re: How to use macros that can work on different Worksheets

    Thanks SocratesJC that was exactly what I needed. I have another question. On the same spreadsheet I need to delete certain rows that contain a specific information. In this case I need to delete every row that contains the words "ShipSvc:USPS..." . How to do in on VBA?

    Here is an example of my spreadsheet:

    spread1.JPG

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to use macros that can work on different Worksheets

    Assuming that column is column D, this is how I would do that, we delete all the found rows all at once at the end.
    Option Explicit
    
    Sub DeleteRows()
    Dim delRNG As Range, cell As Range
    
    For Each cell In ActiveSheet.Range("D:D").SpecialCells(xlConstants)
        If InStr(cell.Value, "ShipSvc:USPS") > 0 Then
            If delRNG Is Nothing Then
                Set delRNG = cell
            Else
                Set delRNG = Union(delRNG, cell)
            End If
    Next cell
    
    If Not delRNG Is Nothing Then delRNG.EntireRow.Delete xlShiftUp
    
    End Sub

  6. #6
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: How to use macros that can work on different Worksheets

    Your welcome SteveBA. I'm glad that worked for you.

    Try JBeaucaire's suggested code above and let us know if it works also (and try to mark the thread as solved if this concludes the issue).
    Last edited by SocratesJC; 03-28-2017 at 03:02 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macros to work in all worksheets
    By bigband1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-16-2016, 09:58 AM
  2. Macros stop to work when work sheet is protected. Run time error 1004
    By sellim in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-18-2012, 01:14 AM
  3. [SOLVED] How to globally get Macros to work around "Protected" /Locked Worksheets and Workbooks
    By nenadmail in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-23-2012, 03:21 AM
  4. [SOLVED] deleting worksheets from a file which has macros in it........does not work.
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-17-2012, 12:56 PM
  5. Macros to work on individual worksheets
    By thunder-foot in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-23-2009, 12:43 PM
  6. [SOLVED] Getting embeded Excel worksheets with macros in Word to work
    By dwight.yorke@gmail.com in forum Excel General
    Replies: 3
    Last Post: 06-20-2006, 02:35 PM
  7. [SOLVED] Getting embeded Excel worksheets with macros in Word to work
    By dwight.yorke@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-19-2006, 11:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1