Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a simple array challenge!

Status
Not open for further replies.

smma

Mechanical
Jun 4, 2007
24
I am new to VB, and I have a question. I want to create an array from a range of cells, starting at C9 and using the xldown to go to the last nonblank cell to grab my array. Then, I would like to make the array be listed in two ways: if the number of cells is 1 (C9 only), it would only list C9. If it is more than 1, then the array would be resolved as "C9 + Chr(13) + Chr(10) + next cell" and loop through until the array is filled. Does anyone have any ideas on how this could properly be set up with a for/next statement? Will I need an If Then statement nested within the For/Next? Your input would be greatly appreciated.
 
Replies continue below

Recommended for you

Selection.End(xlDown).Select gets you to the end of a contiguous block, if it's more than 1 cell deep.

TTFN

FAQ731-376
 
It looks like you are trying to get a concatenated string rather than an array. You can only use a For/Next loop if you know the number of cells. In this case with VBA it's much easier to use a Do Loop, with the IsEmpty function as your test. Try this:
Code:
Private Sub CommandButton1_Click()
firstrow = 9
firstcol = 3
a = firstrow
Do
'mystring = mystring & vbCrLf & Cells(a, firstcol).Value
mystring = mystring & vbCrLf & Chr$(64 + firstcol) & CStr(a)
a = a + 1
Loop Until IsEmpty(Sheets(1).Cells(a, firstcol).Value)
Debug.Print mystring
End Sub
If you use the remmed out line instead of the following line you will get the values rather than the cell names

Good Luck
johnwm
________________________________________________________
To get the best from these forums read faq731-376 before posting
Steam Engine enthusiasts
Steam Engine Prints
 
Every1, thanks 4 your input...it helps me to understand the how and why of visual basic. However, I was able to figure it out. Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor