ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   VBA Loop Help (https://www.excelbanter.com/excel-discussion-misc-queries/175980-vba-loop-help.html)

jlclyde

VBA Loop Help
 
What I am trying to do with this macro is concatenate G1 & B(Whatever
row is selected) & G2. So for instance I woudl like G4 to = G1 & B4 &
G2. Then go to G5 and that woudl be G1 & B5 & G2. I woudl like to do
this through a loop. I am sure the loop part is right, it is just the
stuff int he middle that is messed up.

Thank you,
Jay
Here is my code

Sub Monkey()

Dim G1 As Range
Dim G2 As Range

Set G1 = Range("G1")
Set G2 = Range("G2")

Dim nextRow As Long
nextRow = Selection.Row

Do Until Selection.Offset(0, -5) = ""
Selection = G1 & Range("B" & nextRow) & G2
Selection.Offset(1, 0).Select
Loop

End Sub

Chip Pearson

VBA Loop Help
 
Try something like the following:

Dim S As String
S = Range("G1").Value & _
Cells(ActiveCell.Row, "B").Value & _
Range("G2").Value
Debug.Print S


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2008
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)

"jlclyde" wrote in message
...
What I am trying to do with this macro is concatenate G1 & B(Whatever
row is selected) & G2. So for instance I woudl like G4 to = G1 & B4 &
G2. Then go to G5 and that woudl be G1 & B5 & G2. I woudl like to do
this through a loop. I am sure the loop part is right, it is just the
stuff int he middle that is messed up.

Thank you,
Jay
Here is my code

Sub Monkey()

Dim G1 As Range
Dim G2 As Range

Set G1 = Range("G1")
Set G2 = Range("G2")

Dim nextRow As Long
nextRow = Selection.Row

Do Until Selection.Offset(0, -5) = ""
Selection = G1 & Range("B" & nextRow) & G2
Selection.Offset(1, 0).Select
Loop

End Sub



jlclyde

VBA Loop Help
 
On Feb 7, 3:42*pm, "Chip Pearson" wrote:
Try something like the following:

Dim S As String
S = Range("G1").Value & _
* * Cells(ActiveCell.Row, "B").Value & _
* * Range("G2").Value
Debug.Print S

--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
* * Excel Product Group, 1998 - 2008
Pearson Software Consulting, LLCwww.cpearson.com
(email on web site)

I moved the NextRow into the Loop and then it ran fine. I like how
simple your looks.
Jay



All times are GMT +1. The time now is 06:56 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com