Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50076

Range2CSV macro; modifying input box

$
0
0
I have a macro that concatenates a selected range of values, separated by commas. Currently, the macro uses an input box that prompts the user to manually type in the output cell, but I want to be able to click on a cell and have the macro recognize that as the output cell. For instance, after selecting the range and running the macro, I would need to type in "A2" into the input box when prompted; what I want to do is click on A2 and have it recognize "=$A$2" as a valid output cell.

Here is the macro below:


Code:

Sub Range2CSV()
'
' Shortcut to insert "," between values in a column.
'
 Dim selAreas As Range
 Dim pasteAreas As String
pasteAreas = Application.InputBox(Prompt:= _
            "ENTER TARGET CELL:", _
            Title:="Target cell location", Type:=2)
    If pasteAreas = "" Then
Exit Sub
    End If
Range(pasteAreas).ClearContents
        For Each selAreas In Selection
Range(pasteAreas) = Range(pasteAreas) & "," & selAreas.Value
        Next selAreas
        Range(pasteAreas) = Mid(Range(pasteAreas), 2)
End Sub

So far, I've tried declaring pasteAreas as a range instead of a string and changing the input box type to "8" (instead of "2"), but that just resulted in Run-time error '91': Object variable or With block variable not set. Leaving pasteAreas as a string but changing the input box type resulted in no output at all.

There's probably a straightforward way to do this that I'm missing; please help me not go crazy!

Viewing all articles
Browse latest Browse all 50076

Trending Articles