2017-03-29 35 views
0

我可以讓我的代碼打開選定的單元格PDF。我如何循環這個從A9 to finalrow運行?這是我的工作,但僅限於選定的單元格。excel循環直通單元打開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

問題出在哪裏? – amg

+0

@amg不確定。它不循環。只打開第一個PDF。我在想它是因爲活動程序改變爲PDF。我能否在某處添加',vbNormalNoFocus'來保持Excel的活動程序。 –

+1

你沒有更新循環內部的'pdfPath'。看到我的回答 – user3598756

回答

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

完美的作品,Thx這麼多。 –