gpt4 book ai didi

excel - 如何在 case 语句中定义范围?

转载 作者:行者123 更新时间:2023-12-01 22:50:44 25 4
gpt4 key购买 nike

如果范围内的单元格(在本例中为第一行)包含特定值,我想删除一列。我以为我可以这样做:

Public Sub Delete_Column()

Select Case Range(A1:A10)

Case "Birthday", "Gender"
cell.EntireColumn.Delete

End Select
End Sub

但它不起作用。我确定这是错误的 Select Case Range(A1:A10) 行,但我不知道如何修复它。

最佳答案

删除列(使用Union)

Sub DeleteColumns()

Dim ws As Worksheet: Set ws = ActiveSheet ' improve!

Dim drg As Range
Dim cell As Range

' First 10 cells (columns) in the first worksheet row.
For Each cell In ws.Range("A1").Resize(, 10).Cells
' Specified single-row range in the first worksheet row.
'For Each cell In ws.Range("A1:J1").Cells
Select Case CStr(cell.Value)
Case "Birthday", "Gender"
If drg Is Nothing Then
Set drg = cell
Else
Set drg = Union(drg, cell)
End If
'Case Else ' do nothing
End Select
Next cell

If drg Is Nothing Then Exit Sub ' no cell found

drg.EntireColumn.Delete

End Sub

关于excel - 如何在 case 语句中定义范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74414446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com