2017-08-08 23 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。