簡體   English   中英

如果單元格值為x,則刪除列

[英]Deleteing column if the cell value is x

我在excel中工作,如果單元格中的值在10+列的范圍內,我正在嘗試刪除列。 數據過濾不起作用,因為它刪除了行。因此,我需要選擇指定行中的單元格包含值x的所有列。 我正在錄制宏並嘗試自動執行該操作。 宏代碼是受歡迎的,但沒有必要。謝謝。

像這樣? 將此代碼粘貼到模塊中。

Sub Sample()
    Dim ws As Worksheet
    Dim Rng As Range
    Dim Rw As Long, i as Long

    '~~> This is the relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    '~~> This the row where you want to check
    Rw = 1

    With ws
        '~~> I am assuming there are 10 cols. Change as applicable
        For i = 1 To 10
            '~~> UCASE so that it check for x and X
            If UCase(.Cells(Rw, i).Value) = "X" Then
                '~~> Set your range
                If Rng Is Nothing Then
                    Set Rng = .Columns(i)
                Else
                    Set Rng = Union(Rng, .Columns(i))
                End If
            End If
        Next i
    End With

    If Not Rng Is Nothing Then Rng.Delete Shift:=xlRight
End Sub

截圖

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM