problem with the Sheet name, urgent help required

Amit1

New Member
Joined
Jul 6, 2012
Messages
23
Dear Friends,
I am using a macro for formatting and sorting (based on a particular column value) for more than 1000 of files (wbk). The problem is i have to rename each time i run the macro in the code for the renamed "sheet1".Kindly suggest some way where i do not have to rename it every time.The code is provided below for reference:
Sub format1()
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "Heading1"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Heading2"
Range("C1").Select
Columns("A:A").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
ActiveCell.FormulaR1C1 = "Heading3"
Columns("D:D").Select
Columns("C:C").EntireColumn.AutoFit
Range("D1").Select
ActiveCell.FormulaR1C1 = "Heading4"
Columns("E:E").Select
Columns("D:D").EntireColumn.AutoFit
Range("E1").Select
ActiveCell.FormulaR1C1 = "Heading5"
Range("F1").Select
ActiveCell.FormulaR1C1 = "Heading6"
Columns("G:G").Select
Columns("F:F").EntireColumn.AutoFit
Range("G1").Select
ActiveCell.FormulaR1C1 = "Heading7"
Columns("H:H").Select
Columns("G:G").EntireColumn.AutoFit
Range("H1").Select
ActiveCell.FormulaR1C1 = "Heading8"
Columns("I:I").Select
Columns("H:H").EntireColumn.AutoFit
Range("I1").Select
ActiveCell.FormulaR1C1 = "Heading9"
Columns("J:J").Select
Columns("I:I").EntireColumn.AutoFit
Selection.Delete Shift:=xlToLeft
Range("J1").Select
ActiveCell.FormulaR1C1 = "Heading10"
Columns("K:K").Select
Columns("J:J").EntireColumn.AutoFit
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 8
ActiveWindow.ScrollColumn = 9
ActiveWindow.ScrollColumn = 10
ActiveWindow.ScrollColumn = 11
ActiveWindow.ScrollColumn = 10
ActiveWindow.ScrollColumn = 9
ActiveWindow.ScrollColumn = 8
ActiveWindow.ScrollColumn = 7
Columns("K:CE").Select
Selection.ClearContents
ActiveWorkbook.Save
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Range("K1").Select
Range("A1:J200").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear (This "Sheet1" should be renamed every time)
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C2:C200") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:J200")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Save
End Sub

Looking forward for your help
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hello, maybe this can help:
Code:
ActiveWorkbook.Worksheets("Sheet1").Name = "Sheet4"
or this
Code:
ActiveWorkbook.Worksheets(1).Sort.SortFields.Clear
 
Upvote 0
This should do about the same as your original macro without having to rename the sheet:
Code:
Sub MicroFormat()
Dim Rng As Range
Columns("A:A").Delete shift:=xlToLeft
Rows("1:1").Delete shift:=xlUp
Rows("1:1").Insert shift:=xlDown
Range("J:J").Delete shift:=xlToRight
With Range("A1:J1")
    .Formula = "=""Heading ""&Column(A1)"
    .Value = .Value
    .EntireColumn.AutoFit
End With
Columns("K:CE").ClearContents
Set Rng = Range("A1").CurrentRegion
With ActiveSheet.Sort
    .SortFields.Clear
    .SortFields.Add Key:=Intersect(Rng, Range("C:C")), _
        SortOn:=xlSortOnValues, _
        Order:=xlDescending, _
        DataOption:=xlSortNormal
    .SetRange Rng
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveWorkbook.Save
End Sub
 
Upvote 0
Dear Friends,
Thanks a lot for your support, now the code is working fine and am able to gather the output.
Thank you all once again.
Best Regards,
Amit
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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