我觉得我不得不寻求专家的帮助,因为我已经浪费了很多时间试图解决这个问题,但没有取得太大的成功。
我正在构建一个简单的数据库,在该数据库中,用户将不得不基于下拉菜单输入值。唯一的例外是列AB ,其中包含一个用户表单,操作员必须在其中输入两个数值,即“次要发现”和“主要发现”,均介于1到25之间。>
当前,一旦用户选择了特定范围的单元格,就会自动触发用户窗体 AB14:AB200
然后,用户按下标题为“ 精确度”的按钮,该按钮随后应将两个值转换为两个列 AI 和 AJ < / p>
我面临的问题如下:用户可以从单元格 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
答案 0 :(得分:0)
未经测试:
工作表:
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