2017-08-08 67 views
0

我想從第3行開始遍歷A列,並調試所有已填充的單元格的值。在Outlook中定義Excel變量

我無法定義變量作爲範圍。我認爲問題在於我在Outlook VBA IDE中使用Excel VBA。

Sub Test2() 

    Dim xlApp As Object 
    Dim xlWB As Object 
    Dim xlSheet As Object 
    Dim enviro As String 
    Dim strPath As String 

    enviro = CStr(Environ("USERPROFILE")) 

    'the path of the workbook 
    strPath = enviro & "\Documents\test1.xlsx" 

    On Error Resume Next 
    Set xlApp = GetObject(, "Excel.Application") 
    If Err <> 0 Then 
     Application.StatusBar = "Please wait while Excel source is opened ... " 
     Set xlApp = CreateObject("Excel.Application") 
     bXStarted = True 
    End If 
    On Error GoTo 0 

    'Open the workbook to input the data 
    Set xlWB = xlApp.Workbooks.Open(strPath) 
    Set xlSheet = xlWB.Sheets("Sheet1") 
    ' Process the message record 
    Dim rw As Range 
    Dim RowCount As Integer 

    RowCount = 3 

    For Each rw In xlSheet.Rows 
     Debug.Print xlSheet.Cells(rw.Row, 1).Value 
    Exit For 

    RowCount = RowCount + 1 

Next rw 

xlWB.Close 1 
If bXStarted Then 
    xlApp.Quit 
End If 
End Sub 
+4

你好,你有沒有添加對Excel庫的引用? (在「工具」>「參考」中)調試時,哪裏出現錯誤? – maxhob17

+0

我想我解決了這個問題。我必須將rw變暗爲Object,而不是Range。 – NAlexP

回答

0

問題是rw應該定義爲Object而不是Range。