2017-03-29 3 views
0

Ich kann meinen Code zum Öffnen der ausgewählten Zellen PDF bekommen. Wie kann ich das ausführen, um von A9 to finalrow zu laufen? Dies ist, was ich arbeite, aber nur für ausgewählte Zelle.Excel-Schleife durch Zellen zu öffnen pdf

Function OpenAnyFile(strPath As String) 
Set objShell = CreateObject("Shell.Application") 
objShell.Open (strPath) 
End Function 

Sub Cutsheets() 
Application.ScreenUpdating = False 
On Error GoTo whoa 

Dim ordersheet As Worksheet 
Dim I As Integer 
Dim finalrow As Integer 
Dim pdfPath As String 
Set ordersheet = Sheet1 

finalrow = Cells(Rows.Count, 1).End(xlUp).Row 
pdfPath = "P:\ESO\1790-ORL\OUC\_Materials\Material Cutsheets\" & ActiveCell & ".pdf" 

'loop through the rows to find PDFs 
For I = 9 To finalrow 
    If Cells(I, 1) = Application.Intersect(ActiveCell, Range("A9:A" & finalrow)) Then 
    Call OpenAnyFile(pdfPath) 
    End If 
    ordersheet.Select 
Next I 

whoa: 
Exit Sub 

Application.ScreenUpdating = True 
End Sub 
+1

Wo liegt das Problem? – amg

+0

@amg Nicht sicher. Es wird nicht wiederholt. Öffnet nur das erste PDF. Ich denke daran, weil das aktive Programm zum PDF wechselt. Könnte ich irgendwo ', vbNormalNoFocus' hinzufügen, um Excel als aktives Programm zu behalten? –

+1

Sie aktualisieren 'pdfPath' nicht innerhalb der Schleife. Siehe meine Antwort – user3598756

Antwort

3
Sub Cutsheets() 
    Application.ScreenUpdating = False 
    On Error GoTo whoa 

    Dim I As Integer 
    Dim finalrow As Integer 
    Dim pdfPath As String 

    'loop through the rows to find PDFs 
    With Sheet1 
     finalrow = .Cells(.Rows.Count, 1).End(xlUp).Row 
     For I = 9 To finalrow 
      pdfPath = "P:\ESO\1790-ORL\OUC\_Materials\Material Cutsheets\" & Cells(I, 1).Value & ".pdf" 
      Call OpenAnyFile(pdfPath) 
     Next I 
    End With 

whoa: 
    Application.ScreenUpdating = True 

End Sub 
+0

Funktioniert perfekt, Thx so viel. –