VBA Code not looping each sheet

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
780
Hello,

This code doesn't appear to loop through each sheet, any reason why?

Thanks.

VBA Code:
Sub Sheet_Loop()

sheet_count = ActiveWorkbook.Worksheets.Count

'loop through the worksheets in the workbook
For a = 1 To sheet_count

    Range("D10:D1000").Select
        With Selection
        Selection.NumberFormat = "General"
        .Value = .Value
        End With
    
    Range("E10:E1000").Select
    With Selection
        Selection.NumberFormat = "General"
        .Value = .Value
    End With
    
    Range("F10:F1000").Select
    With Selection
        Selection.NumberFormat = "General"
        .Value = .Value
    End With

Next a

End Sub
 

Some videos you may like

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,052
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
How about
VBA Code:
Sub MM1()
Dim ws As Worksheet
For Each ws In Worksheets
    With Range("D10:F1000")
        .NumberFormat = "General"
        .Value = .Value
    End With
Next ws
End Sub
 

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
780

ADVERTISEMENT

Nothing is protected.

It doesn't solve all the sheets, it is almost like it doesn't go into the next sheet?
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,052
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Change to
VBA Code:
Sub MM1()
Dim ws As Worksheet
For Each ws In Worksheets
    With ws.Range("D10:F1000")
        .NumberFormat = "General"
        .Value = .Value
    End With
Next ws
End Sub

Nothing is protected.

It doesn't solve all the sheets, it is almost like it doesn't go into the next sheet?
Did you try the amended code ??
 

aRandomHelper

Board Regular
Joined
Jan 14, 2021
Messages
236
Office Version
  1. 2016
Platform
  1. Windows
You need to qualify the Range with ws

VBA Code:
With ws.Range("D10:F1000")
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,127,808
Messages
5,627,010
Members
416,214
Latest member
boston814

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
Top