Loop through columns

G

Geir Holmavatn

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
 
G

Gary Keramidas

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
 
G

Geir Holmavatn

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
 
G

Gary Keramidas

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top