Ms access 范围(“A1”)错误。特殊单元格(xlLastCell))

Ms access 范围(“A1”)错误。特殊单元格(xlLastCell)),ms-access,vba,Ms Access,Vba,在我尝试运行以下代码的50%的时间里,我得到了对象的“Method'Range'\u Global'failed”错误。调试将我带到这一行: Set rng = xlWS.Range(Range("A1"), xlWS.Range("A1").SpecialCells(xlLastCell)) 有人能帮我解决这个问题吗??谢谢 Private Sub Command48_Click() 'On Error Resume Next Dim Filename As String Dim month

在我尝试运行以下代码的50%的时间里,我得到了对象的“Method'Range'\u Global'failed”错误。调试将我带到这一行:

Set rng = xlWS.Range(Range("A1"), xlWS.Range("A1").SpecialCells(xlLastCell))
有人能帮我解决这个问题吗??谢谢

Private Sub Command48_Click()
'On Error Resume Next
Dim Filename As String
Dim month1 As String
Dim year1 As Integer

Dim startTime As Date
startTime = Now

Dim strDirectoryPath As String
Filename = strDirectoryPath & "\" & "QI_GAP_REPORT_2_ " & Format$(Now(), "mm-dd-yyyy") & ".xls"
DoCmd.OpenQuery "QI_GAP_REPORT_FOR_EXCEL"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "QI_GAP_REPORT_FOR_EXCEL", Filename, False, "Summary"
DoCmd.Close acQuery, "QI_GAP_REPORT_FOR_EXCEL"

'///****Format excel workbook****////
' Late binding to avoid reference:
Dim xlApp As Object 'Excel.Application
Dim xlWB As Object 'Workbook
Dim xlWS As Object 'Worksheet
Dim GetBook As String



' Create the instance of Excel that we will use to open the temp book
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWB = xlApp.Workbooks.Open(Filename)
Set xlWS = xlWB.Worksheets("Summary")


' Format our temp sheet
' ************************************************** *************************

xlApp.Range("A1").Select

Const xlLandscape As Long = 2
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
Const xlContext As Integer = -5002
Const xlDown As Integer = -4121
Const xlContinuous As Integer = 1
Const xlThin As Integer = 2
Const xlLastCell As Long = 11
Const xlYes As Long = 1


With xlWS
With .UsedRange
.borders.LineStyle = xlContinuous
.borders.ColorIndex = 0
.borders.TintAndShade = 0
.borders.Weight = xlThin
End With

'format header 90 degree
With .Range("i1:y1")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 90
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
.UsedRange.Rows.RowHeight = 15
.UsedRange.Columns.AutoFit

Dim tbl As ListObject
Dim rng As Range

Set rng = xlWS.Range(Range("A1"), xlWS.Range("A1").SpecialCells(xlLastCell))
Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, rng, , xlYes)
tbl.TableStyle = "TableStyleMedium2"
tbl.ShowTotals = True

如果您在
Range
的每次出现之前都加上
xlWS
这样的前缀,会发生什么情况
Set rng=xlWS.Range(xlWS.Range(“A1”)、xlWS.Range(“A1”).SpecialCells(xlLastCell))
如果在
Range
的每次出现之前都加上
xlWS
这样的前缀,会发生什么<代码>设置rng=xlWS.Range(xlWS.Range(“A1”)、xlWS.Range(“A1”).SpecialCells(xlLastCell))