Loop in multiple sheets

shwetankbhardwaj

New Member
Joined
Apr 26, 2017
Messages
20
Hi there,

I am using the below codes to get the job done.

Sub Updatesheetname()

Dim sht As Worksheet

For Each sht In Worksheets

sht.Range("F2").Formula = "=MID(CELL(""filename"",R[211]C[-5]),FIND(""]"",CELL(""filename"",R[211]C[-5]))+1,31)"
sht.Range("F2").Copy
Range("E2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlUp)).Select
sht.Paste

Next sht

End Sub


But the problem here is, its not working for all the worksheets. I want this coding to work with all the sheets (except "Master workbook" coz this contains macro buttons).

Thanks in advance!

Thanks,
Shwetank
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Code:
Sub Updatesheetname()

Dim twb As Workbook
Dim sht As Worksheet
Dim lrow As Long

Set twb = ThisWorkbook
    
    For Each sht In twb.Worksheets
        
        If sht.Name <> "Master workbook" Then
        
            lrow = sht.Range("E2").End(xlDown).Row
            sht.Range("F2:F" & lrow).Formula = "=MID(CELL(""filename"",R[211]C[-5]),FIND(""]"",CELL(""filename"",R[211]C[-5]))+1,31)"
        
        End If
        
    Next sht

End Sub

not sure about that formula though.
 
Upvote 0
Hi Craggs82,

This is amazing!

It is working as I want, just a small problem, one of the tabs don't have any data in E column but F column fetched till end (Sheet name through value), however there shouldn't be any value in F column since there are no data in E column.

Can this be fixed too?

Thanks,
Shwetank
 
Upvote 0
If the data usually starts in E2 you could clarify it with another IF statment.

Code:
Sub Updatesheetname()

Dim twb As Workbook
Dim sht As Worksheet
Dim lrow As Long

Set twb = ThisWorkbook
    
    For Each sht In twb.Worksheets
        
        If sht.Name <> "Master workbook" Then
            
            If sht.Range("E2") <> "" Then
                lrow = sht.Range("E2").End(xlDown).Row
                sht.Range("F2:F" & lrow).Formula = "=MID(CELL(""filename"",R[211]C[-5]),FIND(""]"",CELL(""filename"",R[211]C[-5]))+1,31)"
            End If
            
        End If
        
    Next sht

End Sub

:biggrin:
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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