2016-10-07 4 views
0

以下は私のコードの問題な部分です。コードはInputCodeを使ってProductCodeとQuantityを取り込みますが、ProductCodeが入力されると、その値の横にある "Cost"と "Discount"を定義して最終的に表示することもできます入力ボックス。どんな助けでも大いに感謝します。VBAで複数のセル/範囲を宣言/定義して同時に選択するにはどうすればよいですか?

'Obtaining VLookup Value 
ProductCode = InputBox("Enter the ProductCode's code.") 

'Error checking 
Do Until ErrorCheck = False 
    If ProductCode = "" Then 
     ErrorCheck = True 
     MsgBox ("Not a valid entry.") 
     ProductCode = InputBox("Enter the ProductCode's code.") 
     Cost = ActiveCell.Offset(0, 1).Select 
     MinQty = ActiveCell.Offset(0, 2).Value 
     Discount = ActiveCell.Offset(0, 3).Value 
    ElseIf IsError(Application.VLookup(ProductCode, myRange, 3, False)) Then 
     ErrorCheck = True 
     MsgBox ("The value entered was not found.") 
     ProductCode = InputBox("Enter the ProductCode's code.") 
    Else 
     ErrorCheck = False 
    End If 
Loop 

答えて

0

あなたの物語は非常に明確ではありませんし、また、あなたのコードの一部と競合するが、私はOはそうあなたがこの(コメント)を試してみて適合させることができる全体的なアイデア

だと思うコード

Option Explicit 

Sub Product() 
    Dim ProductCode As String 
    Dim ErrorCheck As Boolean 
    Dim Cost As Double, MinQty As Double, Discount As Double 
    Dim MyRange As Range 
    Dim found As Variant 

    Set MyRange = Range("A1:A10") '<-- change it to your actual "MyRange" setting 

    Do '"main" outer loop 
     Do '"Product code input" inner loop 
      ProductCode = Application.InputBox("Enter the ProductCode's code.", Type:=2) '<--| force string input 
     Loop While ProductCode = "" 

     found = Application.Match(ProductCode, MyRange.Columns(1), 0) '<-- try getting ow index of prodcut code in 1st column of "MyRange" range 
     If IsError(found) Then '<--| if no match found... 
      MsgBox "The value entered was not found!" & vbCrLf & vbCrLf & "Please, try again", vbCritical + vbOKOnly '<-- inform the user and loop again 
     Else '<--| otherwise 
      With MyRange(found, 1) '<-- reference the matching cell 
       Cost = .Offset(0, 1).Value '<--| store "Cos"t from cell 1 column to the right of the referenced one 
       MinQty = .Offset(0, 2).Value '<--| store "MinQty" from cell 2 columns to the right of the referenced one 
       Discount = .Offset(0, 3).Value '<--| store "Discount" from cell 3 columns to the right of the referenced one 
      End With 
     End If 
    Loop While IsError(found) 
End Sub 
+0

ありがとうございます。これはすごくうまくいった。また、Forループに自分のコード全体を配置して、購入したさまざまな製品の数をユーザーに問い合わせる方法は?つまり、ループを通過するたびに、購入した特定の製品に関する情報が得られます。 –

+0

ようこそ。このサイトのルールに従って、1)あなたのコードの試行で新しい問題に新しい問題を投稿する必要があります。2)答えの横にあるチェックマークをクリックして、自分の答えを灰色で塗りつぶして表示してください。 – user3598756

関連する問題