2017-09-05 38 views
1

美好的一天!在我的工作表中我有(1) textbox as TextBox11 button for submit button.我在這裏給出了分割文本作爲輸出的示例代碼。我只是希望如果textbox1中有重複的單詞,並且用戶輸入提交按鈕,它將保存到工作表(DatabaseStorage)和categorize the outputNo Duplicated Wordduplicated Word。因爲系統的某些功能需要這兩個不同的字段。數組插入重複的和不重複的數據到VBA中的不同列

This how the data input this is from SPLIT worksheet Expected output after submiting the submit button for Worksheet DatabaseStorage

Private Sub CommandButton1_Click() 
Call SplitText 
End Sub 
Sub SplitText() 
Dim WArray As Variant 
Dim TextString As String 
TextString = TextBox1 
WArray = Split(TextBox1, " ") 
If (TextString = "") Then 
MsgBox ("Error: Pls Enter your data") 
Else 


With Sheets("DatabaseStorage") 
    .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(UBound(WArray) + IIf(LBound(WArray) = 0, 1, 0)) = Application.Transpose(WArray) 
End With 

MsgBox ("Successfully inserted") 

End If 

End Sub 
+0

你可以提供更多關於如何在文本框中輸入數據的信息嗎?盒子裏每行有一個單詞嗎? – kschindl

+0

嗨,先生,我已經更新後檢查新圖像,你的答案是我需要的,但有一個意想不到的輸出列「沒有重複的單詞」我想單詞財務和刪除重複單詞財務的另一個詞。像上面的例子sir –

回答

1

這應該完成你所需要的。我遍歷數組以檢查給定值是否存在於「無重複」列中。如果沒有,請不要在那裏打印。

任何時候我遇到一種情況,我需要檢查列表中的單個值(例如檢查重複項,GT/LT等),我考慮循環。

Sub SplitText() 
Dim WArray As Variant 
Dim TextString As String 
Dim col_no_dup As Long 
Dim col_dup As Long 
Dim counter As Integer 
Dim sht_database As Worksheet 

With ThisWorkbook 
    Set sht_database = .Sheets("DatabaseStorage") 
    TextString = LCase(.Sheets("Sheet1").Shapes("Textbox1").DrawingObject.Text) 
End With 

WArray = Split(TextString, " ") 'load array 

If (TextString = "") Then 
    MsgBox ("Error: Pls Enter your data") 
    End 
Else: End If 

'set column locations for duplicates/no duplicates 
col_no_dup = 1 
col_dup = 2 

With sht_database 
    .Range("A2:B10000").ClearContents 'clear existing data. Change this as needed 

    'Print whole array into duplicates column 
    .Cells(Cells.Rows.Count, col_dup).End(xlUp).Offset(1, 0).Resize(UBound(WArray) + IIf(LBound(WArray) = 0, 1, 0)) = Application.Transpose(WArray) 

    'Loop through array 
    For i = LBound(WArray) To UBound(WArray) 
     counter = 0 
     lrow_no_dup = .Cells(Cells.Rows.Count, col_no_dup).End(xlUp).Row 
     For n = 1 To lrow_no_dup 'loop through and check each existing value in the no dup column 
      If .Cells(n, col_no_dup).Value = WArray(i) Then 
       counter = counter + 1 'account for each occurence 
      Else: End If 
     Next n 
     If counter = 0 Then 'counter = 0 implies the value doesn't exist in the "No Duplicates" column 
      .Cells(lrow_no_dup + 1, col_no_dup).Value = WArray(i) 
     Else: End If 
    Next i 

End With 

MsgBox ("Successfully inserted") 

End Sub 
+0

您好kschindl它像圖片中的工作,但沒有重複的輸出不節省財務。如果我輸入這個「財務我的問題是財務」,那麼我想保留在沒有重複財務示例的欄中,那麼在這句話中唯一將保存到數據庫的是「我的問題是財務」或「財務我的問題是「這樣的事情 –

+0

我的歉意,我的第一個答案是不完全正確的。我更新了代碼,所以它應該做你現在需要的。我用你的例子對它進行了測試。 – kschindl

+0

謝謝先生幫助我的幫助意味着很多,因爲我們正在爲我們的大學所做的這個系統感謝你很多 –