Mentre concordo con @jonsharpe che se si utilizza Lbound(arr)
e Ubound(arr)
, non è necessario sapere questo in fase di esecuzione, ma penso che sia una domanda interessante.
In primo luogo, un esempio di looping corretto attraverso un array.
For i = LBound(arr) To Ubound(arr)
' do something
Next
Ora, al fine di fare esattamente quello che hai chiesto, è necessario accedere utilizzare la Libreria VBIDE
. Altrimenti nota come libreria "Estensibilità di Microsoft Visual Basic". Fornisce l'accesso all'IDE e al codice con dentro. Puoi usarlo per scoprire se è stato dichiarato Option Base 1
. Tuttavia, non è consigliabile provare a farlo in fase di esecuzione. Questo sarebbe più utile come una sorta di analisi del codice statico durante lo sviluppo.
In primo luogo, è necessario add a reference to the library e grant the code access to itself. Una volta fatto ciò, il codice seguente dovrebbe fare ciò che desideri.
Public Sub FindOptionBase1Declarations()
Dim startLine As Long
startLine = 1
Dim startCol As Long
startCol = 1
Dim endLine As Long
endLine = 1 ' 1 represents the last line of the module
Dim endCol As Long
endCol = 1 ' like endLine, 1 designates the last Col
Dim module As CodeModule
Dim component As VBComponent
For Each component In Application.VBE.ActiveVBProject.VBComponents ' substitute with any VBProject you like
Set module = component.CodeModule
If module.Find("Option Base 1", startLine, startCol, endLine, endCol, WholeWord:=True, MatchCase:=True, PatternSearch:=False) Then
Debug.Print "Option Base 1 turned on in module " & component.Name
' variables are passed by ref, so they tell us the exact location of the statement
Debug.Print "StartLine: " & startLine
Debug.Print "EndLine: " & endLine
Debug.Print "StartCol: " & startCol
Debug.Print "EndCol: " & endCol
' this also means we have to reset them before we look in the next module
startLine = 1
startCol = 1
endLine = 1
endCol = 1
End If
Next
End Sub
Vedere CodeModule.Find documentation per ulteriori informazioni.
Se si utilizza un componente aggiuntivo è un'opzione, @ progetto open source che Mat'sMug e di Rubberduck ha un Code Inspection che vi mostrerà tutte le istanze di questo tutto il progetto attivo.

See this for more information on that particular inspection.
C'è un motivo per cui è necessario? Se usi 'LBound' e' UBound' non importa quale sia l'impostazione usata. – jonrsharpe
È possibile definire una matrice e controllare se 'LBound' è' 0' o '1'. Ma se l'opzione "base" è determinata dal programmatore, perché la stessa persona deve trovarla. Cerca l'istruzione 'Option Base' nel codice per trovarla. –
Downvoter/Close voter. Sarebbe bello se tu lasciassi un commento per spiegare ciò che trovi così poco chiaro. Mi sembra abbastanza chiaro. – RubberDuck