Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA copy contents of cells A1:A22 to string variable

Status
Not open for further replies.

gustaf111

Programmer
Jan 16, 2011
87
SE
Hello,

I have a problem that I can not solve ...

I would like to copy the contents of cells A1 to A22 to a string variable. The cell are not merged becuase I would like to design the contents better (there are several rows).

Ex:

A B C D E .....
1 Hello World

The string I would like to have is:
String = Hello World


Please help! Gustaf
 
Code:
strA=""
for each c in [a1:a22].cells
  strA=strA & c.value
next

_________________
Bob Rashkin
 



hi,

I'd design this as a user defined function. copy this to a MODULE and use as you would any other spreadsheet function...
Code:
Function ConcatValues(rng As Range) As String
  Dim r As Range

  For Each r In rng
    ConcatValues = ConcatValues & r.Value
  Next
End Function


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Yea I agree with Skip. Once you put your function in a cell you'll have to select the range in which the words to form the string are contained and the work is done!
 
How are you treating empty cells?
In your example, it appears that cell B1 is empty but you want whitespace in your output string.
 
Hello all,

Thanks for all comments! I write wromg, it should be A1:Z1!

Sorry!
 

If you use the UDF I created, the range need not be hard coded.

The range is a reference to the function and it can be ANY range you choose.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks!! Do you know anything about my other problem (Excel VBA write protected)?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top