-
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
-
You should use Select Case rather than If/Else
Code:
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
-
If/Then construct
Code:
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
-
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
-
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.
-
Hi Emby,
I couldn't execute the code in the word document. Kindly check and let me know.
Thank you
-
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.
-
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?
-
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
-
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.
Code:
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