Columns("G:G").Select - Selects Entire Page?...

SGTBARRETT

New Member
Joined
Apr 22, 2015
Messages
2
I have written the following code to remove unwanted items from a sheet that is produced daily, elsewhere in my company. But for some reason, The first command selects the entire sheet and of course the delete command then deletes everything. I have a few columns that are at zero width, but I have even added script to set those to a width of 5 and it affects nothing. Everything still gets deleted.

If I run this on an empty sheet, it seems to work fine. Just not on the generated sheet. I'm gonna pull my hair out now...:(

-----------------------
Sub FormatCells()
'
' FormatCells Macro
'

'

Columns("G:G").Select
Range("G17").Activate
Selection.Delete Shift:=xlToLeft

Rows("1:9").Select
Range("A9").Activate
Selection.Delete Shift:=xlUp

Columns("M:Q").Select
Selection.Delete Shift:=xlToLeft

Columns("D:E").Select
Range("D17").Activate
Selection.Delete Shift:=xlToLeft

Columns("C:C").Select
Selection.ColumnWidth = 24.86

Columns("H:H").ColumnWidth = 55.86
Rows("2:60").Select
Selection.RowHeight = 24.75

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Does skipping the selection process fix the problem?

Code:
Sub FormatCells()
Columns("G:G").Delete shift:=xlToLeft
Rows("1:9").Delete shift:=xlToRight
Columns("M:Q").Delete shift:=xlToLeft
Columns("D:E").Delete shift:=xlToLeft
colmns("C:C").Delete shift:=xlToLeft
Columns("H:H").ColumnWidth = 55.86
Rows("2:60").rowheight = 24.75
End Sub
 
Upvote 0
Do you have merged cells? See if this behaves any differently:

Code:
Columns("G:G").Delete Shift:=xlToLeft
 
Upvote 0
Use the Debug\Step Into to watch each step this recorded macro performs.
Its doing exactly what it says and if run more than once it will delete everything on the worksheet.
The first step could be simplified to:
Columns("G:G").Delete Shift:=xlToLeft 'Deletes Column "G"

You also Delete Rows 1 through 9 with
Rows("1:9").Select
Range("A9").Activate
Selection.Delete Shift:=xlUp

This could be simplified to
Rows("1:9").Delete Shift:=xlUp ' Delete Row 1 thru 9

But deleting Row 1? Usually that is were headers are so that seems dubious to me.
And finally you delete columns D & E...?

Maybe you should consider Clear instead of Delete?
 
Upvote 0
IM AN IDIOT...

I failed to realize that the last line of the sheet was all the Columns merged together to form a footer... Never mind...

I cleaned up the code to look like this. Note the unmerge command at the top... LOL!!

Sub FormatCells()
'
' FormatCells Macro
'
Range("A1:O60").UnMerge

Rows("1:9").Delete Shift:=xlUp

Columns("A:A").ColumnWidth = 7.86

Columns("L:Q").Delete Shift:=xlToLeft

Columns("G:G").Delete Shift:=xlToLeft

Columns("D:E").Delete Shift:=xlToLeft

Columns("B:B").Select
Selection.Delete Shift:=xlToLeft

Columns("B:B").ColumnWidth = 28.86
Columns("G:G").ColumnWidth = 55.86

Rows("2:60").RowHeight = 24.75

End Sub
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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