2016-12-27 244 views
0

我是Excel宏中的新成員,並且沒有使用VB或任何語言的經驗。使用宏和其他表格的值將Excel工作表導出爲PDF

我有一個價目表片,在此我們有一個字段與我們的銷售人員的聯繫方式(郵件和手機)的下拉列表。

第二片包含具有名稱和的ContactInfo的表。

今天,我使用下拉菜單選擇的業務員,並出口到PDF到指定的目錄。

我期待用宏做這些事情導出爲PDF格式。我試過一些宏而沒有成功。我想使用名稱@Name保存在目錄中,並使用@ContactInfo替換價目表的特定字段。

我有:

Sub MAKEPDF() 
Dim dvCell As Range 
Dim inputRange As Range 
Dim c As Range 
Dim i As Long 


'Which cell has data validation 
Set dvCell = Sheets("NUEVA LISTA").Range("A3") 
'Determine where validation comes from 
Set inputRange = Evaluate(dvCell.Validation.Formula1) 

arrVendedores = Array("Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7") 

i = 1 
'Begin our loop 
Application.ScreenUpdating = False 
For Each c In inputRange 
    dvCell = c.Value 

     ChDir "D:\Google Drive\Lista de Precios\temp\" & arrVendedores(i) 

     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="(" & Format(Range("A4"), "yyyy-mm-dd") & ") Lista de precios.pdf" 

     'Quality:=xlQualityStandard, _ 
     IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True 

    i = i + 1 
Next c 
Application.ScreenUpdating = True 
End Sub 

這保存爲PDF完美,但如預期導致錯誤9(索引),陣列不工作,爲更好地從表1 @名稱使用當前數據。

任何人都可以幫助我達成目標嗎?

感謝和抱歉我的英語不好。

+0

應該不是你的陣列中使用I = 0來迭代?如arrVenderoes默認情況下(0到6)不是(1到7)。你可以在編輯器的本地窗口中檢查它。 –

+0

這可能是一個想法來代替I = 1,並像對於i = LBOUND(arrVendedores)爲UBound函數(arrVendedores)在我的迭代,如果你改變你是安全的陣列的方式。然後你可以使用你提到的表格更早地設置數組。 –

+0

我試圖用我+ 1裏面arrVendedores但失敗,並且....我應該使用其他變量一樣II =然後我用II + 1裏面arrVendedores – Chiar15

回答

0

我發現很難不看到一些輸入編輯你的代碼,但我得到這個與一些虛擬數據雖然流動。

Sub MAKEPDF() 
    Dim dvCell As Range 
    Dim inputRange As Range 
    Dim c As Range 
    Dim i As Long 

    Application.ScreenUpdating = False 
    'Which cell has data validation 
    Set dvCell = Sheets("NUEVA LISTA").Range("A3") 
'You assigned the value of this cell later on but didn't use it so I removed the value assignment below. 
    'Determine where validation comes from 
    Set inputRange = Evaluate(dvCell.Validation.Formula1) 

    arrVendedores = Array("Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7") 
    'you might want to assign this in code, not sure if it is the sheet names but I assumed it is if not and just filenames to use. 

    For i = LBound(arrVendedores) To UBound(arrVendedores) 
    'this allows an array of any size to be iterated over with having to change the code. 
      ChDir "D:\Google Drive\Lista de Precios\temp\" & arrVendedores(i) 

      Worksheets(arrVendedores(i)).ExportAsFixedFormat Type:=xlTypePDF, Filename:="(" & Format(Range("A4"), "yyyy-mm-dd") & ") Lista de precios.pdf" 
    'you had activeworksheet here but it didn't seem to be changing, so if I assume the array is sheet names this will export those sheet else you will need to change to suit. 
      'Quality:=xlQualityStandard, _ 
      IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True 

    Next 

    Application.ScreenUpdating = True 
    End Sub 
+0

謝謝隊長我用你的意見重建我的宏 – Chiar15

0

謝謝隊長,根據您的回答我犯了一個新的宏如下

Sub MakePDF4() 

Dim myTable As ListObject 
Dim myArray As Variant 
Dim x As Long 
Dim vendedorDatos As Range 
Dim vendedorCampoDatos As Range 



    Set myTable = Sheets("VENDEDORES").ListObjects("Table1") 


    myArray = myTable.DataBodyRange 


    For x = LBound(myArray) To UBound(myArray) 

     Application.ScreenUpdating = False 

     Set vendedorCampoDatos = (Sheets("NUEVA LISTA").Range("A3")) 
     vendedorCampoDatos = myArray(x, 2) 

     'ChDir "D:\Google Drive\Lista de Precios\temp\" & myArray(x, 1) 

     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="D:\Google Drive\Lista de Precios\temp\" & myArray(x, 1) & "\(" & Format(Range("A4"), "yyyy-mm-dd") & ") Lista de precios.pdf" 

       'Quality:=xlQualityStandard, _ 
       IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True 

    Next x 

Application.ScreenUpdating = True 
End Sub 

它正常工作。我在設置ChDir時遇到了一些問題,但在設置文件名時工作。

的所有數據是在一個唯一的文件(可能是我拼寫錯誤或在解釋混淆某些字)。一個工作表是價目表,第二個工作表包含銷售員的[@Name]和[@ContactInfo]表。

我用[@Name]以定在哪個目錄,我將保存的PDF文件和[@ContactInfo]在價格表的工作表獨特的現場銷售人員之間改變