Home > Net >  VBA code for new line using wildcard * symbol
VBA code for new line using wildcard * symbol

Time:12-03

enter image description here

I'm trying to write a macro that creates a new line when it sees the word "Test" but I have other characters after Test that I want it to keep when it adds a new line. For example, if I have Test 1234 and Test 2345 I want it to display as Test 1234 (new line) QA and Test 2345 (new line) QA. I used the wild card '*' but it doesn't keep the original text so I'm not sure how to keep the original text in the code. Any ideas?

CodePudding user response:

Just test if k is in the cell. No need to use it to add text at the end of the cell's content:

Dim k As String
k = "test"

If InStr(Range("B2").Value, k) > 0 Then
    Range("B2").Value = Range("B2").Value & Chr$(10) & "QA"
End If

CodePudding user response:

.............input.............

Test 1234 Test 2345

.............output.............

Test1234

QA

Test2345

QA

.............macro.............

Sub xyz()
txt = Range("B2")
Range("B2").Clear
For i = 0 To UBound(Split(txt, " "))
    Range("B2") = Range("B2") & Split(txt, " ")(i)
    ins = i   1
    If ins Mod 2 = 0 Then Range("B2") = Range("B2") & Chr(10) & "QA" & Chr(10)
Next i
End Sub
  • Related