I'm writing a function that will take a range of cells and return their values in a single cell separated by commas.
So a list of three or four stock tickers would look like this:
GOOG, TSLA, APPL
The easy way to do this is:
For Each cell In rng
Tickers = Tickers & cell.Value & ", "
Next cell
The problem, of course, is that the last value will have a comma which is undesirable.
So I thought it might be possible to include an If
statement that would change the output for the last cell like this:
For Each cell In rng
If cell = rng.Count - 1 Then
Tickers = Tickers & cell.Value & "! "
Else
Tickers = Tickers + cell.Value & ", "
End If
Next cell
However, this does not seem to be working I assume because cell
and rng
are possibly not simple integers so count-1
doesn't make sense.
So what would be the best way to go about this? Is there a method for rng
that would allow the If
statement to know if the cell was indeed the last in the range?