简体   繁体   English

EXCEL VBA:创建一个简单的循环来重复每一行的进程

[英]EXCEL VBA : Creating a simple loop to repeat a process for each row

I know it might seem to be a very simple question but I tried different methods to create a loop that would do what I'm looking for: Basically I have an excel sheet with 4 columns (unknown number of rows) in which I want to enter data. 我知道这似乎是一个非常简单的问题,但我尝试了不同的方法来创建一个可以完成我正在寻找的循环:基本上我有一个包含4列(未知行数)的excel表,我想要输入数据。 This data is then mirrored to a second sheet that contains the "printing design" that I use to create multiple PDF files. 然后将此数据镜像到第二张纸,其中包含用于创建多个PDF文件的“打印设计”。 Problem is: I tried for 4 days now to create a loop and have not achieved anything! 问题是:我现在尝试了4天创建一个循环而没有取得任何成果!

If you could help me, this is the data entry: SCREENSHOT 如果你能帮助我,这是数据条目: SCREENSHOT

Public Sub InputData()

Dim strCap As String
strCap = Sheets("INPUT").Cells(4, 3).Value
Label1.Caption = strCap

Dim strCap2 As String
strCap2 = Sheets("INPUT").Cells(4, 5).Value
Label2.Caption = strCap2

If Sheets("INPUT").Cells(4, 4) = "OE" Then
    Image1.Picture = LoadPicture(ActiveWorkbook.Path & "\OE_Logo.jpg")
Else
    Image1.Picture = LoadPicture(ActiveWorkbook.Path & "\SF_Logo.jpg")
End If

If Sheets("INPUT").Cells(4, 6) = "OE" Then
    Image2.Picture = LoadPicture(ActiveWorkbook.Path & "\OE_Logo.jpg")
Else
    Image2.Picture = LoadPicture(ActiveWorkbook.Path & "\SF_Logo.jpg")
End If

Application.Calculate

Call PrintPDF

End Sub


Sub PrintPDF()
Dim pdfjob As Object
Dim sPDFName As String
Dim sPDFPath As String
 '/// Change the output file name here! ///
sPDFName = "Affidavit" & " " & Sheets("INPUT").Cells(4, 3) & "_" & Sheets    ("INPUT").Cells(4, 5) & ".pdf"
sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
 'Check if worksheet is empty and exit if so
If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub
Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")
With pdfjob
    If .cStart("/NoProcessingAtStartup") = False Then
        MsgBox "Can't initialize PDFCreator.", vbCritical + _
        vbOKOnly, "PrtPDFCreator"
        Exit Sub
    End If
    .cOption("UseAutosave") = 1
    .cOption("UseAutosaveDirectory") = 1
    .cOption("AutosaveDirectory") = sPDFPath
    .cOption("AutosaveFilename") = sPDFName
    .cOption("AutosaveFormat") = 0 ' 0 = PDF
    .cClearCache
End With
 'Print the document to PDF

Sheets("AFFIDAVIT CREATOR").PrintOut Copies:=1, From:=1, To:=1, ActivePrinter:="PDFCreator"
 'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
    DoEvents
Loop
pdfjob.cPrinterStop = False
 'Wait until PDF creator is finished then release the objects
Do Until pdfjob.cCountOfPrintjobs = 0
    DoEvents
Loop
pdfjob.cClose
Set pdfjob = Nothing
End Sub

I actually want to create One SINGLE PDF file for each row, so do this for row 4, 5, 6 etc. till VBA finds an empty row. 我实际上想为每一行创建一个单个PDF文件,所以对第4,5,6等行执行此操作,直到VBA找到一个空行。

Any help would be highly appreciated, thanks in advance for all the help I was able to find on Stackoverflow and hopefully help to come! 任何帮助都将受到高度赞赏,提前感谢我能在Stackoverflow上找到的所有帮助,希望有助于此!

Thanks, 谢谢,

Yannick 雅尼克

In general, a good way to create a loop in VBA involves these steps: 通常,在VBA中创建循环的好方法包括以下步骤:

  1. Define the range of cells over which you want to loop 定义要循环的单元格范围
  2. Assign the range to a variable (declared with Dim myRange as Range ) 将范围分配给变量(使用Dim myRange as Range声明Dim myRange as Range
  3. Loop over the (cells, rows) of the range with a loop like this: 循环遍历范围的(单元格,行),循环如下:
Dim r as Range, myRange as Range
    Set myRange = Range(Sheet("INPUT").cells(4,4), Sheet("INPUT").cells(4,4).end(xlDown))
    For Each r in myRange.Cells
      turnRowIntoPdf r
    Next

This will define myRange to be the range that starts at cell (4,4) - ie D4 - and goes as far down as there are entries. 这将myRange定义为从单元格(4,4)开始的范围 - 即D4 - 并且与条目一样远。 It will then loop over each of these cells in turn (D4, D5, D6, ...) and call a Sub turnRowIntoPdf with parameter r (which will be each of these cells in turn). 然后它将依次循环遍历这些单元格中的每一个(D4,D5,D6,...),并使用参数r (将依次为这些单元格中的每一个)调用Sub turnRowIntoPdf You can then write a sub that takes this parameter as input, and creates the pdf. 然后,您可以编写一个以此参数作为输入的子,并创建pdf。

Think you can manage it from there? 认为你可以从那里管理它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM