当我第一次运行时,一切都正常;但是当再次运行时,弹出错误框,
"Method 'Range' of object '_Global' failed"
不知道是什么原因?望赐教!以下是源程序:
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.WorksheetSub MergeCell(CellRange As String)
Range(CellRange).Select
Selection.MergeCells = True
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
End SubPrivate Sub Command1_Click()
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
MergeCell "A1:A5"
xlApp.Visible = True
Set xlApp = Nothing
End Sub
"Method 'Range' of object '_Global' failed"
不知道是什么原因?望赐教!以下是源程序:
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.WorksheetSub MergeCell(CellRange As String)
Range(CellRange).Select
Selection.MergeCells = True
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
End SubPrivate Sub Command1_Click()
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
MergeCell "A1:A5"
xlApp.Visible = True
Set xlApp = Nothing
End Sub
No Any Other Problem!
关键在于你拷贝到VB里要用全局变量,如最后一个BlueSky的例子,
或者你直接传递一个XlApp,Xlsheet过去,也不会有问题,
今天我就是被这问题搞得浇头烂耳,呵呵。现在可以了,
未必宏里可运行的拷贝到VB里都可以,还要注意对象变量的传递
OK