determine how many elements an array will have

mc136355

New Member
Joined
Mar 20, 2018
Messages
36
Hi

newbie needing to know how to declare an array where we don't know how many elements due to always increasing
I have a list in A1 to A10 and need to use lastrow to declare array

also would like to display msgbox after the loop to give a list of all items (don't know if this would be join or split function)

thanks for any help

MC

Dim lastrow As Long
lastrow = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Count
Dim i As Long
Dim fileArray(1 To lastrow) As String
For i = 1 To lastrow
MsgBox fileArray(i)
Next i
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi,
If you want to assign your range to an array then try this


Code:
      Dim fileArray As Variant, Element As Variant
    
    fileArray = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value2
    
    For Each Element In fileArray
        MsgBox Element
    Next

Dave
 
Last edited:
Upvote 0
Hi thanks for reply

code works great and puts the range into the array

can I instead of msgbox for each individual element just have one with list of elements separated by comma

do I need to use redim preserve and then output after loop

thanks
MC
 
Upvote 0
something like following maybe?


Code:
    For Each Element In fileArray
        msg = msg & Element & " ,"
    Next
    
        MsgBox Mid(msg, 1, Len(msg) - 1)

Dave
 
Upvote 0
Hi again bit stuck on getting the following code to work

first part works and goes to function but I then get subscript out of range

cant understand the problem


Private Sub CommandButton1_Click()
Worksheets("INVENTORY").Activate
If WorksheetFunction.CountIf(Range("C5:C15"), Range("Q17")) = 0 Then
MsgBox "Item not in list, choose from list"
Else
MsgBox "Valid Choice"
myArr = Worksheets("INVENTORY").Range("Q17")

MyVar = Find_Sheet(myArr)


End If
End Sub


Function Find_Sheet(myArr As String) As Boolean
Dim Element As Variant

fileArray = Worksheets("test3").Range("A1:A" & cells(Rows.Count, "A").End(xlUp).Row).Value2

For Each Element In fileArray
msg = msg & Element & " ,"
Next

MsgBox Mid(msg, 1, Len(msg) - 1)
End Function
 
Upvote 0
does Worksheets("test3") exist and if so, is the workbook the active workbook or do you have another workbook open?

Dave
 
Upvote 0
try this update to function resolves the error.


Code:
Function Find_Sheet(ByVal myArr As String) As Boolean
    Dim Element As Variant
    Dim msg as string
    
    With ThisWorkbook.Worksheets("test3")
        fileArray = .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value2
    End With
    
    For Each Element In fileArray
    msg = msg & Element & " ,"
    Next
    
    MsgBox Mid(msg, 1, Len(msg) - 1)
End Function

whats your intention with argument myArr - you pass a value from range to it but are not using it in function?

Dave
 
Upvote 0
Hi Dave

Thanks for your help as a newbie I get quite confused with what is an array variable etc. and how to manipulate.
I will endeavour to keep learning and improve my knowledge.

code is perfect for my needs

thanks
MC
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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