簡體   English   中英

將用戶表單輸入轉移到單元格區域

[英]Transfer Userform input to range of cells

我覺得我不得不尋求專家的幫助,因為我已經浪費了很多時間試圖解決這個問題,但沒有取得太大的成功。

我正在構建一個簡單的數據庫,在該數據庫中,用戶將必須基於下拉菜單輸入值。 唯一的例外是AB列 ,其中包含一個用戶表單,操作員必須在其中輸入兩個數值,即“次要發現”和“主要發現”,均介於1到25之間。

當前,一旦用戶選擇了特定范圍的單元格AB14:AB200 ,就會自動觸發用戶窗體

然后,用戶按下標題為“ 計算嚴重性”的按鈕,然后將其轉換為兩個值AIAJ

我面臨的問題如下:用戶可以從AB56單元格觸發用戶窗體 ,輸入兩個值,按calculare嚴重性 ,但是輸出將始終轉置到范圍的第一行(AI14和AJ14),而不是(AI56和AJ56)。

我已經附上了我的代碼示例以及數據庫的屏幕截圖。

數據庫屏幕快照

Private Sub Calculateseveritybutton_Click()

Worksheets("International CCU Tracker").Activate
Set xrg = Worksheets("International CCU Tracker").Range("AB14:AB200")

For Each xcell In xrg

' replace the sheet name and range A2 or B2 with yours
If Textbox1.Value = "0-25" And Textbox2.Value = "0-25" Then
MsgBox ("Please enter a Minor and Major finding value")

ElseIf Textbox1.Value <> "0-25" And Textbox2.Value = "0-25" Then
Sheets("International ccu tracker").Range("AI" & xcell.Row).Value = Textbox1.Value
MsgBox ("Please enter a Major finding value")

ElseIf Textbox1.Value = "0-25" And Textbox2.Value <> "0-25" Then
Sheets("International ccu tracker").Range("AJ" & xcell.Row).Value = Textbox2.Value
MsgBox ("Please enter a Minor finding value")


ElseIf Textbox1.Value <> "0-25" And Textbox2.Value <> "0-25" Then
Worksheets("International ccu tracker").Range("AI" & xcell.Row).Value = Textbox1.Value
Worksheets("International ccu tracker").Range("AJ" & xcell.Row).Value = Textbox2.Value
MsgBox ("Rating calculated")

Textbox3.Font.Size = 14
Textbox3.TextAlign = 2

End If
Next xcell
Exit Sub
End Sub

這是用戶窗體觸發代碼

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim question As Integer

    If Not Intersect(Target, Me.Range("Userformrange")) Is Nothing Then

        question = MsgBox("Would you like to add or edit a rating?", vbYesNo)
        If question = vbYes Then
            UserForm1.Show
        Else 
            Exit Sub
        End If
    End If
End Sub

未經測試:

工作表:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim rng As Range
    Set rng = Intersect(Target, Me.Range("Userformrange"))

    If Not rng Is Nothing Then
        If MsgBox("Would you like to add or edit a rating?", vbYesNo) = vbYes Then
            ' "TheCell" is a public global in your userform (or make a property Get/Let)
            Set UserForm1.TheCell = rng.Cells(1) 'only dealing with one row at a time...
            UserForm1.Show
        Else
            Exit Sub
        End If
    End If
End Sub

窗體:

Public TheCell As Range 'public variable in your form

Private Sub Calculateseveritybutton_Click()
    If Textbox1.Value = "0-25" Or Textbox2.Value = "0-25" Then
        MsgBox "Minor and Major finding values are both required!", vbExclamation
    Else
        With TheCell.EntireRow
            .Cells(1, "AI").Value = Textbox1.Value 'Cells is *relative* here...
            .Cells(1, "AJ").Value = Textbox2.Value
            MsgBox ("Rating calculated")
            Textbox3.Font.Size = 14
            Textbox3.TextAlign = 2
        End With
    End If
End Sub

暫無
暫無

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

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