2016-12-17 28 views
0

我在下面的代碼中出現錯誤。我想要做的是在文本框中鍵入信息,然後當我轉到下一個框時,它會自動填充下一個文本框與我的數據庫的下一個單元格中的信息。任何事情都會有幫助。用戶表單代碼問題,代碼清理

Private Sub TextBox6_AfterUpdate() 

    If WorksheetFunction.CountIf(Mastor_list.Range(Columns("D:D")), Me.TextBox6.Value) = 0 Then 
     Me.TextBox6.Value = "" 
     Exit Sub 
    End If 

    With Me 
     .TextBox1 = Application.WorksheetFunction.VLookup(CLng(Me.TextBox6), Mastor_list.Range("Lookup"), 5, 0) 
     .TextBox2 = Application.WorksheetFunction.VLookup(CLng(Me.TextBox6), Mastor_list.Range("Lookup"), 6, 0) 
     .TextBox3 = Application.WorksheetFunction.VLookup(CLng(Me.TextBox6), Mastor_list.Range("Lookup"), 7, 0) 
     .TextBox4 = Application.WorksheetFunction.VLookup(CLng(Me.TextBox6), Mastor_list.Range("Lookup"), 8, 0) 
     .TextBox5 = Application.WorksheetFunction.VLookup(CLng(Me.TextBox6), Mastor_list.Range("Lookup"), 10, 0) 
    End With 
End Sub 

行,所以現在我的第一部分工作,但是當我試圖用我的VLOOKUP匹配的東西它不工作我得到醚語法錯誤或編譯錯誤:預期:語句錯誤的結束

Private Sub TextBox6_AfterUpdate() 

Sheets("Mastor list").Select 
ActiveSheet.Unprotect 
If WorksheetFunction.CountIf(Sheets("Mastor list").Range("D:D", Range("D:D").End(xlDown)), Me.TextBox6.Value) = 0 Then 

    Exit Sub 
End If 

With Me 
Sheets("Mastor list").Select 
    TextBox1.Text= "WorksheetFunction.VLookup(Me.TextBox6),Mastor list.Range(Mastor list.Columns("D:D")), 5, 0)" 

Sheets("Mastor list").Select 
ActiveSheet.Protect 


End Sub 
+1

請告訴我們你收到了什麼錯誤。 – tlemaster

+0

你的代碼是否到達With部分?換句話說'COUNTIF'是否返回其他那個零? –

+0

我會**猜**你在Mastor_list.Range(Columns(「D:D」))上出錯。它可能正在工作(也許如果'Mastor_list'是一個'Worksheet'對象,並且它指的是當前活動的工作表),但即使如此,它最好編碼爲Mastor_list.Range(Mastor_list.Columns(「D:D 「))'(或甚至是'Master_list.Columns(」D:D「)'或'Mastor_list.Range(」D:D「)'?)。 – YowE3K

回答

0

您指定範圍的方式不起作用。請參閱以下內容:

Private Sub TextBox6_AfterUpdate() 

    If WorksheetFunction.CountIf(Sheets("Mastor_list").Range("D1", Range("D1").End(xlDown)), Me.TextBox6.Value) = 0 Then 
     Me.TextBox6.Value = "" 
     Exit Sub 
    End If 

    With Me 
     .TextBox1 = WorksheetFunction.VLookup(CLng(Me.TextBox6), Sheets("Mastor_list").Range("Lookup"), 5, 0) 
     .TextBox2 = WorksheetFunction.VLookup(CLng(Me.TextBox6), Sheets("Mastor_list").Range("Lookup"), 6, 0) 
     .TextBox3 = WorksheetFunction.VLookup(CLng(Me.TextBox6), Sheets("Mastor_list").Range("Lookup"), 7, 0) 
     .TextBox4 = WorksheetFunction.VLookup(CLng(Me.TextBox6), Sheets("Mastor_list").Range("Lookup"), 8, 0) 
     .TextBox5 = WorksheetFunction.VLookup(CLng(Me.TextBox6), Sheets("Mastor_list").Range("Lookup"), 10, 0) 
    End With 
End Sub 

您將要包括一些錯誤處理,因爲如果你的任何VLookups都沒有找到匹配,錯誤將被拋出。