Plz help in modifying a vba related to Sheets dynamic

love_guy_1977

Board Regular
Joined
Aug 5, 2006
Messages
111
Hi guys,
I’m having a file with sheet named “Data” but this “data” sheet will change its name between time and time. My bellow macro is based on that sheet i.e “Data”. The sheet name in VBA object is Sheet5 (Data).
The first line in the bellow vba is ok but the 4th & 5th & 7th still having the reference of (Data) which Worksheets("Data").
So how can I change Worksheets("Data") to be sheet5. So if any time wanna change the sheet’s name it will not affect the vba.

Code:
Sub Sort_Data()
    Sheet5.Select     ‘ Mean sheet(“Data”).Select
    Columns("A:C").Select
    Application.CutCopyMode = False
    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add Key:=Range("B2:B45000") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Data").Sort
        .SetRange Range("A1:C45000")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Thank you
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Have you tried something like this?

Code:
Sub Sort_Data()
    Sheet5.Select     ‘ Mean sheet(“Data”).Select
    Columns("A:C").Select
    Application.CutCopyMode = False
    [B][COLOR=#ff0000]Sheet5.[/COLOR][/B]SortFields.Clear
    [B][COLOR=#ff0000]Sheet5.[/COLOR][/B]Sort.SortFields.Add Key:=Range("B2:B45000") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With [B][COLOR=red]Sheet5.[/COLOR][/B]Sort
        .SetRange Range("A1:C45000")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0
IT IS WORKING

Thank you

Have you tried something like this?

Code:
Sub Sort_Data()
    Sheet5.Select     ‘ Mean sheet(“Data”).Select
    Columns("A:C").Select
    Application.CutCopyMode = False
    [B][COLOR=#ff0000]Sheet5.[/COLOR][/B]SortFields.Clear
    [B][COLOR=#ff0000]Sheet5.[/COLOR][/B]Sort.SortFields.Add Key:=Range("B2:B45000") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With [B][COLOR=red]Sheet5.[/COLOR][/B]Sort
        .SetRange Range("A1:C45000")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,727
Messages
6,056,958
Members
444,899
Latest member
Excel_Temp

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