【问题标题】:Export Excel sheet to PDF using Macros and values of another table使用宏和另一个表的值将 Excel 工作表导出为 PDF
【发布时间】:2017-05-12 00:39:31
【问题描述】:

我是 Excel 宏的新手,没有 VB 或任何语言的经验。

我有一张带有价目表的表格,其中我们有一个带有下拉列表的字段,其中包含我们销售员的联系信息(邮件和手机)。

第二张表包含一个包含姓名和联系人信息的表格。

今天,我使用下拉列表选择业务员并导出为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(索引),最好使用 Table1@Name 中的当前数据。

谁能帮我实现这个目标?

感谢和抱歉我的英语不好。

【问题讨论】:

  • 你的数组不应该使用 i=0 来迭代吗?在 arrVenderoes 中,默认情况下将是(0 到 6)而不是(1 到 7)。您可以在编辑器的本地窗口中检查这一点。
  • 将 i=1 和 i 迭代替换为 For i = LBound(arrVendedores) To UBound(arrVendedores) 可能是一个想法,这样如果您更改数组就安全了。然后您可以使用您提到的表格更早地设置数组。
  • 我尝试在 arrVendores 中使用 i+1 但失败了,并且....我应该使用另一个变量,例如 ii = i 然后在 arrVendores 中使用 ii+1
  • 我现在注意到了。

标签: vba excel macros excel-2013


【解决方案1】:

我发现在没有看到一些输入的情况下很难编辑您的代码,但我通过一些虚拟数据让它流动起来。

    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

【讨论】:

  • 谢谢队长我用你的cmets来重建我的宏
【解决方案2】:

谢谢队长,根据你的回答我做了一个新的宏如下

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] 来更改价目表工作表中销售人员之间的唯一字段

【讨论】:

    猜你喜欢
    • 2021-04-22
    • 2017-08-31
    • 1970-01-01
    • 2016-12-09
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    相关资源
    最近更新 更多