September 6, 2019
Hi folks,
Hope everyone is well.
I need macro to add specific words at the end or front of some words, so I checked myonlinetraininghub.com and I found attached macro and I need your help to enhance it please.
I just want to add ability to choose if I want to add in end or front of some words.
When I run the macro, I should change below cell count. so if there ability to run the macro without change below every time.
CoArray = Sheets("Sheet1").Range("A2:A60").Value
PWArray = Sheets("Sheet1").Range("B2:B51").Value
Thanks;
Marsil
October 5, 2010
Hi Marsil,
It's not exactly clear to me what you want to do.
When I run the macro, I should change below cell count. so if there ability to run the macro without change below every time.
What do you mean by 'change below cell count' and 'run the macro without change' ?
If you want to add specific words at the front or end of other words, can you please provide examples of what you want. The file you supplied has a macro that just creates combinations of every word in ColA and Colb.
I just want to add ability to choose if I want to add in end or front of some words
How exactly do you want this to be achieved?
Regards
Phil
VIP
Trusted Members
June 25, 2016
Hi Marsil
My guess is you wanted a dynamic range.
Sub AddPublicWord()
Dim CoArray() As Variant
Dim PWArray() As Variant
Dim r As Long
Dim s As Long
Dim i As Long
Dim lrA As Long
Dim lrB As Long
'Find the last row with data
lrA = Cells(Rows.Count, "A").End(xlUp).Row
lrB = Cells(Rows.Count, "B").End(xlUp).Row
i = 2
CoArray = Sheets("Sheet1").Range("A2:A" & lrA).Value
PWArray = Sheets("Sheet1").Range("B2:B" & lrB).Value
For r = 1 To UBound(CoArray, 1)
For s = 1 To UBound(PWArray, 1)
Cells(i, 5) = CoArray(r, 1) & " " & PWArray(s, 1)
Cells(i, 6) = CoArray(r, 1)
i = i + 1
Next s
Next r
End Sub
As for the adding in front/at the back of words we don't know what is it that you want to achieve.
Always attach your expected result as it will be easier for us to understand what you really have in mind.
Sunny
September 6, 2019
Hi Sunny & Philip,
I just wanted to send my thanks for all your assistance.
So sorry as I didn't explain by request by all details.
Sunny, Yes I want a dynamic range and I updated the macro by above code and it is Ok. thank you.
I just want to add new column to add the words in Front, please check column D to see what I mean.
Thanks;
Marsil
VIP
Trusted Members
June 25, 2016
Hi Marsil
You only need to add this line in red to your code. It just swap the position of the Name and Public Words.
Sub AddPublicWord()
Dim CoArray() As Variant
Dim PWArray() As Variant
Dim r As Long
Dim s As Long
Dim i As Long
Dim lrA As Long
Dim lrB As Long
'Find the last row with data
lrA = Cells(Rows.Count, "A").End(xlUp).Row
lrB = Cells(Rows.Count, "B").End(xlUp).Row
i = 2
CoArray = Sheets("Sheet1").Range("A2:A" & lrA).Value
PWArray = Sheets("Sheet1").Range("B2:B" & lrB).Value
For r = 1 To UBound(CoArray, 1)
For s = 1 To UBound(PWArray, 1)
Cells(i, 4) = PWArray(s, 1) & " " &CoArray(r, 1)
Cells(i, 5) = CoArray(r, 1) & " " & PWArray(s, 1)
Cells(i, 6) = CoArray(r, 1)
i = i + 1
Next s
Next r
End Sub
Good luck
Sunny
Answers Post
1 Guest(s)