Consulting

Results 1 to 7 of 7

Thread: Select Case Statements - Many in a Block Set

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location

    Select Case Statements - Many in a Block Set

    folks good day




    I have got stuck on my select case.

    Well i know there are other ways of doing what im trying to do with arrays and other stuff.

    But i always get stuck on using select case although its meant to be easier

    I wanted to keep my select case statements above in a block

    Then I have my main code

    So it helps me to keep things separate.

    well i set it up but well it doesnt seem to be happening


    this is a basic example.

        
        Sub Select_Case()
        
     
    
        For Each oPara In ActiveDocument.Paragraphs
        
        Set oRng = oPara.Range
    
        oFindText = oPara.Range.Text
        
     
        '-------- All My Select Cases
        Select Case oFindText
       
        Case "@@@"
        oInsertBefore = "Hello"
      
        Case "***"
        oInsertBefore = "Apple"
        
        Case "###"
        oInsertBefore = "Pear"
        
        End Select
        '-----------------------------
        
        
        
        
        
        '--------  Main Code Block
        
        If InStr(1, oRng.Text, oFindText) > 0 Then
        
        oRng.Characters.First.InsertBefore oInsertBefore
        
        
        End If
        Next oPara
     
    
    End Sub
    I m not good at writing functions, and i was trying to avoid arrays

    my Select case should work but im not sure why my idea doesnt work - it doesnt minus some syntaxtical error i suppose.
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  2. #2
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    Are those three bits whole words?

    A paragrapgh that consists soley of one string, "@@@" doesn't sound reasonable
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hello Sam,



    Well theres the first mistake.

    Yes, I was looking for any paragraphs that contain "@@@" then i will insert "Hello" before the paragraph

    I mean thats the idea of it.

    But something has gone wrong in my select cases and Im not sure what else.

    I know how to do basic select cases, but i wanted to have a block set of statements to keep things organised.
    Im a bit old school i need my things laid out easily other wise i usually make a mess as is beknown
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  4. #4
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    The basic problem you have is that oPara.Range includes a paragraph break you've ignored.

    Sub Select_Case()
        For Each oPara In ActiveDocument.Paragraphs
            oInsertBefore = "": Set oRng = oPara.Range
            oFindText = Split(oPara.Range.Text, vbCr)(0)
            Select Case oFindText
            Case "@@@"
                oInsertBefore = "Hello"
            Case "***"
                oInsertBefore = "Apple"
            Case "###"
                oInsertBefore = "Pear"
            End Select
            If Len(oInsertBefore) > 0 Then oRng.InsertBefore oInsertBefore
        Next oPara
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hello Paul,

    thank you for this, yes its the right idea, now i ran it on the sample text below

    =========================

    Lorem Ipsum is simply dummy text of the printing and typesetting industry.

    @@@ Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,

    ### when an unknown printer took a galley of type and

    *** scrambled it to make a type specimen book. It has survived not only five centuries,

    ========================

    but no text was inserted and i tried alot of experimentation im not sure if its me?
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    If you're looking for the first 3 characters use Select Case Left(oFindText, 3)
    Otherwise try
    Sub LoopArray()
        arr1 = Array("@@@", "***", "###")
        arr2 = Array("Hello", "Apple", "Pear")
        For Each oPara In ActiveDocument.Paragraphs
            oInsertBefore = "": Set oRng = oPara.Range
            For i = 0 To 2
            If InStr(1, oPara.Range.Text, arr1(i)) Then oInsertBefore = arr2(i)
            Next i
            If Len(oInsertBefore) > 0 Then oRng.InsertBefore oInsertBefore
        Next oPara
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hello M nice to see you,


    Select Case Left(oFindText, 3)


    it seems to do the trick.

    Well with that and having to split paragraphs it's no wonder the select case got all so confusing when it was meant to be the simpleton.

    But anyway thank you for the additional array.

    The problem with arrays - I always delete something and forget to delete from the other array - and then have two sets of arrays that get very messed up like a soup.

    i thought id better lay things out simply with a select case

    Thanks for the help

    Have a good day M and Paul and everyone.
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


Posting Permissions

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