Merge Cells VBA Run Time Error 1004

jap7675

New Member
Joined
Nov 13, 2015
Messages
23
Hello,

I'm having a small problem with part of a code - and just dont know why it isn't working when using Private Sub Workbook_Open() whereas it works when using commandbutton()!


Private Sub Execute()

'Clear All Previous Data
With Sheets("Summary").Range("A3:K310")
.Interior.ColorIndex = none
.ClearContents
.MergeCells = False
End With

'Speeds up calculation by stopping certain Excel Actions
Application.ScreenUpdating = False
Application.DisplayStatusBar = True
Application.EnableEvents = False

'Recalculates Background
Worksheets("Plant_Bkgrd").Calculate

'TITLE & SUMMARY SETTINGS------------------------------------------------------------------------------------

'Title: Display Settings
Dim i As Integer 'Merge first columns
For i = 3 To 9
Sheets("Summary").Range(Cells(i, 2), Cells(i, 9)).MergeCells = True
Next i​

the problem flags up on the penultimate line, and i just cant see a reason.

Please help!

All the best
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You need to be on the sheet first. So add this line before your "For i = 3 to 9" line:
Code:
Sheets("Summary").Select
 
Upvote 0
Try this.
Code:
Dim i As Integer 'Merge first columns
For i = 3 To 9
    With Sheets("Summary")
        .Range(.Cells(i, 2), .Cells(i, 9)).MergeCells = True
    End With
Next i
PS You should avoid using merged cells.
 
Upvote 0
PS You should avoid using merged cells.
Norie makes a good point. Merged cells can cause lots of issues for things like sorting and VBA.
If you are just merging across a row, you can get the same visual effects using the "Center Across Selection" formatting option without all the issues that Merged Cells bring to the table.

That code would look like this:
Code:
Dim i As Integer
For i = 3 To 9
    With Sheets("Summary")
        .Range(.Cells(i, 2), .Cells(i, 9)).HorizontalAlignment = xlCenterAcrossSelection
    End With
Next i
 
Upvote 0
Perfect,

Thankyou for all your help - much appreciated!
Thanks also for the pointer - I'm aware I shouldnt use merged cells, but theres no other way to get my desired look!
 
Upvote 0
I'm aware I shouldnt use merged cells, but theres no other way to get my desired look!
Did you see what I said up above? If you are just merging across single rows at a time (which it sounds like what you are doing), the "Center Across Selection" should give you the desired look you want without all the issues!
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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