2017-08-08 2 views
0

Ich möchte die Spalte A durchlaufen, beginnend mit Zeile 3, und debug.print die Werte aller bevölkerten Zellen.Excel-Variable in Outlook definieren

Ich kann eine Variable nicht als Bereich definieren. Ich denke, das Problem ist, dass ich Excel VBA in einer Outlook VBA IDE verwende.

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

Hallo, haben Sie einen Verweis auf die Excel-Bibliothek hinzugefügt? (In Tools> Referenzen) Wo erhalten Sie den Fehler beim Debuggen? – maxhob17

+0

Ich denke, ich habe das Problem gelöst. Ich musste rw als Objekt und nicht als Range dimmen. – NAlexP

Antwort

0

Problem war, dass RW als Objekt definiert werden sollte, anstelle von Range.