Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1
Default Using a variable in the Range function

I found this great module on he
Sub ConcatSelection()
Dim rng As Range
Dim strConcat As String

For Each rng In Selection
strConcat = strConcat & " " & rng.Text
Next
Range("F2") = strConcat
End Sub

It works nicely to concatenate the user-selected cells, but I'd like the
user to be able to chose the destination cell. Is that possible?
I tried adding this line above the Range line:
x = Application.InputBox(prompt:="enter the value", Type:=1)
then changed Range("F2") = strConcat to Range(x) = strConcat

But I got an error.
Is it possible to do this?

Thank you,
Dee

  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 29
Default Using a variable in the Range function

Thank you so much for the quick reply!

I changed my code to read:
Sub ConcatAndPaste()
Dim rng As Range
Dim strConcat As String

For Each rng In Selection
strConcat = strConcat & " " & rng.Text
Next
Dim x As String
x = Application.InputBox(prompt:="Enter target cell address")
Range(x) = strConcat
End Sub

I ran it and, when prompted, clicked the cell that I wanted to paste the
concatenated string into which put =$F$2 into the Input box. When I clicked
OK, I got an error. Debug shows
Range(x) = strConcat
highlighted in yellow.

Obviously I goofed. Can you tell me where?

Thanks again,
Dee



"Bernard Liengme" wrote:

This worked for me
Sub tryme()
Dim x As String
x = Application.InputBox(prompt:="Enter target cell address")
Range(x) = "hello, World"
End Sub

best wishes
--
Bernard V Liengme
Microsoft Excel MVP
http://people.stfx.ca/bliengme
remove caps from email


"Dee Sperling" <Dee wrote in message
...
I found this great module on he
Sub ConcatSelection()
Dim rng As Range
Dim strConcat As String

For Each rng In Selection
strConcat = strConcat & " " & rng.Text
Next
Range("F2") = strConcat
End Sub

It works nicely to concatenate the user-selected cells, but I'd like the
user to be able to chose the destination cell. Is that possible?
I tried adding this line above the Range line:
x = Application.InputBox(prompt:="enter the value", Type:=1)
then changed Range("F2") = strConcat to Range(x) = strConcat

But I got an error.
Is it possible to do this?

Thank you,
Dee



  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 35,218
Default Using a variable in the Range function

How about asking for both ranges?

Option Explicit
Sub ConcatSelection()
Dim InputRng As Range
Dim myCell As Range
Dim DestCell As Range
Dim strConcat As String

Set InputRng = Nothing
Set DestCell = Nothing
On Error Resume Next
Set InputRng = Application.InputBox _
(Prompt:="Select the cells to concatenate", _
Default:=Selection.Address, Type:=8)
If InputRng Is Nothing Then
Exit Sub 'user hit cancel
End If
Set DestCell = Application.InputBox _
(Prompt:="Select the destination cell to concatenate", _
Type:=8).Cells(1)
If DestCell Is Nothing Then
Exit Sub 'user hit cancel
End If
On Error GoTo 0

strConcat = ""
For Each myCell In InputRng.Cells
strConcat = strConcat & " " & myCell.Text
Next myCell

If Len(strConcat) 0 Then
strConcat = Mid(strConcat, 2)
End If

DestCell.Value = strConcat

End Sub

Personally, I don't like this for most things.

I'd rather use a function in that receiving cell:

Like:
=multicat(a1:a10)

If you think you want to try this, take a look at JE McGimpsey's site:
http://mcgimpsey.com/excel/udfs/multicat.html



Dee Sperling wrote:

I found this great module on he
Sub ConcatSelection()
Dim rng As Range
Dim strConcat As String

For Each rng In Selection
strConcat = strConcat & " " & rng.Text
Next
Range("F2") = strConcat
End Sub

It works nicely to concatenate the user-selected cells, but I'd like the
user to be able to chose the destination cell. Is that possible?
I tried adding this line above the Range line:
x = Application.InputBox(prompt:="enter the value", Type:=1)
then changed Range("F2") = strConcat to Range(x) = strConcat

But I got an error.
Is it possible to do this?

Thank you,
Dee


--

Dave Peterson
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
Define variable range input for SUM() function JeffC Excel Worksheet Functions 4 May 16th 09 03:54 AM
Variable named range in worksheet function Barb Reinhardt Excel Worksheet Functions 6 July 26th 08 03:39 AM
Variable range in MAX-function nsv Excel Worksheet Functions 3 July 20th 06 12:27 PM
Using a range variable inside a excel function Michael Excel Discussion (Misc queries) 2 November 14th 05 02:52 PM
A function to get a variable row reference for range in XNPV funct Tex1960 Excel Worksheet Functions 6 August 1st 05 11:20 PM


All times are GMT +1. The time now is 02:51 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"