2016-04-15 9 views
0

コンボボックスを作成しました。現在の式は、列Cに与えられた同じ値を複製し、列Rを追加したい追加の列を追加したいという点を除いて機能します。コンボボックスの値を複数の列に追加する

Ex。 ComboBox 現在の月の選択 検索された部分に基づいて、列Cと列Rに500単位を追加したいとします。

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 
Set ws = Worksheets("Summary") 

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 



Select Case MonthComboBox.value 

    Case "Current Month" 

     iCol = "C" And "R" 

    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 
value = Cells(irow, iCol).value 
With ws 


    .Cells(irow, iCol).value = value + CLng(Me.AddTextBox.value) 


End With 

If NewPart = True Then 
    ws.Cells(irow, "A").value = Me.PartTextBox.value 
End If 


If NewPart = True Then 
ws.Cells(irow, "C").value = Me.AddTextBox.value 
End If 

答えて

1

列を格納するために配列を使用することをお勧めします。

Sub t() 
Dim iCol() 
Dim testStr$, myValue$ 
Dim iRow& 
Dim ws As Worksheet 
testStr = "Current Month" 

Select Case testStr 
    Case "Current Month" 
     iCol() = Array("C", "R") 
    Case "Current Month +1" 
     iCol() = Array("N") 
    End Select 

Dim i& 
For i = LBound(iCol) To UBound(iCol) 
    myValue = Cells(iRow, iCol(i)).value ' WHAT SHEET IS THIS ON?? 
    With ws 
     .Cells(iRow, iCol(i)).value = myValue + CLng(Me.AddTextbox.value) 
    End With 
Next i 

End Sub 

、必要に応じてCaseに追加することができます。列の操作が完了したらNext iをラップする必要があるので、2番目に実行する列があるかどうかを確認できます。

また、すべてのコードが含まれていないため、範囲を調整する必要があります。 (myValueにはCells()のシートが指定されていないことに注意してください)。

+0

私は完全なコードを添付しています – Luis

+0

@ルイス -​​ 私の例のように、あなたのコードを更新して配列を含めることができます。 'With ws'ステートメントを終了する' End With'の後に 'Next i'を追加するだけです。あるいは、 'If NewPart'セクションの後に、各カラムがそれに影響するかどうかを指定します。それは理にかなっていますか? – BruceWayne

関連する問題