Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Loop through range and add each cell contents to a string

Hi there,

Say I have a range A1:A100. How, when clicking a command button, do I
get excel to loop through the range adding the contents into a string
with a colon between each entry?

e.g.

A
1
2

3

4


Becomes ;pete@pete .com"


Many thanks,

David

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Loop through range and add each cell contents to a string

dim myRange as range, r as range
dim aWS as worksheet
Dim myString as string

set aWS = activesheet

myString = ""
set myRange = aWS.range("A1:A100")
for each r in myrange
if not isempty(r) then
mystring = mystring & ";" & r.value
end if
next r






next r

"Daveo" wrote:

Hi there,

Say I have a range A1:A100. How, when clicking a command button, do I
get excel to loop through the range adding the contents into a string
with a colon between each entry?

e.g.

A
1
2

3

4


Becomes ;pete@pete .com"


Many thanks,

David


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 698
Default Loop through range and add each cell contents to a string

Maybe something like this:

Paste this code into a General Module
Note: change the "MySheet" reference to the name of your worksheet.

'-------Start of Code---------------
Sub BuildEmailString()
Dim rCell As Range
Dim strText As String

If MsgBox(Title:="IMPORTANT", _
Prompt:="This program inserts the email list in the active cell." _
& vbCr & vbCr & "OK to continue?", _
Buttons:=vbQuestion + vbYesNo) _
= vbNo Then
Exit Sub
End If

strText = ""

For Each rCell In Worksheets("MySheet").Range("A1:A100").Cells
If InStr(1, rCell.Text, "@") 0 Then
strText = strText & rCell.Text & ";"
End If
Next rCell
ActiveCell.Value = Left(strText, Len(strText) - 1)
End Sub
'-------End of Code---------------

Is that something you can work with?
***********
Regards,
Ron

XL2002, WinXP


"Daveo" wrote:

Hi there,

Say I have a range A1:A100. How, when clicking a command button, do I
get excel to loop through the range adding the contents into a string
with a colon between each entry?

e.g.

A
1
2

3

4


Becomes ;pete@pete .com"


Many thanks,

David


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Loop through range and add each cell contents to a string

Thanks folks - got it sorted with your help!

David

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
Macro to clear range contents when cell contents are changed by us Steve E Excel Programming 12 February 22nd 07 09:09 PM
Loop through range of cells, string search, take action [email protected] Excel Programming 1 November 3rd 06 12:56 PM
Display contents of cell in another cell as part of text string? [email protected] New Users to Excel 3 July 8th 06 07:44 PM
IF Cell Contents Start with STRING ConfusedNHouston Excel Worksheet Functions 2 April 17th 06 03:28 PM
How easy is it to loop in VBA based on cell contents? Lee Harris Excel Worksheet Functions 3 November 25th 05 01:21 AM


All times are GMT +1. The time now is 12:15 PM.

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

About Us

"It's about Microsoft Excel"