通过用户表单更新单元格的数据

Updating a a cell's data through means of a Userform

所以我有2组数据: 1)消费金额 2)卡号。

在 a1 和 b1

我想创建一个包含 2 个字段的用户表单,它们将执行以下操作:

字段号1:用户输入卡号,系统会在a栏中查找对应的卡号。

字段号2:用户输入消费金额,系统将使用字段号1中对应的卡号,将字段号2中输入的金额添加到现有值。

如何做到这一点?

虽然使用文本框可以找到相应的值,但组合框会更好地减少输入错误卡号的机会。

在用户窗体模块中使用此代码。

Private Sub CommandButton1_Click()
    Dim Rws As Long, Rng As Range, sh1 As Worksheet, r As Range
    Set sh1 = Worksheets("Sheet1")
    With sh1
        Rws = .Cells(Rows.Count, "D").End(xlUp).Row
        Set Rng = .Range(.Cells(5, "D"), .Cells(Rws, "D"))
    End With
    Set r = Rng.Find(what:=ComboBox1, lookat:=xlWhole)
    If r.Offset(, -1) <> "" Then
        r.Offset(, -1) = r.Offset(, -1) + TextBox1.Value
    Else: r.Offset(, -1) = TextBox1.Value
    End If

End Sub

Private Sub UserForm_Initialize()
    Dim Rws As Long, Rng As Range, sh1 As Worksheet
    Set sh1 = Worksheets("Sheet1")
    With sh1
        Rws = .Cells(Rows.Count, "D").End(xlUp).Row
        Set Rng = .Range(.Cells(5, "D"), .Cells(Rws, "D"))
    End With
    ComboBox1.List = Rng.Value

End Sub