Loop thru sheets

bobkap

Active Member
Joined
Nov 22, 2009
Messages
323
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
I cannot get this code to run except for my active sheet. I will not sequence to the next sheets in the workbook. Any help would be greatly appreciated.

finalcol = Cells(1, Columns.Count).End(xlToLeft).Column
finalrow = Cells(Rows.Count, 1).End(xlUp).Row
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Instpay" Or ws.Name <> "Payroll Data" Then
grosscol = Application.Match("Gross", Range("1:1"), 0)
grosstot = WorksheetFunction.Sum(Range(Cells(2, grosscol), Cells(finalrow, grosscol)))
Cells(finalrow + 1, grosscol).Value = grosstot
Cells(finalrow + 1, grosscol).Select
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End If
Next ws
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I cannot get this code to run except for my active sheet. I will not sequence to the next sheets in the workbook. Any help would be greatly appreciated.

finalcol = Cells(1, Columns.Count).End(xlToLeft).Column
finalrow = Cells(Rows.Count, 1).End(xlUp).Row
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Instpay" Or ws.Name <> "Payroll Data" Then
grosscol = Application.Match("Gross", Range("1:1"), 0)
grosstot = WorksheetFunction.Sum(Range(Cells(2, grosscol), Cells(finalrow, grosscol)))
Cells(finalrow + 1, grosscol).Value = grosstot
Cells(finalrow + 1, grosscol).Select
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End If
Next ws
I did not look at the rest of your code, but I think the reason it is not running as you want is because the red highlighted "Or" should be "And".
 
Upvote 0
Reference to the sheet is missing. I attached the macro with the changes, also considering Rick's note.
Sheet names are case sensitive

Code:
Sub test()
  Dim finalrow As Long, ws As Worksheet, grosscol As Long, grosstot As Variant
  For Each ws In ThisWorkbook.Worksheets
    If [COLOR=#0000ff]LCase(ws.Name) <> "instpay"[/COLOR] [COLOR=#ff0000]And [/COLOR][COLOR=#0000ff]LCase(ws.Name) <> "payroll data"[/COLOR] Then
      grosscol = Application.Match("Gross", [COLOR=#008000]ws[/COLOR].Range("1:1"), 0)
      finalrow = [COLOR=#008000]ws[/COLOR].Cells(Rows.Count, grosscol).End(xlUp).Row
      grosstot = WorksheetFunction.Sum([COLOR=#008000]ws[/COLOR].Range([COLOR=#008000]ws[/COLOR].Cells(2, grosscol), [COLOR=#008000]ws[/COLOR].Cells(finalrow, grosscol)))
      [COLOR=#008000]ws[/COLOR].Cells(finalrow + 1, grosscol).Value = grosstot
      With [COLOR=#008000]ws[/COLOR].Cells(finalrow + 1, grosscol).Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
      End With
    End If
  Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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