Lbound() to UBound() logic

bsquad

Board Regular
Joined
Mar 15, 2016
Messages
194
I am wondering if there was any sort of way to assign an array order. Example - If one of the values in the array matches a cell value then assign that to the number 1 spot in the array. Here is the particular methodology I am using.
I do not want to hardcode and list out my entire array in the actual code as this will be dynamic - each import file name could be different.

or put in a simple way; with using Application.GetOpenFileName, I am wondering if there is a way you can assign the LBound() based on a cell value.
All thats really a concern is the Lbound, after it has matched, I don't care about the order after

If I am not mistaken; the general order logic of Lbound to Ubound is to take numerics first then alpha characters, but I also do not want the user to have to modify the import file name.

I also saw another post where there was a Function that could resort from A to Z, but I'd like to avoid Functions as well if I can.

Code:
Sub AssignUbound()
    Dim eWorkbook, iWorkbook As Workbook
        Set eWorkbook = ThisWorkbook
    Dim iWorkbookImportOpen As Variant
    Dim i As Integer
        
        Application.DisplayAlerts = False: Application.AskToUpdateLinks = False: Application.ScreenUpdating = False: Application.StatusBar = True
        chdir eWorkbook.Path
        iWorkbookImportOpen = Application.GetOpenFilename(FileFilter:="Excel Workbooks(*.xls; *.xlsx; *.xlsm; *.xltx; *.xltm), *.xls; *.xlsx; *.xlsm; *.xltx; *.xltm", _
                                            Title:="Select Import File(s)", ButtonText:="Select & Import", MultiSelect:=True)
                                            On Error Resume Next


        For i = LBound(iWorkbookImportOpen) To UBound(iWorkbookImportOpen)
            MsgBox (Left(iWorkbookImportOpen(i), (InStrRev(iWorkbookImportOpen(i), ".", -1, vbTextCompare) - 1)))
        Next i
        
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
LBound simply returns the lower boundary of the array (usually 0 or 1), and UBound the upper boundary - i.e. the number of items in the array. Neither has anything to do with what data type is in the array.

The only way to control the order you loop through the items is to either populate the array in a specific order in the first place, or rearrange it afterwards.
 
Upvote 0
LBound refers to the first item in an array and Ubound refers to the last item in an array and both hold an integer value. LBound integer value can be 0 or 1. Ubound, when LBound = 0 will be 1 less than the total items in the array, and if LBound = 1 Then UBound will = the number of items in the array. Hope this clarifies it for you.
 
Upvote 0
Thank you both for the replies,

I think I may explained it wrong; I get how Lbound works; but I was just wondering if there was a way I could sort the order based on the Variant value matching to a cell value. So if my array(iWorkbookImportOpen) selected was ("Sheet1", "Sheet2", "Sheet3") and if I had Cells(1,1).value = "Sheet2" >> if I could do something like
For i = Lbound(iWorkbookImportOpen) to Ubound(iWorkbookImportOpen)
If iWorkbookImportOpen(i) = cells(1,1).value then
iWorkbookImportOpen(i) = Lbound(iWorkbookImportOpen)

but I think both of you have kind of answered it
 
Upvote 0
If your array base is 0, ie. Lbound(array) = 0, and you have in your array, "Mom", "Dad", "Junior", "Sissy" then
array(0) = "Mom"
array(3) = "Sissy"
The numbers in parentheses are the indext number of the items in the array with LBound being the lower limit and Ubound being the upper limit regardless of the number of items contained in the array. You can then use the array variable name with an index number just like any other variable value.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,011
Members
452,374
Latest member
keccles

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