+ Reply to Thread
Results 1 to 8 of 8

String search + page break

Hybrid View

  1. #1
    Registered User
    Join Date
    01-15-2009
    Location
    North Carolina
    MS-Off Ver
    Excel 2003
    Posts
    24

    Question String search + page break

    I need help creating a macro that will:

    1) start from cell A1 and search an entire worksheet (column A) for a certain string.

    2) the string being search for will be something like "Project:" with a project # following, but I only want to look for the "Project:"

    3) insert a page break before that row

    4) continue until the end of the worksheet

    PLEASE POST ANY QUESTIONS THAT MAY HELP YOU HELP ME!!!

    Thanks all!

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: String search + page break macro - HELP!

    Try this:
    Sub x()
        Dim wks         As Worksheet
        Const sWhat     As String = "Project"
        Dim cell        As Range
        Dim sAdr        As String
    
        Set wks = ActiveSheet
    
        With wks
            .ResetAllPageBreaks
            With .Columns(1)
                Set cell = .Find(What:=sWhat, After:=.Cells(.Cells.Count), _
                                 LookIn:=xlValues, LookAt:=xlPart, _
                                 SearchDirection:=xlNext, SearchFormat:=False, _
                                 MatchCase:=False, MatchByte:=False)
                If Not cell Is Nothing Then
                    sAdr = cell.Address
                    Do
                        If cell.row > 1 Then wks.HPageBreaks.Add Before:=cell
                        Set cell = .FindNext(cell)
                    Loop Until cell.Address = sAdr
                End If
            End With
        End With
    End Sub
    Last edited by shg; 10-08-2009 at 08:40 AM. Reason: thank you StephenR
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    01-15-2009
    Location
    North Carolina
    MS-Off Ver
    Excel 2003
    Posts
    24

    Question Re: String search + page break

    Thanks for the code!

    1 problem though:

    I get a run-time error '1004': application-defined or object-defined error on this line of code.

    wks.HPageBreaks.Add Before:=cell

    Any ideas???

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: String search + page break

    Do you have "project" in A1? If so, you can't add a page break before row 1. EDIT: should say, needless to say, the code works for me otherwise.
    Last edited by StephenR; 10-08-2009 at 08:45 AM.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: String search + page break

    Dunno if that's the problem, but good catch, Stephen, thank you.

    Prior post corrected.

  6. #6
    Registered User
    Join Date
    01-15-2009
    Location
    North Carolina
    MS-Off Ver
    Excel 2003
    Posts
    24

    Question Re: String search + page break

    That worked great. Thanks all!

    One more thing:

    Would it be easier to include this into the same macro or create a seperate one:

    1) When it finds "Project" -- in column C of the same row --
    =right(cell w/ "project", 9)

    2) Copy and paste special that cell (I need it in number format to vlookup against it)

    3) In column D of the same row -- =vlookup(# from column C, Sheet1!A:B, 2, FALSE)

    Please let me know if you need any further information! THANKS!

  7. #7
    Registered User
    Join Date
    01-15-2009
    Location
    North Carolina
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: String search + page break

    bump bump bump

  8. #8
    Registered User
    Join Date
    01-15-2009
    Location
    North Carolina
    MS-Off Ver
    Excel 2003
    Posts
    24

    Question Re: String search + page break + vlookup (new)

    What would be the best way to include the following into the code that shg already provided to me?

    It would probably be best to imbed it into the same loop already present in the macro, correct? But, I've tried a few ways and cannot get it to work... HELP!

    1) When it finds "Project" -- in column C of the same row --
    =right(cell w/ "project", 9)

    2) Copy and paste special that cell (I need it in number format to vlookup against it)

    3) In column D of the same row -- =vlookup(# from column C, Sheet1!A:B, 2, FALSE)

    Please let me know if you need any further information! THANKS AGAIN ALL!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1