根据该列中第一个单元格的值查找一列,然后将该列定义为可在宏函数中使用的范围

时间:2019-05-13 14:55:37

标签: excel vba

我正在运行一个数据清理宏,该宏将转到G列,并删除所有包含“ Y”的行。

这是整洁且快速的-但是,这取决于在G列中拥有正确的数据(因为这是我在代码中使用的范围)。但是,我希望我的宏变得更聪明,更通用。我希望它经过第一行,读取值(标题),并找到标题为“ Opt Out”的列,然后在该列上运行删除所有行的宏。

这意味着,即使我们在数据中的G列之前添加了另一个额外的列,该宏仍应能够处理该列。

为此,我设法找到的唯一可行的答案是使用WorksheeetFunction.Match方法-但是,此方法的问题在于,它不会将查找我的lookup_value的整个列设置为范围,因此宏返回错误或无法运行。

我在这里和其他来源阅读了很多问题,但是没有找到可以定义这样范围的内容。如果我的问题不清楚,请告诉我。

我对VBA语法不太满意,但是对Excel和PowerQuery相当熟练。如果有一个我没有看到的基本解决方案,请原谅。

谢谢。

D

' ***************************************************************
' Delete rows based on cell value
'****************************************************************


Sub deleteOptOutRows()

    'Disable certain Excel features whilst the macro is running

    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False
    Application.ScreenUpdating = False

    ' Declare variables

    Dim deleteRow As String
    Dim ws As Worksheet

    'Set objects
    Set ws = ActiveSheet

        'Loop through the rows of data, in order to delete rows with a Y

        'Y in column G. Our data commences on row 2

        For deleteRows = ws.Range("G" & Rows.Count).End(xlUp).Row To 2 Step -1

            If ws.Range("G" & deleteRows).Value = "Y" Then
                Rows(deleteRows).EntireRow.Delete
            End If

        ' Mode to next cell in the range, which is being looped
        Next deleteRows

    ' Re-enable the Excel features we've disabled at the top of our macro

    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    Application.ScreenUpdating = True



End Sub

1 个答案:

答案 0 :(得分:1)

这就是您需要的:

Option Explicit
Sub deleteOptOutRows()
    ' ***************************************************************
    ' Delete rows based on cell value
    '****************************************************************

    'Disable certain Excel features whilst the macro is running

    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False
    Application.ScreenUpdating = False

    ' Declare variables

    Dim i As Long 'use i to count loops
    Dim LastRow As Long, Col As Integer 'add a column and last row variable
    Dim ws As Worksheet

    'Set objects
    Set ws = ActiveSheet
    With ws
        Col = .Cells.Find("Opt Out").Column 'find the column value for that header
        LastRow = .Cells(.Rows.Count, Col).End(xlUp).Row 'fin the last row
        'Loop through the rows of data, in order to delete rows with a Y

        'Y in column G. Our data commences on row 2

        For i = LastRow To 2 Step -1
            If .Cells(i, Col) = "Y" Then
                .Rows(i).EntireRow.Delete
            End If
        ' Mode to next cell in the range, which is being looped
        Next i
    End With
    ' Re-enable the Excel features we've disabled at the top of our macro

    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub