Hi everyone,
I was just introduced to this forum and have read a lot of wonderful solutions regarding my VBA issues.
I'm stuck on this particular problem and can't seem to make it work in excel. I'm a borderline n00b so please bare with me.
I managed to find this vba script from somewhere else, however I didn't write it. When it runs, it adds a 'TEXT' to the end of every line in every cell of a given column.
This script works fine and does the job. However what I need to achieve is for there to be an if statement saying that: out of all the lines that the 'TEXT' is being added to, if any line has less than say 5 words OR ends with a colon, add a 'TEXT TEXT' instead of just 'TEXT' to the end of that line. So if a cell contains:
It should be edited to have this text:
So basically put, if a line in a cell meets either of the two conditions (less than 5 words or ends in a colon), "TEXT TEXT" is concatenated to the end of the line. If the line meets none of those two conditions, "TEXT" is concatenated to the end, which is what the script above already does.
So far, I've tried adding If statements within that code saying
and have tried tweaking the syntax but I usually keep getting syntax and compiling errors which was very frustrating and demotivating since I couldn't get anywhere.
Any sort of help is really appreciated!
Thank you
I was just introduced to this forum and have read a lot of wonderful solutions regarding my VBA issues.
I'm stuck on this particular problem and can't seem to make it work in excel. I'm a borderline n00b so please bare with me.
I managed to find this vba script from somewhere else, however I didn't write it. When it runs, it adds a 'TEXT' to the end of every line in every cell of a given column.
Code:
Sub AddText()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets(1)
Dim myCell As Variant, myRange As Range, myArray() As String
Dim i As Integer
Set myRange = ws.Range("A1", ws.Cells(ws.Rows.Count, "A").End(xlUp))
For Each myCell In myRange
myArray= Split(myCell, Chr(10))
myCell.Value = ""
For i = 0 To UBound(myArray)
myArray(i) = myArray(i) & " TEXT"
If i = UBound(myArray) Then
myCell.Value = myCell.Value & myArray(i)
Else: myCell.Value = myCell.Value & myArray(i) & Chr(10)
End If
Next i
Next myCell
End Sub
This script works fine and does the job. However what I need to achieve is for there to be an if statement saying that: out of all the lines that the 'TEXT' is being added to, if any line has less than say 5 words OR ends with a colon, add a 'TEXT TEXT' instead of just 'TEXT' to the end of that line. So if a cell contains:
Code:
One two three four five six seven eight nine ten
One two three four five six seven eight:
One two three four five six
One two
It should be edited to have this text:
Code:
One two three four five six seven eight nine ten TEXT
One two three four five six seven eight: TEXT TEXT
One two three four five six TEXT
One two TEXT TEXT
So basically put, if a line in a cell meets either of the two conditions (less than 5 words or ends in a colon), "TEXT TEXT" is concatenated to the end of the line. If the line meets none of those two conditions, "TEXT" is concatenated to the end, which is what the script above already does.
So far, I've tried adding If statements within that code saying
Code:
If myCell.len < 5 Then
Code:
If myCell.value < 5 Then
Any sort of help is really appreciated!
Thank you