2017-06-12 154 views
0

我目前所面临的问题如下数据:使用VBA如果再声明复制和粘贴与TextBox.Values

源数据目前看起来如下:

Value#1  Value#2 
10    AA 
11    AA 
12    AB 
13    AD 
1231   AA 
125   AB 
4312   AA 
12314   AA 

现在用户有多个用户窗体文本框在那里,他可以定义其中各个值是:

  • Textbox1的=限定的值的开始行
  • TextBox2中为=值#1
  • Textbox3 =柱的值#2栏
  • Textbox4在值#=标准2

现在我想实现以下;用户应该指定他的值#1所在的位置(在TextBox2中),然后他应该定义可以找到标准的列(TextBox3中的值#2),之后他应该定义应该过滤哪些标准。如果他选择在Textbox4输入“AB”,那么以下

所以必须出现在工作表中的第一个可用列:

Value#1  Value#2 
12    AB 
125   AB 

我当前的代码看起来是这样的,但我不断改变它并没有什么真正的工作(语法错误)。实际上,我确定Syntax的开头还是挺好的(?),但我不知道如何用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定义的东西。

+1

“......并没有什么真正的作品。” - 会发生什么,为什么它与你的期望不同?在您的文章中添加此信息。 – Cristina

+0

非常感谢您的建议。我希望我的编辑信息有帮助。 –

回答

0

关于你的代码的几个注意事项:

首先,你与C在该行For Each c In Sheets ("Table")...循环,但以下行要检查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 
+0

谢谢你的提示。真的很感激它。也感谢代码。我试过了,它什么都没做。没有错误消息,没有发生在工作表上。 –

+0

我已将2行代码添加到代码中以帮助您进行调试,请参阅您在即时窗口中获得的结果 –

+0

谢谢。我在代码中添加了两行,但仍然没有任何反应。 –