访问 VBa - 遍历 Excel 列

Access VBa - loop through Excel columns

我在从 Access 循环访问 Excel 文件时遇到问题。这是我的代码:

     Sub Xceltest()

        Dim XcelApp As Object
        Dim XcelBook As Excel.Workbook
        Dim x, i
        Set XcelApp = CreateObject("Excel.Application")
        XcelApp.ScreenUpdating = False

Set XcelBook = XcelApp.Workbooks.Open("C:\Users\Lucky\Desktop\Test\Sample.xlsx")

   With XcelBook

i = XcelApp.Rows(1).Find(What:="Število", LookIn:=xlValues, Lookat:=xlWhole).Column

x = XcelApp.Range(XcelApp.Cells(1, i), XcelApp.Cells(XcelApp.Rows.Count, i).End(xlUp)).Value

        For i = 2 To UBound(x)
            If Not IsNumeric(x(i, 1)) Then

            ExcelApp.Quit
            Set ExcelApp = Nothing
            MsgBox "This Excel file is not valid"
        : Exit Sub

            End If

        Next i

        End With

        XcelApp.Quit
        XcelApp = Nothing

        End Sub

无论我做什么,我总是在这一行中遇到错误:

i = XcelApp.Rows(1).Find(What:="Število", LookIn:=xlValues, Lookat:=xlWhole).Column

或这个:

 For i = 2 To UBound(x)

错误是 "object doesn't support this property or method" 或 "object variable or with block variable not set"。我该如何解决这个问题,有人知道吗??

我已经成功运行:

 Sub Xceltest()

        Dim XcelApp As Object

        Dim x, i
        Set XcelApp = CreateObject("Excel.Application")
        XcelApp.ScreenUpdating = False

        XcelApp.Workbooks.Open("C:\Users\Lucky\Desktop\Test\Sample.xlsx")

   With XcelApp

i = XcelApp.Rows(1).Find(What:="Število", LookIn:=xlValues, Lookat:=xlWhole).Column

x = XcelApp.Range(XcelApp.Cells(1, i), XcelApp.Cells(XcelApp.Rows.Count, i).End(xlUp)).Value

        For i = 2 To UBound(x)
            If Not IsNumeric(x(i, 1)) Then

            ExcelApp.Quit
            Set ExcelApp = Nothing
            MsgBox "This Excel file is not valid"
        : Exit Sub

            End If

        Next i

        End With

        XcelApp.Quit
        XcelApp = Nothing

        End Sub

我只需要从代码中删除 ExcelBook,现在它工作正常。