1

I have codes written (as below) that find the word Total in Column B. It then exports the selection to PDF. The word Total is then replaced by Done.

I am trying to find a way to repeat this code until there is no more Total in Column B.

Columns("B:B").Select
Selection.Find(What:="Total", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Offset(-1, -1).Activate

    ActiveSheet.Range(Selection, Selection.End(xlUp)).Select
    Selection.Resize(, 15).Select
    Selection.Offset(, 1).Select


    Dim rng As Range
    With ActiveSheet
    Set rng = Selection
    .PageSetup.PrintArea = rng.Address
    .PageSetup.Orientation = xlLandscape
    .PageSetup.FitToPagesWide = 1
    .PageSetup.FitToPagesTall = 999
    .PageSetup.PrintTitleRows = "$1:$4"
    .PageSetup.LeftMargin = Application.InchesToPoints(0.45)
    .PageSetup.RightMargin = Application.InchesToPoints(0.2)
    .PageSetup.TopMargin = Application.InchesToPoints(0.25)
    .PageSetup.BottomMargin = Application.InchesToPoints(0.25)
    .PageSetup.HeaderMargin = Application.InchesToPoints(0.3)
    .PageSetup.FooterMargin = Application.InchesToPoints(0.3)
    .PageSetup.PaperSize = xlPaperA4
    .PageSetup.CenterHorizontally = True
    .PageSetup.CenterVertically = False


    Selection.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:="C:Users\kgs-aizat.kassim\Desktop\" & ActiveCell.Offset(0, -1).Value & ".pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True

    End With

Columns("B:B").Select

Selection.Find(What:="Total", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate

    ActiveCell.Value = "Done"

 End Sub

3 Answers 3

1

Here's some code that will search column B for all entries of SearchItem. You need to include an invocation to your PDF processing within this.

By the way, if you changing the cell contents to 'Done' as a means to see if there are no more cells to process, you don't need to do that. If you comment out the line:

rPtr.Value = ReplaceItem

the code will still find the cells only once.

Option Explicit

Sub test()

Dim rData As Range
Set rData = Sheets(1).Range("B:B")
Call ReplaceContents("Test", "Test1", rData)

End Sub

Public Sub ReplaceContents(ByVal SearchItem As String, ByVal ReplaceItem As String, ByVal DataArea As Range)

Dim rPtr As Range
Dim sFirstCell As String
Dim bFinished As Boolean

Set rPtr = DataArea.Find(SearchItem, DataArea(DataArea.Count), XlFindLookIn.xlValues)
If Not rPtr Is Nothing Then
    sFirstCell = rPtr.Address
    Do While bFinished = False
        rPtr.Value = ReplaceItem
        Set rPtr = DataArea.FindNext(rPtr)
        If StrComp(rPtr.Address, sFirstCell, vbTextCompare) = 0 Then bFinished = True
    Loop
End If

End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I really appreciate your help. It is now working great except for one tiny issue. When all TOTALs have been replaced with DONE, the final search for the word TOTAL will come out as an error. How do I get rid of that error message?
I dont' think youll need it because all the found cells have been replaced. Can you just delete it? If you need that extra bit at the end for whatever reason, I believe you will get an exception here because the call doesn't return a range object, it returns nothing - since it cant find any TOTAL data. You need to set a reference to the call and check for Nothing, like I did in my code... Set rPtr = DataArea.Find(SearchItem, DataArea(DataArea.Count), XlFindLookIn.xlValues) If Not rPtr Is Nothing Then......
0

Have a look at http://www.excel-easy.com/vba/loop.html

What you need to do is follow the link above. And then you will to get the total amount of rows used in column "B" and use that as your end to your for loop.

So basically it will be something like

For i = 2 to columnBCount
    do code.......
next

You will just need to replace columnBCount with an actual way to get the count.

I have set i to be 2 As if you have titles this will not include them and start at the second row.

But read up on the loops from the link

Comments

0

I see you're using the 'find' command you can use 'findnext'

Dim rng As Range
With ActiveSheet
    set c = .Columns("B:B").Find(What:="Total", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Offset(-1, -1)

    if c is not nothing then
        firstaddress  = c.address
     do
        c.select
    .Range(Selection, Selection.End(xlUp)).Select
    Selection.Resize(, 15).Select
    Selection.Offset(, 1).Select

    Set rng = Selection
    .PageSetup.PrintArea = rng.Address
    .PageSetup.Orientation = xlLandscape
    .PageSetup.FitToPagesWide = 1
    .PageSetup.FitToPagesTall = 999
    .PageSetup.PrintTitleRows = "$1:$4"
    .PageSetup.LeftMargin = Application.InchesToPoints(0.45)
    .PageSetup.RightMargin = Application.InchesToPoints(0.2)
    .PageSetup.TopMargin = Application.InchesToPoints(0.25)
    .PageSetup.BottomMargin = Application.InchesToPoints(0.25)
    .PageSetup.HeaderMargin = Application.InchesToPoints(0.3)
    .PageSetup.FooterMargin = Application.InchesToPoints(0.3)
    .PageSetup.PaperSize = xlPaperA4
    .PageSetup.CenterHorizontally = True
    .PageSetup.CenterVertically = False


    Selection.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:="C:Users\kgs-aizat.kassim\Desktop\" & ActiveCell.Offset(0, -1).Value & ".pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True

    loop While Not c Is Nothing And c.Address <> firstAddress 
End if 
end with
 End Sub

This will loop through all cells that match your criteria

1 Comment

This looks like a much simplified version compared to what I have now but set c resulted to an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.