Consulting

Results 1 to 12 of 12

Thread: Solved: find whole line based on some text?

  1. #1
    VBAX Contributor samohtwerdna's Avatar
    Joined
    Dec 2004
    Location
    Denver Colorado
    Posts
    143
    Location

    Solved: find whole line based on some text?

    Hello all,

    I am trying to create a macro to clean up some documents that have way too much info. I need to write a function that will loop through my entire doc looking for lines with certain text

    Example: I may have a line that looks like this
    a. hx,Oj Hebrew Pp+xxnbfhkd (particle preposition)

    If I do a find with wildcards for this text (Hebrew P* * ^13) I will find everything after word Hebrew, But I want to select (then delete) the whole line.

    Also I would like to put the search parameters in an array because I will end up having more than ten different searches I need to do.

    Maybe something like:
    [VBA]
    Do
    If InStr(1, Selection.Text, "Hebrew P*") Then
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend

    Selection.Delete

    Loop
    [/VBA]

    Any Help?

    Thanks
    To live is Christ... To code is cool!

  2. #2
    VBAX Contributor samohtwerdna's Avatar
    Joined
    Dec 2004
    Location
    Denver Colorado
    Posts
    143
    Location
    OK, the Do while was not good. Instead I went with:
    [VBA]
    Dim oRng As Word.Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
    .Text = "Hebrew P"
    While .Execute
    oRng.Paragraphs(1).Range.Delete
    Wend
    End With

    With oRng.Find
    .Text = "Hebrew p"
    While .Execute
    oRng.Paragraphs(1).Range.Delete
    Wend
    End With

    With oRng.Find
    .Text = "Hebrew n"
    While .Execute
    oRng.Paragraphs(1).Range.Delete
    Wend
    End With
    [/VBA]

    This works well enough but I hate repeating the code. Any Ideas how to get an array as the search text?
    To live is Christ... To code is cool!

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Put your text strings into an array and use it.[vba]
    Option Explicit

    Sub Yadda()
    Dim oRng As Word.Range
    Dim Search()
    Dim var

    Search = Array("Hebrew P", _
    "Hebrew p", "Hebrew n")

    For var = 0 To UBound(Search)
    Set oRng = ActiveDocument.Range
    With oRng.Find
    .Text = Search(var) ' each item in the array
    While .Execute
    oRng.Paragraphs(1).Range.Delete
    Wend
    End With
    Next
    End Sub
    [/vba]

  4. #4
    VBAX Contributor samohtwerdna's Avatar
    Joined
    Dec 2004
    Location
    Denver Colorado
    Posts
    143
    Location
    Thanks fumei!
    To live is Christ... To code is cool!

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    You are just doing this one-time (each item)?

    Also it is important to understand why the range is re-set for each loop:[vba]
    For var = 0 To UBound(Search)
    Set oRng = ActiveDocument.Range
    [/vba]It may not be needed, but generally it would be. I am not totally clear on what you are doing.

    If this has indeed solved your question, please mark the thread as Solved, from the Thread Tools tabs at the top.

  6. #6
    VBAX Contributor samohtwerdna's Avatar
    Joined
    Dec 2004
    Location
    Denver Colorado
    Posts
    143
    Location
    Fumei,

    Thanks for the help, I am not quite finished with the setup. I want to put the search ability in a UserForm and have an input text field for the end user to specify what to delete just by placing the first letter in the input and comma separating for more than one entry.
    So I did this:
    [VBA]Private Sub cmdRun_Click()
    Dim oRng As Word.Range
    Dim Search()
    Dim var


    Search = Array(txtExclude.Value)

    For var = 0 To UBound(Search)
    Set oRng = ActiveDocument.Range
    With oRng.Find
    .Text = "Hebrew " + Search(var) ' each item in the array
    While .Execute
    oRng.Paragraphs(1).Range.Delete
    Wend
    End With
    Next
    End Sub[/VBA]

    however, that does not work for separating the values?
    To live is Christ... To code is cool!

  7. #7
    VBAX Contributor samohtwerdna's Avatar
    Joined
    Dec 2004
    Location
    Denver Colorado
    Posts
    143
    Location
    I tried to use the split function but I keep getting a type mismatch??

    [VBA]
    Search = Array(Split(txtExclude.Value, ","))[/VBA]

    am I using the split wrong??
    To live is Christ... To code is cool!

  8. #8
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    The Split function returns an array, so all you need to code is:

    [VBA]Search = Split(txtExclude.Value, ",")[/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  9. #9
    VBAX Contributor samohtwerdna's Avatar
    Joined
    Dec 2004
    Location
    Denver Colorado
    Posts
    143
    Location
    thanks Tony,

    That actually did not work either because of the Dim Search()
    but I removed that and it seems to work
    To live is Christ... To code is cool!

  10. #10
    VBAX Contributor samohtwerdna's Avatar
    Joined
    Dec 2004
    Location
    Denver Colorado
    Posts
    143
    Location
    OK - So if I use
    [VBA]Option Explicit[/VBA]

    I must Dim my variables but Dim Search() does not work neither does Dim Search As String

    I get a type miss-match on
    [VBA]Search = Split(txtExclude.Value, ",") [/VBA]

    But If I remove Option Explicit and any Dim for Search then it works just fine. That must mean Word is guessing what to use and I am declaring it incorrectly.

    Any Ideas??

    thanks,
    To live is Christ... To code is cool!

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    1. Use Option Explicit.

    2. "Dim Search() does not work" Oh yes it does. But it work better if you:[vba]
    Dim Search
    Dim strIn As String
    strIn = "one,two,three"
    Search = Split(strIn, ",")
    MsgBox Search(1)
    [/vba]Notice Search is declared without the parenthesis. Thus, it is declared as a Variant. NOT an array. The Split instruction makes it an array.
    [vba]
    Dim Search()
    Dim strIn As String
    strIn = "one,two,three"
    Search = Split(strIn, ",")
    [/vba]Declaring Search() - WITH the parenthesis - will indeed return a error 13, the dreaded type-mismatch.



    Use Option Explicit.

  12. #12
    VBAX Contributor samohtwerdna's Avatar
    Joined
    Dec 2004
    Location
    Denver Colorado
    Posts
    143
    Location
    thanks fumei!

    That works well. And thank for the explanation of the variant - very helpful
    To live is Christ... To code is cool!

Posting Permissions

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