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