Checking if it is the 1st cell in a range?

jxb

Board Regular
Joined
Apr 19, 2007
Messages
172
Office Version
  1. 2010
Platform
  1. Windows
to all

Is there a way of checking when the 1st cell in a range is used see below

VBA Code:
Sub macro1()

Dim rgcurrentrw As Range
Set rgcurrentrw = ActiveSheet.Range("A2:E2")

Dim strout As String

'what I have currently
For Each cell In rgcurrentrw
    strout = strout & "," & cell.Value
Next cell

Debug.Print strout
'I get strout ,aa1,bb1,1,2,3
'but I want aa1,bb1,1,2,3 ie no comma from the 1st cell
'someting like
If 1stcell in range rgcurrentrw then
   strout = cell.Value
Else
      strout = strout & "," & cell.Value
End If

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi,
see if this update to your code does what you want

VBA Code:
For Each cell In rgcurrentrw
    strout = strout & cell.Value & ","
Next cell
strout = Mid(strout, 1, Len(strout) - 1)

Dave
 
Upvote 0
Thanks. This MID() removes the last value in the string it seems. I think I get the idea however
VBA Code:
strout = Mid(strout, 2, Len(strout))
I am not sure this is going to work for me as if I want to expand to multiple rows then at "loop" I'll lose the 1st character every time
 
Last edited:
Upvote 0
VBA Code:
For Each cell In rgcurrentrw
    strout = strout & cell.Value & ","
Next cell
strout = Mid(strout, 1, Len(strout) - 1)

my code removes the comma (",") at the very end of the string

You have posted something different

Dave
 
Upvote 0
Solution
my apologies. Just spotted the different syntax for strout.
 
Upvote 0
my apologies. Just spotted the different syntax for strout.

No worries does happen from time to time - always worth looking at all updates to code posted.

Trust suggestion has resolved the issue?

Dave
 
Upvote 0
I think it did. But thinking about the ultimate aim of the macro I might be better of writing to a csv (txt) file directly rather than working with a concatenated string and then write the final one to the file
 
Upvote 0

Forum statistics

Threads
1,215,639
Messages
6,125,968
Members
449,276
Latest member
surendra75

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top