简体   繁体   English

Excel VBA-比较工作表中的值并删除值匹配的特定工作表上的行

[英]Excel VBA - Compare values across worksheets and delete the rows on a specific worksheet where the values match

Here is what I have so far: 这是我到目前为止的内容:

Private Sub CopyDataFromTemporarySheet()
    Dim checkCellSheet_1 As String
    Dim checkRangeSheet_2 As String
    Dim answer As Long
    Dim sheetName As String


    checkCellSheet_1 = Application.ActiveSheet.Cells(3, 2).Value
    checkRangeSheet_2 = AllWeldersData.range("B" & Rows.count).End(xlUp).Value
    sheetName = WQTR_Form.wqtrNumberText.Text


    If checkCellSheet_1 <> checkRangeSheet_2 Then
        range("A3:XFD3").Copy AllWeldersData.range("A" & Rows.count).End(xlUp).Offset(1, 0)
    Else
        answer = MsgBox("A record in ""All Welders Data"" with the WQTR Number: " + checkCellSheet_1 + _
                        " already exists.  Clicking Yes will overwrite the older" _
                        + " record with this new version.  Would you like to continue?" _
                        , vbYesNo, "Record Already Exists.")

        If answer = vbYes Then
            Worksheets("All Welders Data").Activate
            '****
            '*Delete the entire row where CheckRangeSheet_2 is
            '***
            Worksheets(sheetName).Activate
            range("A3:XFD3").Copy AllWeldersData.range("A" & Rows.count).End(xlUp).Offset(1, 0)
        Else
            Exit Sub
        End If
    End If
End Sub

Here is what it does (except for the missing code where the asterisks are located): It looks at a value in a cell (checkCellSheet_1) and compares it to the values in the range (column "B", checkRangeSheet_2) on the All Welders Data sheet. 这是它的作用(星号所在位置缺少代码除外):它查看单元格中的值(checkCellSheet_1),并将其与所有焊工上的范围值(列“ B”,checkRangeSheet_2)进行比较。数据表。 If a match is found it displays a message box. 如果找到匹配项,则会显示一个消息框。

If the answer is yes it copies a row from the active sheet and pasts it as the last row. 如果答案是肯定的,它将复制活动工作表中的一行并将其粘贴为最后一行。

What I am missing and can't seem to get my head around today, is how to delete the row where the matching value was found on All Welders Data sheet. 我想念的是如何删除在“所有焊工数据”表上找到匹配值的行,而我今天似乎仍然无法理解。 I have tried various things but I keep getting a type mismatch warning or an invalid qualifier. 我已经尝试了各种方法,但是我不断收到类型不匹配警告或无效的限定符。 I am sure this simple and I am just not seeing the forest for the trees. 我相信这很简单,我只是没有看到森林里有树木。 Somehow, I need to get the row number returned for the checkRangeSheet_2 valriable...I think. 以某种方式,我需要获取checkRangeSheet_2可变的返回的行号...我认为。

I appreciate any suggestions. 我感谢任何建议。

This did the trick 这成功了

My original code modified slightly: 我的原始代码稍作修改:

Private Sub CopyDataFromTemporarySheet()
    Dim checkCellSheet_1 As String
    Dim checkRangeSheet_2 As String
    Dim answer As Long
    Dim sheetName As String

    checkCellSheet_1 = Application.ActiveSheet.Cells(3, 2).Value
    checkRangeSheet_2 = AllWeldersData.range("B" & Rows.count).End(xlUp).Value
    sheetName = WQTR_Form.wqtrNumberText.Text

    If checkCellSheet_1 <> checkRangeSheet_2 Then
        range("A3:XFD3").Copy AllWeldersData.range("A" & Rows.count).End(xlUp).Offset(1, 0)
    Else
        answer = MsgBox("A record in ""All Welders Data"" with the WQTR Number: " + checkCellSheet_1 + _
                        " already exists.  Clicking Yes will overwrite the older" _
                        + " record with this new version.  Would you like to continue?" _
                        , vbYesNo, "Record Already Exists.")

        If answer = vbYes Then
            Call DeleteRowsWithDuplicateWQTRNumber 'ADDED THIS LINE
            Worksheets(sheetName).Activate  'ADDED THIS LINE
            range("A3:XFD3").Copy AllWeldersData.range("A" & Rows.count).End(xlUp).Offset(1, 0)
        Else
            Exit Sub
        End If
    End If
