在用户窗体中编辑电子表格上的文本

Editing text on a spreadsheet within a UserForm

我有以下代码,它允许我根据两个组合框的值 select 从电子表格中输入文本。

问题是;在用户表单上进行编辑时如何在电子表格上进行编辑?

Private Sub CommandButton1_Click()
    Dim s As Worksheet
    Set s = ActiveSheet ' use for active sheet
    'Set s = Worksheets("Sheet1") ' use for a specific sheet
    For row = 1 To s.Cells(s.Rows.Count, 1).End(xlUp).row
      If s.Cells(row, "A").Value = ComboBox1.Value And _
          s.Cells(row, "B").Value = ComboBox2.Value Then
          TextBox1.Value = s.Cells(row, "C").Value
          TextBox2.Value = s.Cells(row, "D").Value
          TextBox3.Value = s.Cells(row, "E").Value
          TextBox4.Value = s.Cells(row, "F").Value
      End If
    Next
End Sub

假设“行”是一个 module-level 变量(在模块顶部声明)这就是您所需要的,假设您有一个名为 CommandButton2 的按钮,您希望单击它来保存数据:

Private Sub CommandButton2_Click()
  s.Cells(row, "C").Value = TextBox1.Value
  s.Cells(row, "D").Value = TextBox2.Value
  s.Cells(row, "E").Value = TextBox3.Value
  s.Cells(row, "F").Value = TextBox4.Value
End Sub

在范围上绑定用户窗体控件

可以使用ControlSource属性一个控件。 这会将其绑定在电子表格范围内。用户窗体控件上的所有更改将直接进入 bindet range.That 也将在相反的方向上工作。

使用方法

创建一个空的用户窗体。 在其中放置两个文本框控件 (TextBox1 and TextBox2)。 将两者的 ControlSource 属性 设置为目标范围。

请记住,您将 string 分配给了 ControlSource。因此,您可以在运行时建立它的价值。

Private Sub UserForm_Initialize()
    ' Set the Target Range to bind the control to
    TextBox1.ControlSource = "Table1!" & "A1"
    TextBox2.ControlSource = "Table2!A1"
End Sub

问题用例

这是适合您的问题示例的代码。 把它放到一个空的用户表单中

Option Explicit


'
'   Create an empty userbox with the following Controls
'
'   ComboBox1 as ComboBox
'   ComboBox2 as ComboBox
'
'   TextBox 1 as TextBox
'   TextBox 2 as TextBox
'   TextBox 3 as TextBox
'   TextBox 4 as TextBox



Private Sub ComboBox1_Change()
    Dim lngTargetRow As Long
    lngTargetRow = FindTargetRow()
    Call SetTextBoxControlSources(lngTargetRow)
End Sub
Private Sub ComboBox2_Change()
    Dim lngTargetRow As Long
    lngTargetRow = FindTargetRow()
    Call SetTextBoxControlSources(lngTargetRow)
End Sub

' Finds the Target Row regarding the values of both comboboxes
Private Function FindTargetRow()
    Dim s As Worksheet
    Set s = ActiveSheet
    Dim Row As Long
    Dim RetVal As Long: RetVal = -1
    For Row = 1 To s.Cells(s.Rows.Count, 1).End(xlUp).Row
      If s.Cells(Row, "A").Value = ComboBox1.Value And _
          s.Cells(Row, "B").Value = ComboBox2.Value Then
          RetVal = Row
          Exit For
        End If
    Next
    FindTargetRow = RetVal
End Function


Private Function SetTextBoxControlSources(lngTargetRow As Long)
    If (lngTargetRow <= 0) Then
        ' No Match, No Binding
        TextBox1.ControlSource = ""
        TextBox2.ControlSource = ""
        TextBox3.ControlSource = ""
        TextBox4.ControlSource = ""
        TextBox1.Text = "No Match"
        TextBox2.Text = "No Match"
        TextBox3.Text = "No Match"
        TextBox4.Text = "No Match"
    Else
        ' Set the Target Range to bind the control to
        TextBox1.ControlSource = "Table1!C" & lngTargetRow
        TextBox2.ControlSource = "Table1!D" & lngTargetRow
        TextBox3.ControlSource = "Table1!E" & lngTargetRow
        TextBox4.ControlSource = "Table1!F" & lngTargetRow
    End If
End Function

将文本框值分配给范围值

如果你只想进入一个方向,你可以将用户表单 textbox.text 属性 分配给目标 range.value.

Activesheet.Range("a1").value = TextBox1.text

这只是从用户表单进入在电子表格上制作的 spreadsheet.Changes 不会更新回用户表单文本框。

使用答案开头描述的 ControlSource 方法可以让您远离编写自己的更新方法,从而帮助您保持代码的可读性。