2015-04-01 133 views
6

如何以編程方式確定VBA中的Option Base的當前設置? Option Base可以設置爲01,並且這可以確定數組索引是從0還是1開始(請參閱MSDN)。如何以編程方式確定VBA中的Option Base的當前設置

但是,我看不到任何簡單的方法來找出當前設置是什麼。我希望有可能是一個Option()功能,我可以將參數傳遞,是這樣的:

Debug.Print Option("Base") 

,它會告訴我,但它似乎並不在那裏。

+4

是否有這樣的理由?如果您使用'LBound'和'UBound',則使用哪種設置無關緊要。 – jonrsharpe 2015-04-01 12:33:06

+0

你可以定義一個數組,看'LBound'是'0'還是'1'。但是,如果'option base'由編碼器確定,爲什麼同一個人需要找到它。在代碼中搜索'Option Base'語句來查找它。 – 2015-04-01 12:38:49

+0

Downvoter /關閉投票人。如果您留下評論以解釋您發現如此不清楚的情況,那將會很好。看起來很清楚。 – RubberDuck 2015-04-01 12:58:22

回答

7

雖然我同意@jonsharpe,如果您使用Lbound(arr)Ubound(arr),您不需要在運行時知道這一點,但我認爲這是一個有趣的問題。

首先,正確循環數組的示例。現在

For i = LBound(arr) To Ubound(arr) 
    ' do something 
Next 

,爲了做到準確問什麼,你需要訪問使用VBIDE庫。否則稱爲「Microsoft Visual Basic for Applications擴展性」庫。它提供對IDE和代碼的訪問。您可以使用它來發現是否已聲明Option Base 1。我不建議在運行時嘗試這樣做。這在開發過程中作爲一種靜態代碼分析會更有用。

首先,您需要登錄add a reference to the librarygrant the code access to itself。一旦你這樣做了,下面的代碼應該做你想做的。

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 

查看CodeModule.Find documentation瞭解更多信息。


如果使用外接爲RubberduckCode Inspection會告訴你整個活動項目的這個所有實例的選項,@ Mat'sMug和我的開源項目。

Option Base 1 Code Inspection

See this for more information on that particular inspection

+0

創建一個數組並檢查它的'LBound'不是更容易嗎? – jonrsharpe 2015-04-01 13:12:40

+0

真棒答案,我希望可以很容易地修改這與其他'Option's一起工作,甚至可以創建我尋求的神聖的Option()函數。 @jonsharpe是對的這是一個有點學術問題,但我很高興你也發現它也很有趣。這是一個例行公事,並滾動到模塊的頂部(是的,我知道Ctrl + Home)並不像是一個壞主意。 ;) – Caltor 2015-04-01 13:13:26

+0

是的。確定它是@jonrsharpe,但這不是OP要求的。 – RubberDuck 2015-04-01 13:14:06

2
Function GetBaseOption() As Long 

    Dim arr 

    arr = Array("a") 
    GetBaseOption = LBound(arr) 

End Sub 
+0

不錯的解決方案。我想知道你是否可以在直接窗口中成功排隊。 – RubberDuck 2015-04-01 16:36:49

+0

@RubberDuck確定只需將其更改爲函數並返回'LBound'而不是打印它,並且您可以在即時沒有問題的情況下使用它。 – Brad 2015-04-01 16:38:17

+1

@RubberDuck - 我不知道在哪個上下文中運行(如果您在立即窗口中完全運行它) - 可能不可靠,因爲您可以在每個代碼模塊中使用不同的基本設置。 – 2015-04-01 16:38:55

相關問題