Selecting workbook then worksheet without using sheet name

ajt8888

New Member
Joined
Aug 23, 2010
Messages
35
Office Version
  1. 2019
Platform
  1. Windows
I am trying to use a macro across a number of worksheets in a workbook I am not very good at writing VBA so just record it but from some reason the macro just references the one sheet how do I change it so that it doesnt just look for the same sheet all the time? Also the number of rows varies in each sheet how do I get it to select however many rows there are?

Columns("B:B").Select
ActiveWorkbook.Worksheets("F1711412").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("F1711412").Sort.SortFields.Add2 Key:=Range( _
"B2:B91"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("F1711412").Sort
.SetRange Range("B1:B91")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A50")
Range("A2:A50").Select
Range("B1").Select
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Run the following on the sheet you need.

Code:
Sub test()
  Dim lr As Long
  lr = Range("B" & Rows.Count).End(xlUp).Row
  Range("B1:B" & lr).Sort key1:=Range("B1"), order1:=xlAscending, Header:=xlYes
  Range("A2").AutoFill Destination:=Range("A2:A" & lr)
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,936
Members
452,539
Latest member
delvey

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