Not able to delete column because of merged row in between

rickyckc

Active Member
Joined
Apr 1, 2004
Messages
327
Hi All,

I have a script that is supposed to...

1) Change formula to values
2) Clear validation
3) Delete all 3 conditional format and
4) Delete column G

In my sheet, Row A3:Q3 are merged. Row A8:Q8 are also merged.

My problem is, if I do it manually, I am able to delete column G but if I run my script, column A to Q are deleted. What's wrong with the my script below ?

Sub FormA()
'
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
With Selection.Validation
.Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween
.IgnoreBlank = True
.InCellDropdown = True
.ShowInput = True
.ShowError = True
End With
Selection.FormatConditions.Delete
Columns("G:G").Select
Range("G2").Activate
Selection.Delete Shift:=xlToLeft
Range("A2").Select
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi ricky,

Maybe trying only this:


Code:
Columns("G:G").Delete Shift:=xlToLeft
instead of this:
Code:
Columns("G:G").Select
  Range("G2").Activate
  Selection.Delete Shift:=xlToLeft
Hope this helps.

Regards
 
Upvote 0
Have you tried the script with the cells unmerged ??
Merged cells are an absolute nightmare.
try changing the merged cells to unmerged and then
Highlight A3:Q3 / Format / Cells /Alignment / Horizontal and select "Center Across Selection"
This will then appear to be merged.
Code:
Sub FormA()
'
Cells.Value = Cells.Value
    With Cells.Validation
        .Delete
        .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
        :=xlBetween
    End With
Cells.FormatConditions.Delete
Columns("G:G").Delete Shift:=xlToLeft
End Sub
 
Upvote 0
Hi cgcamal and Michael,

Thanks for your help and time. 'Columns("G:G").Delete Shift:=xlToLeft' works great !

Regards,
 
Upvote 0
Sorry, one more question please.

I have about 30 or 31 tabs to run this script. If I start from first tab, how to auto advance to next tab until the last tab and auto stops ?

My final script is below

Code:
Sub FormA()
'
' Keyboard Shortcut: Ctrl+u
'
    Cells.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
        :=xlBetween
        .IgnoreBlank = True
        .InCellDropdown = True
        .ShowInput = True
        .ShowError = True
    End With
    Selection.FormatConditions.Delete
    Columns("G:G").Delete Shift:=xlToLeft
    Columns("J:J").Delete Shift:=xlToLeft
    Columns("P:AU").Delete Shift:=xlToLeft
    Columns("AF:CC").Delete Shift:=xlToLeft
    Range("A2").Select
End Sub
 
Upvote 0
ricky,

To iterate through all sheets from the first one until the last sheet try with

Code:
Sub MyMacro
Dim Sh as Integer

For Sh = 1 To Sheets.Count
    With Sheets(Sh)
        [COLOR=DarkGreen]'Your code[/COLOR]
         
[COLOR=Green]' Don't forget to put the dot in front of required code within the "With" block for each Sheet[/COLOR]
.Columns("G:G").Delete Shift:=xlToLeft 
     End With
Next
End Sub

Hope this helps.

Regards.
 
Last edited:
Upvote 0
As in like this ? If 'Yes', I'm afraid it doesn't work. Just remains at tab I'm starting with and deleted off my columns G to CC. Btw, the tab I'm starting with is actually 2nd tab.

Code:
Sub MyMacro()
Dim Sh As Integer

For Sh = 1 To Sheets.Count
    With Sheets(Sh)

' Start of my script
    Cells.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
        :=xlBetween
        .IgnoreBlank = True
        .InCellDropdown = True
        .ShowInput = True
        .ShowError = True
    End With
    Selection.FormatConditions.Delete
    Columns("G:G").Delete Shift:=xlToLeft
    Columns("J:J").Delete Shift:=xlToLeft
    Columns("P:AU").Delete Shift:=xlToLeft
    Columns("AF:CC").Delete Shift:=xlToLeft
    Range("A2").Select
' End of my script

    End With
Next
End Sub
 
Upvote 0
cgcamal,

Ignore my last post. Below is my current code. It did went thru all the tabs. The starting tab was done fine. However, the next till the last tab somehow didn't convert formula to value.

Code:
Sub MyMacro()
Dim Sh As Integer

For Sh = 1 To Sheets.Count
    With Sheets(Sh)
    Cells.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
        :=xlBetween
        .IgnoreBlank = True
        .InCellDropdown = True
        .ShowInput = True
        .ShowError = True
    End With
    Selection.FormatConditions.Delete
    .Columns("G:G").Delete Shift:=xlToLeft
    .Columns("J:J").Delete Shift:=xlToLeft
    .Columns("P:AU").Delete Shift:=xlToLeft
    .Columns("AF:CC").Delete Shift:=xlToLeft
    Range("A2").Select
         End With
Next
End Sub
 
Upvote 0
Did you use my code in with Cesars changes ?
Also, put sh.activate before this line
Code:
sh.activate
With Sheets(Sh)
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,153
Members
452,891
Latest member
JUSTOUTOFMYREACH

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