2017-06-12 167 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

謝謝。我在代碼中添加了兩行,但仍然沒有任何反應。 –