Public Function MergeCells()
Dim Workbook As Excel.Workbook
Dim Worksheet As Excel.Worksheet
Dim Range As Excel.Range
Dim Text As String
Dim BackColor As Long
Set Workbook = ThisWorkbook
Set Worksheet = Workbook.Worksheets(1)
Set Range = Worksheet.Range("E1", "L1")
Text = "Some text"
BackColor = RGB(100, 150, 200)
Range.Merge
Range.Value = Text
Range.Interior.Color = BackColor
Set Range = Nothing
Set Worksheet = Nothing
Set Workbook = Nothing
End Function
Dim oRange as Excel.Range
Set oRange = oSHT.Range("E1", "L1")
oRange.Merge
oRange.Value = "Weekly data"
oRange.Interior.Color = RGB(255, 255, 204)
And here is the error:Run-time error '1004':
Application =-defined or object-defined error
I don't normally do it this way, I normally do it like: oSHT.columns("N").EntireColumn.Autofit
with osht.Range("E1", "L1")
.Merge
.Value = "Weekly data"
.Interior.Color = RGB(255, 255, 204)
end with
But this is generating the same error on the ".Merge" line. All of the rest of the code that references oSHT is working just fine.
Option Compare Database
Option Explicit
Sub tt()
1000 Dim ap As Object, objsht As Object
1010 Set ap = GetObject(, "excel.application")
1020 Set objsht = ap.activesheet
1025 If TypeName(objsht) <> "Worksheet" Then
MsgBox "objSht is not a Worksheet. It is a " & TypeName(objsht)
End If
1030 With objsht.[e1:l1]
1040 .Merge
1050 .Value2 = "some text"
1060 .Interior.Color = vbYellow
1070 End With
Exit Sub
err_routine:
MsgBox "error #" & Err & "(" & Error & ") encountered at line " & Erl
Exit Sub
Dim oRange as Excel.Range
Set oRange = oSHT.Range("E1", "L1")
osht.unprotect ' <=== try unprotecting sheet
oRange.Interior.Color = RGB(255, 255, 204) ' <== also put color change first to see if that much works
oRange.Merge
oRange.Value = "Weekly data"
If that doesn't work, try this code in your Access program.. Line 5 will "kill" all VBA activity.. Immediately switch to Excel and use Alt HMM to manually merge the cells. I expect the manual merge will still fail which will help you figure things out. Dim oRange as Excel.Range
Set oRange = oSHT.Range("E1", "L1")
osht.activate
oRange.select
end ' <== this will kill VBA. You can then go to Excel and try merging the cells manually
oRange.Merge
oRange.Value = "Weekly data"
oRange.Interior.Color = RGB(255, 255, 204)
dim oSHT as Excel.Worksheet
dim oRange as Excel.Range
set oSHT = oWBK.Worksheets(n)
'Write the column headers to the selected worksheet
For intcol = 0 To rs.Fields.Count - 1
oSHT.Cells(2, intcol + 1).Value = Replace(Replace(rs.Fields(intcol).Name, "Wk_", ""), "YTD_", "")
Next
oSHT.Range("A3").CopyFromRecordset rs
oSHT.Columns("B:D").EntireColumn.AutoFit
oSHT.Columns("E:M").EntireColumn.ColumnWidth = 6
oSHT.Columns("N:N").NumberFormat = "0.00%"
oSHT.Columns("N").EntireColumn.AutoFit
oSHT.Range("E:N").HorizontalAlignment = xlCenter
Set oRange = oSHT.Range("E1", "L1")
With oRange
.Merge '<= This line raises the 1004 error
.Value = "Weekly data"
.Interior.Color = RGB(255, 255, 204)
end with
oSHT.Columns("M:M").EntireColumn.Hidden = True
oSHT.Cells(2, 15).Value = 0
oSHT.Range("O2").ClearContents
oSHT.Columns("O").EntireColumn.ColumnWidth = 4
oSHT.Columns("X").NumberFormat = "0.00%"
oSHT.Columns("P:W").EntireColumn.ColumnWidth = 7
oSHT.Columns("X").EntireColumn.AutoFit
oSHT.Range("P:X").HorizontalAlignment = xlCenter
Set oRange = oSHT.Range("P1", "X1")
With oRange
.Merge '<= This line raises the 1004 error
.Value2 = "Year-to-Date data"
.Interior.Color = RGB(255, 255, 204)
End With
The only lines of code in this entire code segment which fail are the two .Merge lines.
you must have done something very special with those cells.I agree with Gustav; rebuilding your worksheet might solve the problem. But I still think there must be a much simpler solution.
.Merge '<= This line raises the 1004 error
Temporarily replace line 18 with the following codeon error resume next
Dim temp As String
On Error Resume Next
With Selection
temp = .address(, , , True)
.merge
If err <> 0 Then
On Error GoTo 0
MsgBox "please click OK, then save the excel workbook and post it to experts exchange" _
& vbCrLf & "Sheet name = <" & temp & " >" _
& vbCrLf & ActiveCell.address(, , , True)
End
End If
End With
On Error GoTo 0 ' <=== use this if you do not already have an error handler
On Error GoTo your_err_Routine ' <=== change this if you already have an error handler
.When your MS Access hits this temporary code, it will display a message then end all vba activity and leave MS Access open.other alligators to fight.Take your time. I am looking forward to a final solution.
I threw the range in for good measure.I am very glad you included the ORange address, because that was the most import item. It thought I asked for oRange but made a stupid mistake in my code. It should have said this.
on error resume next
Dim temp As String
On Error Resume Next
With oRange ' <== I had previously posted "With Selection" which was not my intention.
temp = .address(, , , True)
.merge
If err <> 0 Then
On Error GoTo 0
MsgBox "please click OK, then save the excel workbook and post it to experts exchange" _
& vbCrLf & "oSheet full address = <" & temp & " >" _
& vbCrLf & ActiveCell.address(, , , True)
End ' <=== do not remove this line
End If
End With
On Error GoTo 0 ' <=== use this if you do not already have an error handler
On Error GoTo your_err_Routine ' <=== change this if you already have an error handler
Sub t()
Const xlMinimized = -4140, xlNormal = -4143, xlMaximized = -4137
Dim wb, oSht, xl
Set xl = CreateObject("Excel.application")
xl.Visible = False ' it doesn't seem to matter what I set this to
Set wb = xl.Workbooks.Add
Set oSht = wb.Worksheets.Add
xl.WindowState = xlMinimized ' doesn't seem to matter what I set this to.hhhhhhhhhh
With oSht.Range("E1", "L1")
.Merge
End With
wb.Close False
xl.Quit
End Sub
Is that code running in an Excel VBA module, or Access.The code could be run from any Excel workbook or any ms Access database. You might need to add the following line at the top. (For simplicity I often create an empty workbook or database for testing things.)
I've got a solution that works, so I'm not interested in pursuing further. I understand completely, so we can close this question completely.
FinalTest: Call TemporaryTest(osht) ' <=== add this line to your current workaround
' then rerun your code. FinalTest should report a problem.
oXL.WindowState = xlMaximized ' < this is the rest of your unchanged code
with oSHT.Range("E1", "L1")
.Merge
etc etc etc etc
' Also add the following subroutine to MS Access, then insert it in anywhere your code
Public Sub TemporaryTest(oSht)
On Error Resume Next
Debug.Print Time & " Windowstate=" & oXl.windowstate
oSht.Range("E1", "L1").merge
If Err <> 0 Then
MsgBox "oSht merge failed"
End If
End Sub
1 YourCurrentCode: Set oSht = oWb.Worksheets.Add ' <== your code might be slightly different
2 FirstTest: Call temporaryTest(osht)
.Merge
.Value2 = "some text"
.Interior.Color = vbYellow
End With
Colors can become surprisingly complicated. For instance you could use .Interior.colorindex = 6
Let me know if you want to get into those weeds.
See also