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

问题描述

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

这既简洁又快速 - 但是,这取决于 G 列中是否有正确的数据(因为这是我在代码中使用的范围)。但是,我希望我的宏能更智能、更通用。我希望它通过第一行,读取值(标题)并找到标题为“选择退出”的列,然后在该列上运行我的删除所有行宏。

这意味着即使我们在数据中的 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

标签: excelvba

解决方案


这就是你需要的:

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

推荐阅读