Consulting

Results 1 to 10 of 10

Thread: Help with VBA project

  1. #1
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    5
    Location

    Help with VBA project

    Hi,

    I need help with VBA project in word.
    Topic: Multiple IF condition to count the number of characters in string and insert a character in between the string.
    Requirements: The multiple IFs should be if the string has three characters then insert X after second character, if the string has four characters then insert X after second character, if the string has five characters then insert X after third character, if the string has six characters then insert X after third character, if the string has seven characters then insert X after fourth character.

    Thank you
    Last edited by sinan km; 02-27-2020 at 01:57 AM.

  2. #2
    You should use Select Case rather than If/Else

    Sub Macro1()
    'Graham Mayor - https://www.gmayor.com - Last updated - 27 Feb 2020
    Dim strText As String
        strText = Selection.Text
        Select Case Len(strText)
            Case Is = 7
                strText = Left(strText, 4) & "X" & Mid(strText, 5)
            Case Is = 6, 5
                strText = Left(strText, 3) & "X" & Mid(strText, 4)
            Case Is = 4, 3
                strText = Left(strText, 2) & "X" & Mid(strText, 3)
            Case Else
        End Select
        Selection.Text = strText
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    If/Then construct


    Option Explicit
    
    
    Sub test()
        Dim s As String, s1 As String
        
        s = "abcdefgh"
        s1 = "*"
        
        If Len(s) = 3 Then
            s = Left(s, 2) & s1 & Right(s, 1)
        ElseIf Len(s) = 4 Then
            s = Left(s, 2) & s1 & Right(s, 2)
    
    
        ElseIf Len(s) = 5 Then
            s = Left(s, 3) & s1 & Right(s, 2)
        ElseIf Len(s) = 6 Then
            s = Left(s, 3) & s1 & Right(s, 3)
    
    
        ElseIf Len(s) = 7 Then
            s = Left(s, 4) & s1 & Right(s, 3)
        Else
            MsgBox "no good"
        End If
    
    
        MsgBox s
    
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    4
    Location
    Sub Macro2
    Dim sl As Integer, i As Integer, Str As String

    Str = "1234567" ' and so on
    sl = Len(Str)
    i = Round(sl / 2, 0)
    Str = Left(Str, i) & "X" & Mid(Str, i + 1)



    MsgBox Str

    End Sub

  5. #5
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    5
    Location
    Hi Paul,
    Appreciate your help. I tried the code in word but it is no executing as anticipated. Kindly cross check and advice.
    Thank you.

  6. #6
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    5
    Location
    Hi Emby,

    I couldn't execute the code in the word document. Kindly check and let me know.

    Thank you

  7. #7
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    5
    Location
    Hi gmayor,

    I executed the code as macro within word document. But the code isn't working as expected. Kindly check and advice. Hoping to hear from you.

    Thank you in advance.

  8. #8
    The code I posted works as you requested. In order for it to work you must have a text string selected of between 3 and 7 characters, or it doesn't do anything. What did you have selected when you ran it?

    The codes provided by my fellow contributors also work, though Paul has used a '*' character for the variable s1 rather than the 'X' you requested. However both those versions have the string hard coded, instead of using a selection. There is more than one way to achieve the requested result. You have had three variations offered. In what way don't they work for you?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    5
    Location
    Hi gmayor,

    Sorry for being confusing, the code worked well with one string. Can you please help me with how to execute the same with a paragraph sir?

    Thank you

  10. #10
    You are going to have to explain what you mean. The code works with a string. If you want to (say) process the first word in a number of selected paragraphs then loop through those paragraphs e.g.

    Sub ProcessParagraphs()
    Dim oPara As Paragraph
    Dim oRng As Range
        For Each oPara In Selection.Range.Paragraphs
            Set oRng = oPara.Range.Words(1)
            SplitString oRng.Text
        Next oPara
    lbl_Exit:
        Set oPara = Nothing
        Set oRng = Nothing
        Exit Sub
    End Sub
    
    
    Sub SplitString(strText As String)
    'Graham Mayor - https://www.gmayor.com - Last updated - 27 Feb 2020
        Select Case Len(strText)
            Case Is = 7
                strText = Left(strText, 4) & "X" & Mid(strText, 5)
            Case Is = 6, 5
                strText = Left(strText, 3) & "X" & Mid(strText, 4)
            Case Is = 4, 3
                strText = Left(strText, 2) & "X" & Mid(strText, 3)
            Case Else
        End Select
        Selection.Text = strText
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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