loop truncating and renaming sheet vba excel

tuytuy

Board Regular
Joined
Mar 28, 2013
Messages
75
Hi,
i wrote this code to truncate a cell in a sheet, then rename it with the new value of this cell and delete the whole row afterwards. I need this to loop through all the sheets with the value of B1.
here is what i have so fare but it doesn't seem to work.

Code:
Dim workS As Worksheet
    For Each workS In ActiveWorkbook.Worksheets
' Truncating "section" cell


ActiveCell = Left(Range("B1"), 9)


' Replacing blanks by underscore for renaming purposes


ActiveCell = Replace(ActiveCell, " ", "_")


'Replacing "SheetX" with the name of the section


Dim ws1 As Worksheet
Set ws1 = ActiveSheet
ws1.Name = ws1.Range("A1")


'deleting row 1


Selection.EntireRow.Delete


    Next workS
 
Last edited:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Do you mean something like

Code:
Sub test()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
    With ws
        .Name = Replace(Left(.Name, 9), " ", "_")
    End With
Next ws
End Sub
 
Upvote 0
Try:

Code:
Sub Test()
    Dim workS As Worksheet
    For Each workS In ActiveWorkbook.Worksheets
        With workS.Range("B1")
'           Truncating "section" cell
'           Replacing blanks by underscore for renaming purposes
            .Value = Replace(Left(.Value, 9), " ", "_")
            .Parent.Name = .Value
            .EntireRow.Delete
        End With
    Next workS
End Sub
 
Upvote 0
Try:

Code:

Sub Test() Dim workS As Worksheet For Each workS In ActiveWorkbook.Worksheets With workS.Range("B1")' Truncating "section" cell' Replacing blanks by underscore for renaming purposes .Value = Replace(Left(.Value, 9), " ", "_") .Parent.Name = .Value .EntireRow.Delete End With Next workS </pre>

this would renam each sheet with the name of the first one.
i would want want each sheet to rename it self with their own B1 value
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,955
Members
449,199
Latest member
Riley Johnson

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