Excel Vba loop not working

Human_doing

Board Regular
Joined
Feb 16, 2011
Messages
137
Hi all,

Can anyone please explain why this code doesn't loop through all worksheets in the workbook and perform the formatting on each?

Code:
[FONT=Arial]Private Sub CommandButton1_Click()[/FONT]
[FONT=Arial]'Ask user to select Excel file[/FONT]
[FONT=Arial]NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Select the file with raw data for the report")[/FONT]
[FONT=Arial]If NewFN = False Then[/FONT]
[FONT=Arial]' They pressed Cancel[/FONT]
[FONT=Arial]MsgBox "Stopping because you did not select a file"[/FONT]
[FONT=Arial]Exit Sub[/FONT]
[FONT=Arial]Else[/FONT]
[FONT=Arial]Workbooks.Open Filename:=NewFN[/FONT]
[FONT=Arial]End If[/FONT]
 
[FONT=Arial]Dim Sh As Worksheet<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>[/FONT]
[FONT=Arial]   <o:p></o:p>[/FONT]
[FONT=Arial]   For Each Sh In ActiveWorkbook.Worksheets<o:p></o:p>[/FONT]
[FONT=Arial]   With Sh[/FONT]
 
[FONT=Arial]'Unmerge all cells[/FONT]
[FONT=Arial]   Cells.Select[/FONT]
[FONT=Arial]   Selection.MergeCells = False[/FONT]
[FONT=Arial]'Delete unnecessary columns and rows[/FONT]
[FONT=Arial]  Columns("A:A").Select[/FONT]
[FONT=Arial]  Selection.Delete Shift:=xlToLeft[/FONT]
[FONT=Arial]      Rows("1:3").Select[/FONT]
[FONT=Arial]   Selection.Delete Shift:=xlUp[/FONT]
 
[FONT=Arial]Next Sh[/FONT]
 
[FONT=Arial]End Sub [/FONT]

Any help much appreciated

Thanks
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try

Code:
Dim Sh As Worksheet

For Each Sh In ActiveWorkbook.Worksheets
    With Sh
    
        'Unmerge all cells
        .Cells.MergeCells = False
        'Delete unnecessary columns and rows
        .Columns("A").Delete
        .Rows("1:3").Delete
    End With
Next Sh
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,555
Members
452,928
Latest member
101blockchains

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