[英]Saving and looping using vba in excel
I'm trying to save as a PDF a section of an Excel sheet that is based from the information in another sheet of the workbook. 我正在尝试将Excel工作表的一部分另存为PDF,这是基于工作簿另一工作表中的信息。
The first sheet in which I got to print in a PDF the section A1:I23 is named Enveloppe. 我要以PDF格式打印的第一张纸A1:I23部分名为Enveloppe。 The second sheet in which the data is stored is named Info-Cas.
存储数据的第二张表名为Info-Cas。
Basically, I made a table that I have to export as a PDF and merge every of the 144 PDF together. 基本上,我制作了一个表,必须将其导出为PDF并将144个PDF中的每一个合并在一起。 Of course, I don't want to type everyting one by one so I wrote the following macro.
当然,我不想一一列举,所以我写了下面的宏。 Obviously, it does not work right now.
显然,它现在不起作用。
Dim FName As String
Dim i As Integer
FName = Worksheets("Enveloppe").Range("K1").Text
For i = 2 To 4
'Copier la journée du cas
Worksheets("Info-Cas").Range("K" & i).Copy
Worksheets("Enveloppe").Range("A3").PasteSpecial Paste:=xlPasteValues
'Copier les informations principales (Cas, Numéro d'équipe, Salle)
Worksheets("Info-Cas").Range("A" & i).Copy
Worksheets("Enveloppe").Range("B4").PasteSpecial Paste:=xlPasteValues
Worksheets("Info-Cas").Range("B" & i).Copy
Worksheets("Enveloppe").Range("B5").PasteSpecial Paste:=xlPasteValues
Worksheets("Info-Cas").Range("C" & i).Copy
Worksheets("Enveloppe").Range("B6").PasteSpecial Paste:=xlPasteValues
Worksheets("Info-Cas").Range("D" & i).Copy
Worksheets("Enveloppe").Range("B7").PasteSpecial Paste:=xlPasteValues
Worksheets("Info-Cas").Range("E" & i).Copy
Worksheets("Enveloppe").Range("B8").PasteSpecial Paste:=xlPasteValues
'Copier les informations sur l'horaire
Worksheets("Info-Cas").Range("F" & i).Copy
Worksheets("Enveloppe").Range("B10").PasteSpecial Paste:=xlPasteValues
Worksheets("Info-Cas").Range("G" & i).Copy
Worksheets("Enveloppe").Range("B11").PasteSpecial Paste:=xlPasteValues
Worksheets("Info-Cas").Range("H" & i).Copy
Worksheets("Enveloppe").Range("B12").PasteSpecial Paste:=xlPasteValues
Worksheets("Info-Cas").Range("I" & i).Copy
Worksheets("Enveloppe").Range("B13").PasteSpecial Paste:=xlPasteValues
Worksheets("Info-Cas").Range("J" & i).Copy
Worksheets("Enveloppe").Range("B14").PasteSpecial Paste:=xlPasteValues
'Enregistrer le document au format PDF
Worksheets("Enveloppe").Range("A1:I23").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=FName
Next
The problem I have is that, for the sample I tried, only the last PDF is saving right now. 我的问题是,对于我尝试的示例,现在仅保存了最后一个PDF。
Also, I'd like to save the file in a certain folder on my computer. 另外,我想将文件保存在计算机上的某个文件夹中。 Can anyone help me doing those?
有人可以帮我做那些吗?
Thanks a lot! 非常感谢!
now, you r save the file 3 times. 现在,您将文件保存3次。 first you need to copy the data on a sheet 'Enveloppe'
首先,您需要将数据复制到“信封”工作表中
for i = 2 To 4
next
then save the file: 然后保存文件:
Worksheets("Enveloppe").Range("A1:I23").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=FName
Your copy/paste method is very inefficient. 您的复制/粘贴方法效率很低。
See if this works (untested): 看看是否可行(未试用):
'Your worksheet variables
Dim wsInfoCas As Worksheet, wsEnveloppe As Worksheet
Set wsInfoCas = ThisWorkbook.Worksheets("Info-Cas")
Set wsEnveloppe = ThisWorkbook.Worksheets("Enveloppe")
'Your copy variable
Dim retVal, retArr1(), retArr2()
For i = 2 To 4
FName = wsEnveloppe.Range("K1").Text
With wsInfoCas
retVal = .Range("K" & i).Value
retArr1 = .Range("A" & i, "E" & i).Value
retArr2 = .Range("F" & i, "J" & i).Value
End With
With Application.WorksheetFunction
wsEnveloppe.Range("A3").Value = retVal
wsEnveloppe.Range("B4:B8").Value = .Transpose( _
retArr1)
wsEnveloppe.Range("B10:B14").Value = .Transpose( _
retArr2)
End With
Next
wsEnveloppe.Range("A1:I23").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=FName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.