Macro run until empty (variable cell)

acurle

New Member
Joined
Feb 26, 2025
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,

Hoping someone could help?
I'm just getting back into VBA and although I've managed to get quite far, I'm drawing a mind blank getting the end at variable table length.
I have a table (called "Mass_Doc_Run") where the length can be variable Sheets("Mass Doc Run"), columns A, B and C have data which is copy pasted into another sheet. There is then a corresponding IF statement depending upon the answer given within a cell to create a PDF and save down.
Can someone help with repeating the above until the table "Mass_Doc_Run" ends? This is variable length which is the bit which is causing me a problem.

Hope this makes sense - first time using a forum!

Thanks,
Alex

VBA Code:
Sub TEST()

    Sheets("Mass Doc Run").Select
    Range("A2").Select
    Selection.Copy
   
    Sheets("Template Control").Select
    Range("B6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
       
    Sheets("Mass Doc Run").Select
    Range("B2").Select
    Selection.Copy
   
    Sheets("Template Control").Select
    Range("B8").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
   
    Sheets("Mass Doc Run").Select
    Range("C2").Select
    Selection.Copy
   
    Sheets("Template Control").Select
    Range("B9").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
   
    If Sheets("Template Control").Range("B9").Value = "POC" Then
   
        Sheets("Proof of Compliance (POC)").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        Sheets("Template Control").Range("B1").Text, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=False
       
    ElseIf Sheets("Template Control").Range("B9").Value = "POS" Then
   
        Sheets("Proof of Sustainability (POS)").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        Sheets("Template Control").Range("B1").Text, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=False
       
    End If
   
       
    
End Sub
 
Hi Alex

I hope this is what you meant.
VBA Code:
Sub TEST()
    Dim lastRow As Long, i As Long
    lastRow = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To lastRow
        Sheets("Template Control").Range("B6").Value = Sheets("Mass Doc Run").Range("A" & i).Value2
        Sheets("Template Control").Range("B8").Value = Sheets("Mass Doc Run").Range("B" & i).Value2
        Sheets("Template Control").Range("B9").Value = Sheets("Mass Doc Run").Range("C" & i).Value2
        
        If Sheets("Template Control").Range("B9").Value = "POC" Then
            Sheets("Proof of Compliance (POC)").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            Sheets("Template Control").Range("B1").Text, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, OpenAfterPublish:=False
        End If
    Next i
End Sub
 
Upvote 0
Morning,
Sadly its not worked. Its now just copying the last data row and producing that as a pdf.
Do you know if there is a way I can add something to the top and bottom which would rerun the VBA until it reaches a blank row?
Thanks,
Alex
 
Upvote 0
Your original copied (for row 2) A2 to B6, B2 to B8 and C2 to B9, then checked B9 and sent to PDF if it equaled POC.
The code I provided repeats that process for every row down to the bottom of the data, so it would repeat for row 3, row 4 etc until it encounters a blank row.
If that’s not what was required then which cells should the data in rows 3 to the last non-blank row go before it does the PDF check?
 
Upvote 0
Not sure how to elaborate on the process so maybe writing the process steps I'm needing will be best:
1) Copy from "Mass Doc Run" A2 and paste into "Template Control" B6 (this is fixed)
2) Copy from "Mass Doc Run" B2 and paste into "Template Control" B8 (this is fixed)
3) Copy from "Mass Doc Run" C2 and paste into "Template Control" B9 (this is fixed)
4) Copy from "Mass Doc Run" D2 and paste into "Template Control" B7 (this is fixed)7
5) IF "Template Control B9 = "POC" then run POC PDF runner, IF "Template Control B9 = "POS" then run POS PDF runner
6) repeat the above until "Mass Doc Run" column A hits a blank

The issue with the code suggested was that it seemed to only produce one PDF document rather than all of them

Hope that helps? Thanks again, Alex
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top