Function ConCat(Delimiter As Variant, ParamArray CellRanges() As Variant) As String
Dim cell As Range, Area As Variant
If IsMissing(Delimiter) Then Delimiter = ""
For Each Area In CellRanges
If TypeName(Area) = "Range" Then
For Each cell In Area
If Len(cell.Value) Then ConCat = ConCat & Delimiter & cell.Value
Next
Else
ConCat = ConCat & Delimiter & Area
End If
Next
ConCat = Mid(ConCat, Len(Delimiter) + 1)
End Function
X = ConCat(",",Range("A1:A4"))
Set MyRange = Range("A2",Range("A2").End(xlDown))
Dim X as String
X = ConCat(",",MyRange)
Sub MainMacro()
'This is where I do stuff
'Define my variables
Dim myRange As Range
Dim myString As String
'Set this variable to something
Set myRange = Range("A2:A10")
'or a dynamic range
Set myRange = Range("A2", Range("A2").End(xlDown))
'call the function and pass the output to a variable
myString = ConCat(",", myRange)
'Do something with that info
MsgBox myString
End Sub
Function ConCat(Delimiter As Variant, ParamArray CellRanges() As Variant) As String
Dim cell As Range, Area As Variant
If IsMissing(Delimiter) Then Delimiter = ""
For Each Area In CellRanges
If TypeName(Area) = "Range" Then
For Each cell In Area
If Len(cell.Value) Then ConCat = ConCat & Delimiter & cell.Value
Next
Else
ConCat = ConCat & Delimiter & Area
End If
Next
ConCat = Mid(ConCat, Len(Delimiter) + 1)
End Function
Option Explicit
Sub MergEM1()
Dim r As Range
Dim str As String
'Loop through the used cells and combine.
For Each r In Range("A2", Range("A2").End(xlDown))
str = str & r.text & ", "
Next r
MsgBox Left(str, Len(str) - 2) 'Present results cleanly
End Sub
I need a simple macro to combine a list of numbers (rows) into one cell separated by commas.
I picture a loop that starts at the first cell and continues until there is a blank.
Function VList$(Rg As Range, Optional Delimiter$ = ",")
VList = Join(Application.Transpose(Range(Rg(1), Rg(1).End(xlDown))), Delimiter)
End Function
Sub Demo()
[E1].NumberFormat = "@"
[E1].Value = VList([A1])
End Sub
Not sure what you mean...my comments appear just fine.@Luke M
Hi, buddy!
"all pretty"... but (and as b(ut)ob(ut)hc uses to say, there's always a but...t?) not for indented comments.
Regards!
Just in case we are interested in a formula instead of the VBA, here is what can be done.
Assumption: Your data starts from A1 upto A10.
in B1 update the formula"=A1"
in B2 update ="B1&","&A2"
Hope this helps..
@Luke MNot sure what you mean...my comments appear just fine.
Option Explicit
Sub ExpandAndPlay()
' constants
Const ksWSSource = "Analyse"
Const ksDate = "A2"
Const kiColumnKey = 2
Const kiColumnCalculationSource = 14
Const ksWSTarget = "ZB "
Const ksKey = "D2"
Const kiColumnDate = 1
Const kiColumnCalculationTarget = 15
' declarations
Dim dDate As Date, nCalculation As Single, iKey As Integer
Dim I As Long, J As Long
' start
dDate = Worksheets(ksWSSource).Range(ksDate).Value
' process
For I = 4 To Worksheets(ksWSSource).Rows.Count
' quit at 1st empty
If Worksheets(ksWSSource).Cells(I, 1).Value = "" Then Exit For
' retrieve key
iKey = Worksheets(ksWSSource).Cells(I, kiColumnKey).Value
With Worksheets(ksWSTarget)
' put key
.Range(ksKey).Value = iKey
' find row
J = .Cells.Find(dDate, .Cells(1, kiColumnDate), xlValues, xlWhole).Row
' retrieve calculation
nCalculation = .Cells(J + 1, kiColumnCalculationTarget).Value
Worksheets(ksWSSource).Cells(I, kiColumnCalculationSource).Value = nCalculation
' collapse all groups
.Outline.ShowLevels 1
' expand found group
.Rows(J - 1).ShowDetail = True
' print
.PrintOut , , , True
End With
Next I
' end
Beep
End Sub
Option Explicit
Sub ExpandAndPlay()
' constants
Const ksWSSource = "Analyse"
Const ksDate = "A2"
Const kiColumnKey = 2
Const kiColumnCalculationSource = 14
Const ksWSTarget = "ZB "
Const ksKey = "D2"
Const kiColumnDate = 1
Const kiColumnCalculationTarget = 15
' declarations
Dim dDate As Date, nCalculation As Single, iKey As Integer
Dim I As Long, J As Long
' start
dDate = Worksheets(ksWSSource).Range(ksDate).Value
' process
For I = 4 To Worksheets(ksWSSource).Rows.Count
' quit at 1st empty
If Worksheets(ksWSSource).Cells(I, 1).Value = "" Then Exit For
' retrieve key
iKey = Worksheets(ksWSSource).Cells(I, kiColumnKey).Value
With Worksheets(ksWSTarget)
' put key
.Range(ksKey).Value = iKey
' find row
J = .Cells.Find(dDate, .Cells(1, kiColumnDate), xlValues, xlWhole).Row
' retrieve calculation
nCalculation = .Cells(J + 1, kiColumnCalculationTarget).Value
Worksheets(ksWSSource).Cells(I, kiColumnCalculationSource).Value = nCalculation
' collapse all groups
.Outline.ShowLevels 1
' expand found group
.Rows(J - 1).ShowDetail = True
' print
.PrintOut , , , True
End With
Next I
' end
Beep
End Sub
So it's not the same as initial demand was
@b(ut)ob(ut)hc
Hi, Marc L!Are we on the same planet ?‼
@Luke M@SirJB7
So it does, I guess I never noticed.