ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Loop through columns (https://www.excelbanter.com/excel-programming/372153-loop-through-columns.html)

Geir Holmavatn

Loop through columns
 
Hi,

Say I have forenames in column A and family names in column B.

How do I loop through all rows in column B and create
<familyname, <forename in column C?

Thanks for hints on this ;-)

/geir

Gary Keramidas

Loop through columns
 
do you need code?

in c1 = a1 & " " & b1

or

one way in code

Sub test()
Dim cell As Range
For Each cell In Range("a1:a50")
cell.Offset(0, 2) = cell.Value & " " & cell.Offset(0, 1).Value
Next

End Sub
--


Gary


"Geir Holmavatn" wrote in message
...
Hi,

Say I have forenames in column A and family names in column B.

How do I loop through all rows in column B and create
<familyname, <forename in column C?

Thanks for hints on this ;-)

/geir




Geir Holmavatn

Loop through columns
 
Gary Keramidas skrev:

Sub test()
Dim cell As Range
For Each cell In Range("a1:a50")
cell.Offset(0, 2) = cell.Value & " " & cell.Offset(0, 1).Value
Next

End Sub


Great

If I have a column with integers:

12345
2345
345

... and want to split the two last digits into one row and leftpad the
first digits with zeros into another row like this:

12345 0123 45
2345 0023 45
345 0003 45

how can that be done in code..?

/geir

Gary Keramidas

Loop through columns
 
maybe something like this

Sub test()
Dim cell As Range
Dim lastrow As Long
lastrow = Range("a65000").End(xlUp).Row
For Each cell In Range("A1:A" & lastrow)
cell.Offset(0, 1).Value = Left(cell.Value, Len(cell.Value) - 2)

Do While Len(cell.Offset(0, 1).Value) < 4
cell.Offset(0, 1).Value = "'0" & cell.Offset(0, 1).Value
Loop
cell.Offset(0, 2).Value = Right(cell.Value, 2)
Next
End Sub

--


Gary


"Geir Holmavatn" wrote in message
...
Gary Keramidas skrev:

Sub test()
Dim cell As Range
For Each cell In Range("a1:a50")
cell.Offset(0, 2) = cell.Value & " " & cell.Offset(0, 1).Value
Next

End Sub


Great

If I have a column with integers:

12345
2345
345

.. and want to split the two last digits into one row and leftpad the first
digits with zeros into another row like this:

12345 0123 45
2345 0023 45
345 0003 45

how can that be done in code..?

/geir





All times are GMT +1. The time now is 09:42 PM.

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