End Sub

I added two lines in the If answer = vbYes... One to call another sub (see it below) and the other to make sure the active sheet is correct when the exectution returns to this point. 我在If answer = vbYes...添加了两行,一行调用另一个子程序(请参阅下面的内容),另一行确保执行返回到此点时活动工作表正确。

The new sub follows: 新的子项如下:

Sub DeleteRowsWithDuplicateWQTRNumber() 'ADDED THIS SUB
    Dim lastRow As Long
    Dim counter As Long
    Dim wqtrNumber

    Worksheets("All Welders Data").Activate

    lastRow = AllWeldersData.range("A1").CurrentRegion.Rows.count
    wqtrNumber = WQTR_Form.wqtrNumberText.Text

    For counter = lastRow To 1 Step -1
        If Cells(counter, 2) = wqtrNumber Then
        Rows(counter).Delete
        End If
    Next
End Sub

This activates the sheet where the row will be deleted, finds the last row in the current region, looks for matches to the "wqtrNumber" (from a field in my user form but could just as easily be a cell reference), and finally deletes any rows where it finds a match. 这将激活将要删除该行的工作表,找到当前区域的最后一行,寻找与“ wqtrNumber”匹配的内容(从我的用户表单中的一个字段开始,但是很容易成为单元格引用),最后删除找到匹配项的任何行。

Then execution returns to the previous sub and pastes in a row from another sheet (where the matching value exists which was also taken from the user form). 然后执行返回到前一个子项,并从另一张纸粘贴到一行中(存在匹配的值,该值也从用户表单中获取)。

It took a bit of trial and error on my part but I have tested it multiple times and it works. 我进行了一些反复试验,但是我已经对其进行了多次测试,并且可以正常工作。

UPDATE - THE ABOVE CODE IS FLAWED 更新-上面的代码不正确

For anyone who is looking for a solution that actually works: After further testing I realized that the above code does not work properly. 对于正在寻找切实可行的解决方案的任何人:经过进一步测试,我意识到上述代码无法正常工作。 I revised it and have tested several hundred times and everything does what it supposed to do! 我对其进行了修改,并进行了数百次测试,一切都按预期进行! The revised code is below. 修改后的代码如下。

Option Explicit
Dim wqtrNumber As String 'Global Variables
Dim lastRow As Long
Dim counter_1 As Long
Dim counter_2 As Long

Private Sub CopyDataFromTemporarySheet()
    Dim checkCellSheet_1 As String
    Dim checkRangeSheet_2 As String
    Dim answer As Long

    checkCellSheet_1 = Application.ActiveSheet.Cells(3, 2).Value
    checkRangeSheet_2 = AllWeldersData.range("B" & Rows.count).End(xlUp).Value
    wqtrNumber = WQTR_Form.wqtrNumberText.Text
    lastRow = AllWeldersData.range("A1").CurrentRegion.Rows.count

    For counter_1 = lastRow To 1 Step -1
        If checkCellSheet_1 = checkRangeSheet_2 Then
        answer = MsgBox("A record in ""All Welders Data"" with the WQTR Number: " + checkCellSheet_1 + _
                        " already exists.  Clicking Yes will overwrite the older" _
                        + " record with this new version.  Would you like to continue?" _
                        , vbYesNo, "Record Already Exists.")
            If answer = vbYes Then
                Worksheets("All Welders Data").Activate
                Call DeleteRowsWithDuplicateWQTRNumber
                Worksheets(wqtrNumber).Activate
            Else
                Exit Sub
            End If
        ElseIf checkCellSheet_1 <> checkRangeSheet_2 Then
            'Do Nothing!
        End If
        checkRangeSheet_2 = AllWeldersData.range("B" & counter_1).Value
    Next counter_1

    Worksheets(wqtrNumber).Activate
    range("A3:XFD3").Copy AllWeldersData.range("A" & Rows.count).End(xlUp).Offset(1, 0)
End Sub

Sub DeleteRowsWithDuplicateWQTRNumber()

    Worksheets("All Welders Data").Activate

    For counter_2 = lastRow To 1 Step -1
        If Cells(counter_2, 2) = wqtrNumber Then
        Rows(counter_2).Delete
        End If
    Next
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM