VBA Code Working Incorrectly, Loops

Sherifa

New Member
Joined
Oct 23, 2017
Messages
45
I have a list of values which I want to be part of my Header names.
The problems is the code cycles through all the names for each worksheet, rather than matching worksheet 3 to d8 and 4 to d9 etc.

Code:
Sub RenameRows()    Dim Wkb As Workbook
    Dim ws As Sheets
    
    Set Wkb = ThisWorkbook
    Set ws = ThisWorkbook.Worksheets




Dim i As Integer
Dim j As Integer
       
ws_count = Wkb.Worksheets.Count


For i = 3 To ws_count
For j = 8 To 55
            
            
        Wkb.Worksheets(i).Range("A1") = ws(i).Name & " " & Sheets("Cost of Cover").Cells(j, 4)
            
        
    Next
 
 Next
     
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Untested, but try
Code:
Sub RenameRows()
    Dim Wkb As Workbook
    Dim i As Long
    Dim j As Long

    Set Wkb = ThisWorkbook

    j = 8
    For i = 3 To Wkb.Worksheets.Count
        Wkb.Worksheets(i).Range("A1") = ws(i).Name & " " & Sheets("Cost of Cover").Cells(j, 4)
        j = j + 1
    Next i
     
End Sub
 
Upvote 0
I've just defined it. I think I didn't do it properly the first time.

Code:
Sub RenameRowstest()

    Dim Wkb As Workbook
    Dim ws As Sheets
    
    Set Wkb = ThisWorkbook
    Set ws = ThisWorkbook.Worksheets


    Dim i As Long
    Dim j As Long


    Set Wkb = ThisWorkbook


    j = 8
    For i = 3 To Wkb.Worksheets.Count
        Wkb.Worksheets(i).Range("A1") = ws(i).Name & " " & Sheets("Cost of Cover").Cells(j, 4)
        j = j + 1
    Next i
     
End Sub

How do you know when to use Long in VBA?
 
Upvote 0
How do you know when to use Long in VBA?
There's very little point in using Integer these days, as the compiler will convert them to long
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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