用户案例中基于案例的If语句

时间:2019-05-28 14:02:48

标签: excel vba combobox case

方案1:如果组合框2显示的是当前 工作表MRFL Range(A1:A250)和组合框1显示与工作表MFRL Range(B1:B250)中相同的文本/值,然后仅更改与工作表MFGLR中组合框2值在同一行中的AE列的值。

方案2:如果组合框2在工作表MRFL Range(A1:A250)中当前显示相同的文本/值,而组合框1在工作表MFRL Range(B1:B250)中显示不同的文本,则将combobox1和在工作表MFRL中下一个可用行的combobox2,B列中的Combobox1和A列中的Combobox2。然后仅更改与工作表MFGLR中的combobox 2值相同的行中的AE列的值。

方案3:如果组合框2当前在工作表MRFL范围(A1:A250)中显示了不同的文本/值,而组合框1在工作表MFRL范围(B1:B250)中显示了不同的文本,则将combobox1和在工作表MFRL的下一个可用行的combobox2,在工作表CT的A列的下一个可用行添加combobox2值,并在工作表MFGLR的B列中的Combobox1和列的Combobox2的下一个可用行中添加combobox2值A.然后仅更改与工作表MFGLR中的组合框2值在同一行中的AE列的值。

对于场景1和2,我很难让代码找到与combobox2值相同的行,然后将textbox1值粘贴到右侧的31列。这就是我到目前为止所拥有的。

With Worksheets("MFGLR").Range("a1:a500")
Set C = .Find(ComboBox2.Value, LookIn:=xlValues)
If Not C Is Nothing Then
firstAddress = C.Address
Do
    C.Value = TextBox1.Value
    Set C = .FindNext(C)
Loop While Not C Is Nothing
End If
End With
  1. 这些情况似乎并非在所有情况下都成立
  2. 我希望CT的边界从最后一列A移到V列
  3. 我希望MFRL的边界从最后一列A移到E列
  4. 我希望MFRL的边界从最后一列A移到AM列
  5. 从MFGLR AF列的最后一行向下填充到AH列

这是我所有带有边框和填充的方案的在制品。

Option Explicit
Private Sub CommandButton1_Click()

Dim ColA As New Scripting.Dictionary  'Need Microsoft Scripting Runtime Reference
Dim ColB As New Scripting.Dictionary
Dim LastRow As Long
Dim Criteria1 As Boolean
Dim Criteria2 As Boolean
Dim C As Range



With ThisWorkbook.Sheets("MFRL")
    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'This gets the last row on column A
    For Each C In .Range("A1:A" & LastRow) 'loop through the whole column
    On Error Resume Next
        'If you have duplicated entries this will throw an error
        ColA.Add C.Value, C.Row 'add the values from column A to DictA, also store it's row for later purposes
        ColB.Add C.Offset(0, 1).Value, C.Row 'add the values from column B to DictB, also store it's row for later purposes
    Next C
    'Criterias will give value of True if matched or False if not
    Criteria1 = ColA.Exists(ComboBox2.Value) 'this is getting matched with ColA Dictionary
    Criteria2 = ColB.Exists(ComboBox1.Value) 'this is getting matched with ColB Dictionary
    If Criteria1 And Criteria2 Then 'SCENARIO 1
        .Cells(ColA(ComboBox2.Value), "AE:100") = TextBox1.Value
    ElseIf Criteria1 And Not Criteria2 Then 'SCENARIO 2
        .Cells(LastRow + 1, 1) = ComboBox2.Value
        .Cells(LastRow + 1, 2) = ComboBox1.Value
        .Cells(ColA(ComboBox2.Value), "AE") = TextBox1.Value
    With Worksheets("MFRL")
        .Cells(Rows.Count, "B").End(xlUp).Offset(-1, 1).Resize(, 3).AutoFill .Cells(Rows.Count, "B").End(xlUp).Offset(-1, 1).Resize(, 3).Resize(2)
        .Cells(Rows.Count, "B").End(xlUp).Offset(-1, 1).Resize(, 3).Resize(2).Borders.LineStyle = xlContinuous
        End With
    ElseIf Not Criteria1 And Not Criteria2 Then 'SCENARIO 3
        .Cells(LastRow + 1, 1) = ComboBox2.Value
        .Cells(LastRow + 1, 2) = ComboBox1.Value
        'Add data to the column A next blank row in sheet CT
        LastRow = ThisWorkbook.Sheets("CT").Cells(ThisWorkbook.Sheets("CT").Rows.Count, 1).End(xlUp).Row + 1
        ThisWorkbook.Sheets("CT").Cells(LastRow, 1) = ComboBox2.Value
        ThisWorkbook.Sheets("CT").Cells(Rows.Count, "A").End(xlUp).Offset(-1, 1).Resize(, 21).AutoFill .Cells(Rows.Count, "A").End(xlUp).Offset(-1, 1).Resize(, 21).Resize(2)
        ThisWorkbook.Sheets("CT").Cells(Rows.Count, "A").End(xlUp).Offset(-1, 1).Resize(, 21).Resize(2).Borders.LineStyle = xlContinuous
        'Add data to the column A next blank row in sheet MFGLR
        LastRow = ThisWorkbook.Sheets("MFGL R").Cells(ThisWorkbook.Sheets("MFGLR").Rows.Count, 1).End(xlUp).Row + 1
        ThisWorkbook.Sheets("MFGLR").Cells(LastRow, 1) = ComboBox2.Value
        ThisWorkbook.Sheets("MFGLR").Cells(LastRow, "AE") = TextBox1.Value
        ThisWorkbook.Sheets("MFGLR").Cells(Rows.Count, "AE").End(xlUp).Offset(-1, 1).Resize(, 3).AutoFill .Cells(Rows.Count, "AE").End(xlUp).Offset(-1, 1).Resize(, 3).Resize(2)
        ThisWorkbook.Sheets("MFGLR").Cells(Rows.Count, "A").End(xlUp).Offset(-1, 1).Resize(, 38).Resize(2).Borders.LineStyle = xlContinuous
        ThisWorkbook.Sheets("MFRL").Cells(LastRow, 1) = ComboBox2.Value
        ThisWorkbook.Sheets("MFRL").Cells(LastRow, 2) = ComboBox1.Value
        ThisWorkbook.Sheets("MFRL").Cells(Rows.Count, "B").End(xlUp).Offset(-1, 1).Resize(, 3).AutoFill .Cells(Rows.Count, "B").End(xlUp).Offset(-1, 1).Resize(, 3).Resize(2)
        ThisWorkbook.Sheets("MFRL").Cells(Rows.Count, "B").End(xlUp).Offset(-1, 1).Resize(, 3).Resize(2).Borders.LineStyle = xlContinuous
   End If
 End With
 ActiveWorkbook.RefreshAll
 Unload Me
 End Sub

