2016-05-14 10 views
0

現在、VBAを使用している間に1つのワークシートに売上を保存できるアプリケーションを作成しようとしています。VBA Vlookupで間違った結果が返される

私はVLOOKUPは常に「2015」

を同じ値を返します。自分自身に値を入力する必要がないように商品コードの価格を決定するためにVLOOKUPを使用しようとすると、私はそれが間違っ

起こっているのか見当もつかない

これは、シートのレイアウトです:Layout これは私のユーザーフォームのレイアウトです:Layout

そして、これは私が私のコマンドボタンの上に使うコードです:

Private Sub CommandButton1_Click() 

Dim emptyRow As Long 
Dim r As Range 

Dim Productprijs As Integer 
Dim productaantal As Integer 
Dim Eindprijs As Integer 



Sheet1.Activate 

emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1 

Cells(emptyRow, 1).Value = TextBox1.Value 
Cells(emptyRow, 2).Value = TextBox2.Value 
Cells(emptyRow, 3).Value = TextBox3.Value 
Cells(emptyRow, 5).Value = TextBox4.Value 

Productprijs = CInt(Application.VLookup(TextBox3.Value, "J2:L2000", 3, False)) 
productaantal = TextBox2.Value 
Eindprijs = Productprijs * productaantal 
Cells(emptyRow, 4).Value = Eindprijs 


UserForm1.Hide 

誰かが私の問題を助けてくれますか?私は現在見落としている小さなことかもしれません。点で

おかげで、 マルタイン

+2

Productprijs = CInt(Range( "L"&Application.Match(TextBox3.Value、 "J2L:L2000"、3、False))で 'Productprijs = CInt(Application.VLookup 、Range( "J:J")、0))) –

答えて

3

あなたのコードと2つの問題があります。 "J2:L2000"をRange( "J2:L2000")に置き換える必要があります(2015は2015エラーの整数バージョンです)

VlookupでTextbox3.Valueが見つからない場合、コードは機能しません。それがエラーを返しますケース:コードは、より多くのあなたが最初の引数としてTextBox3.Value内部VLookup機能を入れているので、あなたのコードは「2015エラー」投げている。この

Sub testv() 
    Dim v As Variant 
    Dim i As Long 
    v = Application.VLookup(9, Range("A1:a3"), 1, False) 
    If Not IsError(v) Then 
     i = CLng(v) 
    Else 
     MsgBox "not Found" 
    End If 
End Sub 
1

のようになります。次のコードは動作することに注意してください:

Private Sub CommandButton1_Click() 
    Dim emptyRow As Long 
    Dim Price As Variant 
    Dim Quantity As Double 
    Dim Total As Double 
    Dim x As Double 

    'This finds the next empty row in the first table 
    emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1 

    'Place the new values from the UserForm to the table 
    Cells(emptyRow, 1).Value = TextBox1.Value 'Date 
    Cells(emptyRow, 2).Value = TextBox2.Value 'Quantity 
    Cells(emptyRow, 3).Value = TextBox3.Value 'ProductID 
    Cells(emptyRow, 5).Value = TextBox4.Value 'Customer 

    'Assign the value of the ProductID text box to x 
    x = TextBox3.Value 

    'Calculate the total price, using x instead of TextBox3.Value 
    Price = Application.VLookup(x, Range("J2:L3"), 3, False) 

    Quantity = TextBox2.Value 
    Total = Price * Quantity 
    Cells(emptyRow, 4).Value = Total 

    UserForm1.Hide 
End Sub 

これもCIntを使用して、価格の変数を変換する必要がなくなります。うまくいけば、VLookupの中のTextBox3.Valueがなぜエラーを送出したのですか?

関連する問題