Hi Billy.
The only reason your are getting away with compiling your example above is because you are late binding. I would assume that is why the compiler is not raising an error. Why the code is working to begin with is beyond me. It should not work the first time.
xlApp.Range(Cells(1, 1), Cells(1, 5)).Merge
Who does "Cells" belong to? It is an unqualified reference. VB is assuming that this must be a Global method but since it does not exist, you are getting the error. A more correct way to code this would be as such:
<table width="100%" border="1" bgcolor="White" style="filter
rogid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New> <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Command1_Click()
<font color="#0000A0">Dim</font> xlApp <font color="#0000A0">As</font> <font color="#0000A0">Object</font>
<font color="#0000A0">Dim</font> xlWb <font color="#0000A0">As</font> <font color="#0000A0">Object</font>
<font color="#0000A0">Dim</font> xlWs <font color="#0000A0">As</font> <font color="#0000A0">Object</font>
<font color="#0000A0">Dim</font> xlRange <font color="#0000A0">As</font> <font color="#0000A0">Object</font>
<font color="#008000">'create an application object and retain a reference</font>
<font color="#0000A0">Set</font> xlApp = CreateObject("Excel.Application")
xlApp.Visible = <font color="#0000A0">True</font>
<font color="#008000"> 'add a workbook and</font>
<font color="#0000A0">Set</font> xlWb = xlApp.Workbooks.Add
<font color="#008000"> 'grab a reference to the first worksheet</font>
<font color="#0000A0">Set</font> xlWs = xlWb.Worksheets(1)
<font color="#008000"> 'reference the range A1 to D1</font>
<font color="#0000A0">Set</font> xlRange = xlWs.Range(xlWs.Cells(1, 1), xlWs.Cells(1, 5))
<font color="#008000"> 'use the reference</font>
<font color="#0000A0">With</font> xlRange
.Merge
.Value = " Billy7978 Educational Student Project but have problem with merged cells"
.Columns.WrapText = <font color="#0000A0">True</font>
.HorizontalAlignment = xlCenter
.Rows(1).RowHeight = 30.57 '
<font color="#0000A0">End</font> <font color="#0000A0">With</font>
<font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
</FONT></td></tr></table><button onclick='document.all("972006215823734").value=document.all("972006215823734").value.replace(/<br \/>\s\s/g,"");document.all("972006215823734").value=document.all("972006215823734").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("972006215823734").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="972006215823734" wrap="virtual">
Private Sub Command1_Click()
Dim xlApp As Object
Dim xlWb As Object
Dim xlWs As Object
Dim xlRange As Object
'create an application object and retain a reference
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
'add a workbook and
Set xlWb = xlApp.Workbooks.Add
'grab a reference to the first worksheet
Set xlWs = xlWb.Worksheets(1)
'reference the range A1 to D1
Set xlRange = xlWs.Range(xlWs.Cells(1, 1), xlWs.Cells(1, 5))
'use the reference
With xlRange
.Merge
.Value = " Billy7978 Educational Student Project but have problem with merged cells"
.Columns.WrapText = True
.HorizontalAlignment = xlCenter
.Rows(1).RowHeight = 30.57 '
End With
End Sub</textarea>