1 个答案:

答案 0 :(得分:1)

如果我了解您的所有情况,那么应该这样做,否则,请随时修改代码:

Option Explicit 'always get this, it forces you to declare all your variables
Sub Test()

    Dim ColA As New Scripting.Dictionary  'Need Microsoft Scripting Runtime Reference
    Dim ColB As New Scripting.Dictionary
    Dim LastRow As Long
    Dim Criteria1 As Boolean
    Dim Criteria2 As Boolean
    Dim C As Range


    With ThisWorkbook.Sheets("MFGLR")
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'This gets the last row on column A
        For Each C In .Range("A1:A" & LastRow) 'loop through the whole column
            'If you have duplicated entries this will throw an error
            ColA.Add C.Value, C.Row 'add the values from column A to DictA, also store it's row for later purposes
            ColB.Add C.Offset(0, 1).Value, C.Row 'add the values from column B to DictB, also store it's row for later purposes
        Next C
        'Criterias will give value of True if matched or False if not
        Criteria1 = ColA.Exists(Combobox2.Value) 'this is getting matched with ColA Dictionary
        Criteria2 = ColB.Exists(Combobox1.Value) 'this is getting matched with ColB Dictionary
        If Criteria1 And Criteria2 Then 'SCENARIO 1
            .Cells(ColA(Combobox2.Value), "AE") = Combobox2.Value
        ElseIf Criteria1 And Not Criteria2 Then 'SCENARIO 2
            .Cells(LastRow + 1, 1) = Combobox2.Value
            .Cells(LastRow + 1, 2) = Combobox1.Value
        ElseIf Not Criteria1 And Not Criteria2 Then 'SCENARIO 3
            .Cells(LastRow + 1, 1) = Combobox2.Value
            .Cells(LastRow + 1, 2) = Combobox1.Value
            'Add data to the column A next blank row in sheet CT
            LastRow = ThisWorkbook.Sheets("CT").Cells(ThisWorkbook.Sheets("CT").Rows.Count, 1).End(xlUp).Row + 1
            ThisWorkbook.Sheets("CT").Cells(LastRow, 1) = Combobox2.Value
            'Add data to the column A next blank row in sheet MFGLR
            LastRow = ThisWorkbook.Sheets("MFGLR ").Cells(ThisWorkbook.Sheets("MFGLR ").Rows.Count, 1).End(xlUp).Row + 1
            ThisWorkbook.Sheets("MFGLR").Cells(LastRow, 1) = Combobox2.Value
            ThisWorkbook.Sheets("MFGLR").Cells(LastRow, 2) = Combobox1.Value
            ThisWorkbook.Sheets("MFGLR").Cells(LastRow + 1, "AE") = Combobox2.Value
        End If
    End With

End Sub