Mohamed Hussien
Mohamed Hussien

Reputation: 69

EXCEL VBA | Cell equals selection

I've a question about showing a Selection value inside a specific cell in my sheet.(let's call it J1 for now) So, If the user drag-selected (by mouse) A1,A2,A3,A4. J1 value will show "A1:A4", after then with some VBA code I concatenate these cells to show cells values separated by ";". The problem is, when the user selects cells which is not in order (by holding CTRL), Like A1,A5,A11. J1 value will shows "A1,A5,A11" when I concatenate, it gives "#VALUE" error.

Can we just replace every cell reference here with cell value? and leave the "comma" in between as is. then later we can Subtitute comma with ";"

Excuse me if my question seems a little bit ignorant :)

my code for selection:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Dim s As String
Set rng = Application.Selection
If rng.Count < 2 Then
Range("H1").Value = Cells(Target.Row, Target.Column).Value
Else
Range("H1").Value = rng.Address
End If     
End Sub

Code for Concatenation:

Function ConcatenateRange(ByVal cell_range As Range, _
                    Optional ByVal seperator As String) As String

Dim cell As Range
Dim lastrow
Dim choice
Dim lastrowmodified
Dim rangy

Dim newString As String
Dim cellArray As Variant
Dim i As Long, j As Long

cellArray = cell_range.Value

For i = 1 To UBound(cellArray, 1)
    For j = 1 To UBound(cellArray, 2)
        If Len(cellArray(i, j)) <> 0 Then
            newString = newString & (seperator & cellArray(i, j)) & ";"
        End If
    Next
Next

If Len(newString) <> 0 Then
    newString = Right$(newString, (Len(newString) - Len(seperator)))
End If

ConcatenateRange = newString

End Function

Upvotes: 3

Views: 3872

Answers (2)

Jordan
Jordan

Reputation: 4514

Another method would be to use a string array in conjunction with a JOIN function. This works for non-contiguous selections:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim c as Range, i as Integer
Dim arr() As String
ReDim arr(0 To Selection.Count - 1) As String

If Selection.Count < 2 Then
    Range("J1").Value = Selection.Value
Else
    For Each c In Selection.Cells
        arr(i) = c.Value
        i = i + 1
    Next c
    Range("J1").Value = Join(arr, ";")
End if

End Sub

Upvotes: 3

Wolfie
Wolfie

Reputation: 30101

If I understand correctly, you want the one cell, say J1 to contain all values of selected cells, separated by a semi colon? If so, you can just modify your first sub,

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim rng As Range
    Set rng = Application.Selection
    Dim vCell as Range

    Range("J1").Value = ""

    ' Cycle through cells in range
    For each vCell in rng 

        ' Use if so that J1 doesn't start with a semi colon
        If Range("J1").Value = "" Then
            Range("J1").Value = vCell.Value
        Else
            Range("J1").Value = Range("J1").Value & ";" & vCell.Value
        End If


    Next vCell

End Sub

Upvotes: 5

Related Questions