首页 > 解决方案 > 更新对工作表上已编辑数据的更改不起作用

问题描述

我正在尝试编辑工作表中文本框中显示的数据,我可以执行所有其他必要的操作,例如添加数据,但是我附加的代码不会使用更改更新工作表,我尝试使用 offset(0,1)但所做的只是用它正在搜索的 ID 号更改一行。Box1 是一个带有 ID 号的文本框,它是我的程序运行的主要标准。

我对 VBA 比较陌生,并试图寻找解决方案,但现在我很茫然,没有更改我的整个代码,只是为了解决可能对比我更有经验的人来说是一个简单的解决方案。

Dim findvalue As Range

Set findvalue = Sheet1.Range("A:A").Find(What:=Box1, LookIn:=xlValues)

cNum = 20
    For X = 1 To cNum
        findvalue = Me.Controls("Box" & X).Value(0, 1)
        Set findvalue = findvalue(0, 0)
    Next

标签: excelvba

解决方案


这对我来说没有太大意义,但是如果您有 20 个文本框(1 到 20),您可以使用以下语法将存储在文本框中的值写入 ID 所在范围内。我希望你能更接近解决你的问题。

    Sub makeAndboxes()
    Dim j As Integer
    Dim tbox As MSForms.TextBox
    'For the test creates 20 Textboxes with the name and value Box1, Box2, ..Box20.
    'The same text is in the range("A:A") somewhere to test
    For j = 1 To 20
        Set tbox = UserFormBX.Controls.Add("Forms.Textbox.1", "Box" & j)
        tbox.Value = tbox.Name 'Write the name in the box
    Next j

    Dim findvalue As Range
    'If it is the "A:A" range, it will be the cell where it is found
    Set findvalue = Sheet3.Range("A:A").Find(What:=UserFormBX.Controls("Box1").Value, LookIn:=xlValues)
    If findvalue Is Nothing Then
        MsgBox "Value: " & UserFormBX.Controls("Box1").Value & " not in Range(A:A)"
        Exit Sub
    End If
    Dim cNum As Integer
    Dim x As Integer
    cNum = 20
    For x = 1 To cNum
        findvalue.Value = UserFormBX.Controls("Box" & x).Value & " New text"
        Set findvalue = findvalue.Offset(1, 0) 'next row
    Next
End Sub

推荐阅读