簡體   English   中英

使用VBA不能滿足某些參數時,請調用用戶窗體

[英]Call Userform when a certain parameter is not met using VBA

我目前正在處理一張excel工作表,如果If ... Then語句為true,則試圖在該工作表中基本上顯示一個用戶窗體。 我的“如果,則”語句基本上由大於最大值或小於最小值的數據點組成。

數據點滿足這些參數后,即要調用用戶窗體。 我試圖讓用戶窗體顯示一個數字條目,一旦輸入,它將確保它不大於最大值或小於最小值。 然后,如果它不大於最大值或小於最小值,那么我想提交在用戶輸入數據的單元格中輸入的那個數字。 我知道這似乎很簡單,但是我對VBA還是陌生的,我正在努力學習。 到目前為止,我已經開始設計UserForm。 所以我只輸入了文本和輸入框。 就是這樣。 感謝您的幫助!

這也是我在“ If,Then”語句中使用的代碼。 我曾經在發送電子郵件的地方收到過它。 因此,在“然后”一詞之后。 我使宏向該Excel工作表的所有者發送電子郵件。 我也在嘗試使用此代碼來制作UserForm:

Option Explicit


Public Sub OutofControl()
Dim lRow        As Long
Dim lstRow      As Long
Dim data As Variant
Dim ul As Variant
Dim ll As Variant
Dim wb As Workbook
Dim ws          As Worksheet


With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True


End With

Set ws = Sheets(2)
ws.Select


lstRow = WorksheetFunction.Max(1, ws.Cells(Rows.Count, "R").End(xlUp).Row)

For lRow = 1 To lstRow

data = Cells(lRow, "E").Value
ul = Range("K26")
ll = Range("K25")

    If data > ul Or data < ll Then

    If IsNumeric(data) = True And data Like "" = False Then

' Code for Sending Email after Then

現在,這是我做完一些研究后在工作表上進行選擇更改的代碼。 但是,我遇到了無限循環,並且“輸入”框沒有輸入我鍵入的數據。 此外,代碼中的“ Run OutofControl”行還指向另一個發出自動電子郵件的宏。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim lRow        As Long
Dim lstRow      As Long
Dim KeyCells As Range
Dim data As Variant
Dim ul As Variant
Dim ll As Variant
Dim ws          As Worksheet

Set ws = Sheets(2)
ws.Select

lstRow = WorksheetFunction.Max(1, ws.Cells(Rows.Count, "R").End(xlUp).Row)

For lRow = 1 To lstRow

data = Cells(lRow, "E").Value
ul = Range("K26")
ll = Range("K25")


    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("E:E")


    If (data > ul) Or (data < ll) Then

         Application.EnableEvents = False
    If IsNumeric(data) = True And data Like "" = False Then

     Run ("OutofControl") 'Macro
      Application.EnableEvents = False


        ' Display a message when one of the designated cells has been
        ' changed.

        Application.EnableEvents = True

        On Error GoTo 0

        MsgBox ("There was an Out of Control Point at " & Cells(lRow, "C").Value)
       Teststr = InputBox("Enter your Control data:")

         End If
    End If


    Next lRow



End Sub

使用InputBox更容易做到這一點!

TestStr = InputBox("Enter your data:")
'If... Then conditions
Range("Cell Name").Value = TestStr

然后根據您的參數測試該字符串。 編輯:更新為新的條件:

Dim StrPrompt As String
Dim TestStr As Long

StrPrompt = "How many data?"
redo:
TestStr = Application.InputBox(StrPrompt, "Enter an integer number (numbers will be rounded)", , , , , , Type:=1)
If TestStr > ul or lngNum < ll Then
    StrPrompt = "How many data - this must be between " & ll & " and " & ul
    GoTo redo
End If
Cells(lRow, "E").Value = TestStr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM