用於 Excel 的 VBA,建立某種動態 if 語句

用於 Excel 的 VBA,建立某種動態 if 語句

我正在開發一個類似資料庫的文檔,用於儲存專案及其資訊。我在考慮使用 VBA 為這些內容產生報告的方法時遇到一些問題。目前,我有一個用戶表單,其中有一個多頁,其中包含項目的每行資訊的選項卡:

選項卡:[全部新增至代表] [按狀態新增] [按類型新增] [按序列新增] [按到期日新增]

每個選項卡都有不同的切換按鈕。

起初,我只想讓程式碼使用帶有 multipage.SelectedItem.index 的選擇案例來製作報告,並且我意識到這對報告的方式非常有限。我決定在選項卡中添加一個“添加頁面”,以便您可以添加不同的條件,例如將狀態為A、expDate A 的所有項目添加到expDate B。我將編寫所有可能的 IF 語句...現在尋求協助:有沒有辦法製作動態 IF 或 CASE 區塊,在其中只能將「活動」或新增的標籤新增至搜尋條件?程式碼的其餘部分包括計算工作表上的項目數量,並使用 FOR 迴圈進行迭代,並在滿足條件時將它們新增至 Word 文件。我還沒有編寫可靠的程式碼,但我的程式碼如下所示:

在這段程式碼中,我嘗試建立一個「收集」函數來管理可能的 IF 語句,這取決於您傳遞給它的參數:

Private Function advancedGathering(Optional status As String, Optional itemType As String, Optional id As Integer, Optional expDate As Date, Optional holder As String)

Select Case advancedGathering
    Case status <> Null And _
         itemType <> Null And _
         id <> Null And _
         expDate <> Null And _
         holder <> Null

    Case status <> Null And _
         itemType <> Null And _
         id  <> Null And _
         expDate <> Null And _
         holder <> Null

    End Select


End Function

正如你所看到的,它是不完整的。

我也有這個按鈕點擊事件:

Private Sub Button_generate_Click()

    Dim row, quantity As Integer

    quantity = WorksheetFunction.Count(Range(colHandler.column("id") & 3, colHandler.column("id") & 900)) 'There are less than 900 items, this is just to capture all

    If Toggle_status_ATR.Value = False Or _
       Toggle_type_ATR.Value = False Or _
       Toggle_id_ATR.Value = False Or _
       Toggle_expDate_ATR.Value = False Or _
       Toggle_holder_ATR.Value = False Then

        Select Case MultiPage_main.SelectedItem.Index
            Case 0 'Add All"
                If Toggle_all_addAll.Value = True Then
                    'This is the first tab which I'm not worried about because if selected it will add all the info on the items
                Else

                End If

            Case 1 'Add by Status
                    If Toggle_status_wayOverdue.Value = True Then
                ElseIf Toggle_status_overdue.Value = True Then
                ElseIf Toggle_status_due.Value = True Then
                ElseIf Toggle_status_good.Value = True Then
                ElseIf Toggle_status_valid.Value = True Then
                End If

            Case 2 'Add by Type

            Case 3 'Add by ID

            Case 4 'Add by expDate

            Case 5 'Add by Holder

        End Select

    Else '> Any of the [Toggle_xxx_ATR] buttons are clicked

    End If

Multipage_selection: 'I tried to use GOTO statements


check_valid:

    Unload Me

End Sub

老實說,我已經沒有想法了,我不是一個熟練的編碼員,而且作為我實際工作的附加項目,我使用 VBA 的時間只有幾個月。任何想法都有幫助,感謝大家的寶貴時間!

相關內容