2016-09-02 9 views
0

オプション変数をtrueまたはfalseで使用しています(選択したケースを選択した場合はnExtendと表示されます)、そうでない場合はnExtendを追加します)。アプリケーション定義でエラーが発生します。どんな助けも、以下の完全なコードで大いに感謝されるでしょう。nオプションを使用していない場合は、文を使用しないでください。

Private Sub cmdAdd_Click() 


    Dim irow As Long 
    Dim lastRow As Long 
    Dim iCol As String 
    Dim c As Range 
    Dim ws As Worksheet 
    Dim value As Long 
    Dim NewPart As Boolean 
    Dim ws_warehouse(7) As Worksheet '7 is total warehouse tab you have 
    Dim nExtend As Integer 
    Dim cel As Range 

    Set ws = Worksheets("Main") 

    Set ws_warehouse(1) = Worksheets("Elkhart East") 
    Set ws_warehouse(2) = Worksheets("Tennessee") 
    Set ws_warehouse(3) = Worksheets("Alabama") 
    Set ws_warehouse(4) = Worksheets("North Carolina") 
    Set ws_warehouse(5) = Worksheets("Pennsylvania") 
    Set ws_warehouse(6) = Worksheets("Texas") 
    Set ws_warehouse(7) = Worksheets("West Coast") 

    Set c = ws.Range("A7:A1048576").Find(What:=Me.PartTextBox.value, SearchOrder:=xlRows, _ 
      SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole) 
    If c Is Nothing Then 
    'find first empty row in database 
     lastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count 
     irow = lastRow + 1 
     NewPart = True 
    Else 
    'find row where the part is 
     irow = ws.Cells.Find(What:=Me.PartTextBox.value, SearchOrder:=xlRows, _ 
      SearchDirection:=xlPrevious, LookIn:=xlValues).Row 
     NewPart = False 
    End If 

    'check for a part number 
    If Trim(Me.PartTextBox.value) = "" Then 
     Me.PartTextBox.SetFocus 
     MsgBox "Please Enter A Part Number" 
     Exit Sub 
    End If 

    If Trim(Me.MonthComboBox.value) = "" Then 
     Me.MonthComboBox.SetFocus 
     MsgBox "Please Enter A Month" 
     Exit Sub 
    End If 

    If Trim(Me.AddTextBox.value) = "" Then 
     Me.AddTextBox.SetFocus 
     MsgBox "Please Enter A Value To Add Or Substract" 
     Exit Sub 
    End If 




    For Each Cel2 In .Cells(irow, iCol).Resize(, nExtend) 
     If Onetimeoption.value = True Then 
      Select Case MonthComboBox.value 
       Case "Current Month" 
        iCol = "C" 
       Case "Current Month +1" 
        iCol = "N" 
       Case "Current Month +2" 
        iCol = "O" 
       Case "Current Month +3" 
        iCol = "P" 
       Case "Current Month +4" 
        iCol = "Q" 
      End Select 
     Else 
      nExtend = 1 'Set this as a default. 
      Select Case MonthComboBox.value 
       Case "Current Month" 
        iCol = "C" 

       Case "Current Month +1" 
        iCol = "N" 
        nExtend = 4 
       Case "Current Month +2" 
        iCol = "O" 
        nExtend = 3 
       Case "Current Month +3" 
        iCol = "P" 
        nExtend = 2 
       Case "Current Month +4" 
        iCol = "Q" 
      End Select 
     End If 

     actWarehouse = Me.warehousecombobox.ListIndex + 1 
     With ws 
      .Cells(irow, "A").value = Me.PartTextBox.value 

      For Each Cel1 In .Cells(irow, iCol).Resize 
       For Each Cel2 In .Cells(irow, iCol).Resize(, nExtend) 
        Cel1.value = Cel2.value + CLng(Me.AddTextBox.value) 
        Cel1.Interior.ColorIndex = 6 
       Next Cel2 
      Next Cel1 
     End With 

    With ws_warehouse(actWarehouse) 
     'find part number 
     l_row = .Range("A" & .Rows.Count).End(xlUp).Row 

     NewPart = True 
     For r = 7 To l_row 
      If Trim(.Range("A" & r)) = "" Then Exit For 
      If LCase(.Range("A" & r)) = LCase(Me.PartTextBox.Text) Then 
       irow = r 
       Exit For 
       NewPart = False 
      End If 
     Next r 
     If NewPart Then irow = r 

     .Cells(irow, "A").value = Me.PartTextBox.value 
     For Each cel In .Cells(irow, iCol).Resize(, nExtend) 
      cel.value = cel.value + CLng(Me.AddTextBox.value) 
      cel.Interior.ColorIndex = 6 
     Next cel 
    End With 

    'clear the data 
    Me.PartTextBox.value = "" 
    Me.MonthComboBox.value = "" 
    Me.AddTextBox.value = "" 
    Me.PartTextBox.SetFocus 
    Me.warehousecombobox.value = "" 

End Sub 

Private Sub cmdClose_Click() 
    Unload Me 
End Sub 

Private Sub UserForm_Initialize() 

    'Empty NameTextBox 
    PartTextBox.value = "" 

    'Empty PhoneTextBox 
    AddTextBox.value = "" 

    'Empty DinnerComboBox 

    'Fill DinnerComboBox 
    With MonthComboBox 
     .AddItem "Current Month" 
     .AddItem "Current Month +1" 
     .AddItem "Current Month +2" 
     .AddItem "Current Month +3" 
     .AddItem "Current Month +4" 

    End With 

    With warehousecombobox 
     .AddItem "Elkhart East" 
     .AddItem "Tennessee" 
     .AddItem "Alabama" 
     .AddItem "North Carolina" 
     .AddItem "Pennsylvania" 
     .AddItem "Texas" 
     .AddItem "West Coast" 
    End With 

End Sub 
+0

これは正確には何をする*と思われますか?あなたは2 *同一* 'ケース選択 'ブロックを持っています。 – Comintern

+0

_アプリケーション定義でエラーが発生しました_あなたは黙認できますか?エラーコードとエラーメッセージは何ですか? – litelite

+0

@Comintern選択ケースの1つにnExtendが含まれていますが、 – Luis

答えて

0

あなたは.Cells(...)を使用しているが、それはWithブロックではありませんが、式はドットで始まる(。)または感嘆符(!)ブロックとの外側に表示することはできません。 Cells(...)(先行ドットなし)またはActiveSheet.Cells(...)のいずれかを使用する必要があります。どちらも同等です。

関連する問題