Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default how do you concatenate two variables in VBA

Need to concatenate the contents of two cells together, using VBA.
for example:

Cell A1 = HSBC
Cell A2 = LN equity

so that cell A3 would show exactly as follows: HSBC LN equity

I can do this on excel spread sheet as: Cell A3 = A1&A2
(indenting contents of A2 one space) however I need to do this in VBA
format.

The beginning of my program is as follows but I get an error message
in line 2 of program.

If [C1] = "United Kingdom" Then
[A1] & [A2]
.. . . . .
ElseIf [C1] = "France" Then
.. . . . .
Else [C1] = ""
EndIf


need to do this for 15 other countries.

Your help in this would be very much appreciated.

Manuel
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 493
Default how do you concatenate two variables in VBA

One way:

If Range("C1").Value = "United Kingdom" Then
Range("C1").Value = Range("A1").Value & Range("A2").Value
ElseIf Range("C1").Value = "France" Then
...
Else
Range("C1").ClearContents
End If
or, a bit more efficiently:

With Range("C1")
If .Value = "United Kingdom" Then
.Value = Range("A1").Value & Range("A2").Value
ElseIf .Value = "France" Then
...
Else
.ClearContents
End If
End With

For 15 countries, though, I'd use the Select Case structu

With Range("C1")
Select Case .Value
Case "United Kingdom"
.Value = Range("A1").Value & Range("A2").Value
Case "France"
...
Case ...
...
Case Else
.ClearContents
End Select
End With


Note that the [] notation, while it saves typing, is in most cases
much slower than using the range references.

In article ,
billabong < wrote:

Need to concatenate the contents of two cells together, using VBA.
for example:

Cell A1 = HSBC
Cell A2 = LN equity

so that cell A3 would show exactly as follows: HSBC LN equity

I can do this on excel spread sheet as: Cell A3 = A1&A2
(indenting contents of A2 one space) however I need to do this in VBA
format.

The beginning of my program is as follows but I get an error message
in line 2 of program.

If [C1] = "United Kingdom" Then
[A1] & [A2]
. . . . .
ElseIf [C1] = "France" Then
. . . . .
Else [C1] = ""
EndIf


need to do this for 15 other countries.

Your help in this would be very much appreciated.

Manuel

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
Not at all clear on use of variables and/or object variables JMay-Rke Excel Discussion (Misc queries) 11 July 4th 08 06:36 PM
Three Variables George Excel Worksheet Functions 1 July 8th 06 07:47 PM
Using variables . . . Wayne Knazek Excel Discussion (Misc queries) 2 July 6th 06 05:01 PM
Using variables in a name Vispy Excel Discussion (Misc queries) 4 February 22nd 06 01:17 AM
I know how to concatenate ,can one de-concatenate to split date? QUICK BOOKS PROBLEM- New Users to Excel 1 July 26th 05 05:07 PM


All times are GMT +1. The time now is 10:37 AM.

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"