Excel 更新对工作表上未工作的已编辑数据的更改
我正在尝试编辑工作表文本框中显示的数据,我可以执行所有其他必要的操作,例如添加数据,但是我附加的代码不会随更改而更新工作表,我尝试使用偏移量(0,1),但所做的只是更改一行,并将其搜索的ID号作为一行。 Box1是一个文本框,其中有一个ID号,是我的程序运行的主要条件 我对VBA比较陌生,曾尝试过寻找解决方案,但现在我不知所措,没有改变我的全部代码,只是为了解决比我更有经验的人可能会发现的简单解决方案Excel 更新对工作表上未工作的已编辑数据的更改,excel,vba,Excel,Vba,我正在尝试编辑工作表文本框中显示的数据,我可以执行所有其他必要的操作,例如添加数据,但是我附加的代码不会随更改而更新工作表,我尝试使用偏移量(0,1),但所做的只是更改一行,并将其搜索的ID号作为一行。 Box1是一个文本框,其中有一个ID号,是我的程序运行的主要条件 我对VBA比较陌生,曾尝试过寻找解决方案,但现在我不知所措,没有改变我的全部代码,只是为了解决比我更有经验的人可能会发现的简单解决方案 Dim findvalue As Range Set findvalue = Sheet1.
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
像这样的方法应该会奏效:
Dim f As Range, x As Long
'always use "lookat" if you want an exact match
Set f = Sheet1.Range("A:A").Find(What:=Box1, LookIn:=xlValues, _
lookat:=xlWhole)
If Not f Is Nothing Then
For x = 2 To 20 '<< no need to replace the value you just found
Debug.Print "Setting " & f.Offset(0, x - 1).Address & " to value '" & _
Me.Controls("Box" & x).Value & "' from 'Box" & x & "'"
f.Offset(0, x - 1).Value = Me.Controls("Box" & x).Value
Next
Else
MsgBox "Id not found!"
End If
尺寸f为范围,x为长度
如果你想要精确匹配,请始终使用“注视”
设置f=Sheet1.Range(“A:A”).Find(What:=Box1,LookIn:=xlValues_
注视:=xlother)
如果不是的话,那么f什么都不是
对于x=2到20',类似的方法应该可以工作:
Dim f As Range, x As Long
'always use "lookat" if you want an exact match
Set f = Sheet1.Range("A:A").Find(What:=Box1, LookIn:=xlValues, _
lookat:=xlWhole)
If Not f Is Nothing Then
For x = 2 To 20 '<< no need to replace the value you just found
Debug.Print "Setting " & f.Offset(0, x - 1).Address & " to value '" & _
Me.Controls("Box" & x).Value & "' from 'Box" & x & "'"
f.Offset(0, x - 1).Value = Me.Controls("Box" & x).Value
Next
Else
MsgBox "Id not found!"
End If
尺寸f为范围,x为长度
如果你想要精确匹配,请始终使用“注视”
设置f=Sheet1.Range(“A:A”).Find(What:=Box1,LookIn:=xlValues_
注视:=xlother)
如果不是的话,那么f什么都不是
对于x=2到20',这对我来说没有太大意义,但是如果您有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
这对我来说没有太大意义,但是如果您有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
请澄清:您使用的是工作表
还是用户表单
文本框?要将每个文本框中的数据添加到何处?您的文本框名称是否正确,例如“Box1”、“Box2”或“TextBox 1”等。您尝试完成的示例/屏幕截图将非常有用。您好,GMalc我使用的是一个用户表单,其中填充了从工作表中的20列到20个控件的数据,一些文本框和一些组合框&试图编辑任何具有不同数据的控件请澄清:您使用的是工作表
还是用户表单
文本框?要将每个文本框中的数据添加到何处?您的文本框名称是否正确,例如“Box1”、“Box2”或“TextBox 1”等。您尝试完成的示例/屏幕截图将非常有用。您好,GMalc我使用的是一个用户表单,其中填充了从工作表中的20列到20个控件的数据,一些textbox和一些combobox&试图用不同的数据编辑这些控件中的任何一个hi Tim这几乎成功了,但是它只更改了第2列中的数据,没有更改任何其他数据,也就是说,col2是名col3是姓这只是更改了名而不是姓。您需要调试并找出问题所在-我在上面添加了一个调试。打印语句-看看这给您带来了什么Hi Tim这几乎奏效了,但是它只更改了第2列中的数据,没有更改任何其他数据,也就是说,col2是名字col3是姓氏这只是更改了名字而不是姓氏。您需要调试并找出问题所在-我在上面添加了一个debug.Print语句-看看这会给您带来什么