简体   繁体   中英

Transfer Userform input to range of cells

I felt like I had to ask help from experts as I've been losing quite a lot of time trying to figure this out without much success.

I'm in the midst of building a simple database where users will have to enter values based on drop-down menus. The exception to that is, column AB which contains a userform in which the operator will have to enter two numerical values ie "Minor findings" & "Major findings", both ranging from 1 to 25.

Currently, the userform is automatically triggered as soon as the user selects a specific range of cells AB14:AB200

The user then presses a button entitled caclculate severity which is supposed to then transpose the two values onto the two columns AI & AJ

The issue I'm facing is the following: The user can trigger the userform from cell AB56 , enter the two values, press calculare severity , but the output will always be transposed onto the first rows of the range (AI14 & AJ14) instead of (AI56 & AJ56).

I've attached a sample of my code along with a screenshot of the database.

数据库屏幕快照

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

This is the Userform trigger code

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

Untested:

Worksheet:

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

Userform:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM