excel - 删除除包含 X 值的列之外的所有列
问题描述
我正在尝试删除除名称之外的所有列,当前下面的代码将删除名称。
Private Sub CommandButton2_Click()
Dim i As Integer
Dim A As Range
For i = 100 To 1 Step -1
Set A = Cells(1, i).Find(What:="Names", LookIn:=xlValues)
If Not A Is Nothing Then A.EntireColumn.Delete
Next i
End Sub
更新代码:
Private Sub CommandButton1_Click()
Dim i As Integer
Dim A As Range
If Cells(1, i) <> "Names" Then
If A Is Nothing Then A.EntireColumn.Delete
End If
End Sub
解决方案
我不敢相信没有更有效的方法,但这应该可行。
为了允许 Names 列在任何地方,首先我将它移动到第 1 列,然后再删除其他所有内容。100 是硬编码的,因此您可能希望更改它。
Private Sub CommandButton1_Click()
Dim i As Long 'better than integer
Dim A As Range
Set A = Rows(1).Find(What:="Names", LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not A Is Nothing Then 'Names found
Columns(A.Column).Cut
Range("A1").Insert
Range("B1").Resize(, 99).EntireColumn.Delete
Else
Range("A1").Resize(, 100).EntireColumn.Delete 'delete all columns as Names not found
End If
End Sub
推荐阅读
- elasticsearch - 查询以匹配 Kibana 中两个字段的内容
- python - 如何使用烧瓶逐行从csv文件中获取json
- mikrotik - 如何绕过 mikrotik 中的代理?
- node.js - 为 NodeJS 应用程序构建 docker 时找不到模块错误
- hadoop - Apache Hadoop HDFS 能否帮助加快将大文件上传(通过 Web 浏览器)到服务器的速度?
- excel - If ... Then 用于校正角度测量的语句
- mysql - 为什么我不能运行这个查询?
- python - 如何在 Python 中对列表的元素进行分组,然后对分组的元素进行操作
- html - 外部css颜色不起作用,但其他一切正常,内部工作
- java - 带有 Maven 多模块的 Spring Boot 不起作用