Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Simple loop?

Hi,

Say column B in an Excel sheet contains ForeNames and column C contains
FamilyNames, how would I merge these two together in an inserted Column
D as <forename<space<familyname?

Also another column contains numeric values with 3 or 4 digits. I need
to pad these numbers with a leading zeros making 234 to 0234 and also
convert this field to a string field to make sure that excel doesn't
strip this zero off again. How would I do this..?

Thanks a lot if someone has a couple of code snippets which can give me
a point of departure with routines like this in the future

regards

tor

  #2   Report Post  
Posted to microsoft.public.excel.programming
Ian Ian is offline
external usenet poster
 
Posts: 238
Default Simple loop?

The first bit is easy enough without code. In D1 type =B1&" "&C1

To change the numbers to a string, select the column, go Format|Cells|Number
and select Category as Text.

To pad the numbers use this code

For r = 1 To 10 ' change numbers to suit row range
If Len(Cells(r, 3)) < 4 Then ' check length of entry in current row column 3
(C)
Cells(r, 3).Value = String(4 - Len(Cells(r, 3)), "0") & Cells(r, 3).Value '
pads out string with leading zeros
End If
Next

The If line is only there in case you have a cell with more than 4 digits.
--
Ian
--
"bushtor" wrote in message
oups.com...
Hi,

Say column B in an Excel sheet contains ForeNames and column C contains
FamilyNames, how would I merge these two together in an inserted Column
D as <forename<space<familyname?

Also another column contains numeric values with 3 or 4 digits. I need
to pad these numbers with a leading zeros making 234 to 0234 and also
convert this field to a string field to make sure that excel doesn't
strip this zero off again. How would I do this..?

Thanks a lot if someone has a couple of code snippets which can give me
a point of departure with routines like this in the future

regards

tor



  #3   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default Simple loop?

Hi bushtor,

Say column B in an Excel sheet contains ForeNames and column C contains
FamilyNames, how would I merge these two together in an inserted Column
D as <forename<space<familyname?


Try this:

Sub test()
With ActiveSheet
Set rng = .Range(.Cells(2, "B"), _
.Cells(.Rows.Count, "B").End(xlUp))
End With
For Each c In rng
c.Offset(, 2) = Trim(c & " " & c.Offset(, 1))
Next c
End Sub


Also another column contains numeric values with 3 or 4 digits. I need
to pad these numbers with a leading zeros making 234 to 0234 and also
convert this field to a string field to make sure that excel doesn't
strip this zero off again. How would I do this..?


Sub test2()
With ActiveSheet
Set rng = .Range(.Cells(2, "B"), _
.Cells(.Rows.Count, "B").End(xlUp))
End With
For Each c In rng
c.NumberFormat = "@"
c = Format(c, "0000")
Next c
End Sub

Regards,
KL


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 510
Default Simple loop?

Hi

To convert numbers to text strings with leading 0's, you can use an
worksheet formula like
=TEXT(A1,"0000")

An equivalent in VBA is
TextValue = Format(NumericValue,"0000")


--
Arvi Laanemets
( My real mail address: arvil<attarkon.ee )


"bushtor" wrote in message
oups.com...
Hi,

Say column B in an Excel sheet contains ForeNames and column C contains
FamilyNames, how would I merge these two together in an inserted Column
D as <forename<space<familyname?

Also another column contains numeric values with 3 or 4 digits. I need
to pad these numbers with a leading zeros making 234 to 0234 and also
convert this field to a string field to make sure that excel doesn't
strip this zero off again. How would I do this..?

Thanks a lot if someone has a couple of code snippets which can give me
a point of departure with routines like this in the future

regards

tor



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
If-Else Simple Loop function Junior728 Excel Programming 3 August 1st 05 09:27 AM
Simple question on For...Next loop Alex Excel Programming 1 July 26th 05 03:30 PM
Some help w/ simple loop, please? terry b Excel Programming 6 February 6th 05 06:17 PM
simple loop macro jim27[_3_] Excel Programming 0 October 17th 04 06:40 PM
simple loop macro jim27 Excel Programming 2 October 17th 04 06:28 PM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"