[英]Using VBA If then statement to copy and paste data with TextBox.Values
我目前面临以下问题:
当前的源数据如下所示:
Value#1 Value#2
10 AA
11 AA
12 AB
13 AD
1231 AA
125 AB
4312 AA
12314 AA
现在用户有多个用户窗体文本框,他可以在其中定义各个值的位置:
现在我要实现以下目标; 用户应指定其值#1的位置(在TextBox2中),然后他应定义可在其中找到标准的列(在TextBox3中的值#2),然后,他应定义应过滤的条件。
因此,如果他选择在Textbox4中键入“ AB”,则必须在工作表的第一个可用列中显示以下内容:
Value#1 Value#2
12 AB
125 AB
我当前的代码看起来像这样,但是我不断地对其进行更改,但实际上没有任何效果(语法错误)。 我实际上可以确定语法的开头还可以(?),但是我不知道如何用vba表示我希望他们在条件匹配的情况下将列中的值复制到另一个中。 这里和其他地方都有很多示例,但是我找不到范围或值未预定义的任何内容。
Dim c as Range
If OptionButton1.Value = True Then 'also this only happens if the user decides that an Optionbutton is true
For Each c In Sheets ("Table") Range (TextBox3.Value & TextBox1.Value + 1 & ":" & TextBox3.Value & lastrow)
If cell.Value = TextBox4.Value Then
Range (TextBox2.Value & TextBox1.Value + 1 & ":" & TextBox2.Value & lastrow) c.copy c.Offset (, 1) 'syntax-error
End If
Next
End If
我对VBA和整个编程还很陌生,因此找不到解决方案。
通过一些研究,我很确定解决此问题的语法有点像“对于每个“ x””等。但是我从未发现使用TextBox定义范围和值的东西。
有关您的代码的一些注意事项:
首先,在C
For Each c In Sheets ("Table")...
行中使用C
循环,但在下面的行中检查的是If cell.Value = TextBox4.Value
,而不是If C.Value = TextBox4.Value
。
其次,如果要在等于TextBox4.Value
情况下复制该单元格到下一列,请使用C.Copy C.Offset(, 1)
。
请尝试以下代码:
Dim Rng As Range
Dim C As Range
If OptionButton1.Value = True Then 'also this only happens if the user decides that an Optionbutton is true
' set the range fisrt , See if you are getting the Error on this line -->
Set Rng = Sheets("Table").Range(TextBox3.Value & TextBox1.Value + 1 & ":" & TextBox3.Value & lastrow)
For Each C In Rng
' for DEBUG ONLY
Debug.Print C.Value & " | TextBox4 value: " & TextBox4.Value
If C.Value = TextBox4.Value Then
MsgBox "There is a match" ' <-- for DEBUG ONLY
C.Copy C.Offset(, 1) ' copy the value from the cell into the next column to the right
End If
Next C
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.