我是Excel Macros的新手,没有VB或任何语言的经验。
我有一张带有价目表的表格,我们有一个带有下拉列表的字段,其中包含我们销售员的联系信息(邮件和手机)。
第二张表包含一个名为ContactInfo的表。
今天,我使用下拉列表选择销售员并导出到pdf到特定目录。
我希望使用Macro执行这些操作来导出到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中的当前数据。
任何人都可以帮助我实现目标吗?
谢谢,抱歉我的英语不好。
答案 0 :(得分: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
答案 1 :(得分: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]在价目表工作表中更改销售员之间的唯一字段