范围内输入的文本导致另一个范围被删除

时间:2019-06-10 16:16:35

标签: excel vba excel-formula

我有2个宏在一张纸上运行。一个宏仅影响复制和粘贴,而另一个则取决于用户窗体的填充,边框和组合框/文本框的输入文本。

由于工作表(每月FGL报告)中的某些原因,当我在A列的下一个可用单元格中输入任何内容时,AM列的前一行(作为iferror vlookup公式)将导致错误。对于A列下面的任何单元格,此操作都继续进行。

我不知道为什么会这样。下面是主要代码。如果您对为什么会发生这种情况有任何想法,那也会有所帮助。

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

Dim wb As Workbook: Set wb = ThisWorkbook

With wb.Sheets("Master Fronting Room List")
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
    Call linepick

ElseIf Criteria1 And Not Criteria2 Then 'SCENARIO 2
    .Cells(LastRow + 1, 1) = ComboBox2.Value
    .Cells(LastRow + 1, 2) = ComboBox1.Value
    Call linepick
    .Cells(LastRow, "B").Offset(-1, 1).Resize(, 3).AutoFill .Cells(LastRow, "B").Offset(-1, 1).Resize(2, 3)
    .Cells(LastRow, "A").Offset(-1, 0).Resize(2, 5).Borders.LineStyle = xlContinuous

ElseIf Not Criteria1 And Not Criteria2 Then 'SCENARIO 3
    .Cells(LastRow + 1, 1) = ComboBox2.Value
    .Cells(LastRow + 1, 2) = ComboBox1.Value
    .Cells(LastRow, "B").Offset(-1, 1).Resize(, 3).AutoFill .Cells(LastRow, "B").Offset(-1, 1).Resize(2, 3)
    .Cells(LastRow, "A").Offset(-1, 0).Resize(2, 5).Borders.LineStyle = xlContinuous
    With wb.Sheets("Connection Totals")
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
        .Cells(LastRow, 1) = ComboBox2.Value
        .Cells(LastRow, "A").Offset(-1, 1).Resize(, 21).AutoFill .Cells(LastRow, "A").Offset(-1, 1).Resize(2, 21)
        .Cells(LastRow, "A").Resize(1, 22).Borders.LineStyle = xlContinuous
    End With

    With wb.Sheets("Monthly FGL Report")
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
        .Cells(LastRow, 1) = ComboBox2.Value
        .Cells(LastRow, "AE") = TextBox1.Value
        .Cells(LastRow, "AE").Offset(-1, 1).Resize(, 5).AutoFill .Cells(LastRow, "AE").Offset(-1, 1).Resize(2, 4)
        .Cells(LastRow, "A").Offset(-1, 0).Resize(2, 44).Borders.LineStyle = xlContinuous
    End With
End If
End With

wb.RefreshAll
Unload Me
End Sub

0 个答案:

没有答案