[英]VBA: How to delete a column based on the adjacent cell containing certain characters?
I'm trying to write a VBA code to search a series of column headers for any string that contains "_STD" (there are multiple of these, and not always in the same spot on the worksheets). 我正在尝试编写VBA代码,以搜索一系列列标题,以查找包含“ _STD”的任何字符串(它们有多个,但并不总是在工作表上的同一位置)。 If that substring exists, I want to delete the entire column BEFORE it (OR replace the values in the adjacent column with the "_STD" column values), and then remove "_STD" from the header. 如果该子字符串存在,则我想删除它之前的整个列(或用“ _STD”列值替换相邻列中的值),然后从标题中删除“ _STD”。
Not sure the code for "contains", but I thought I could do something like: 不确定“包含”的代码,但我认为我可以做类似的事情:
If cell.Value = "_STD" Then cell.EntireColumn.delete(xlToRight)
Data example included in the link below: 以下链接中包含的数据示例:
Thank you! 谢谢!
You can use Like
and Offset
您可以使用“ Like
和“ Offset
If cell.Value Like "*_STD" Then
cell.Offset(0,-1).EntireColumn.delete
End If
LIKE
but if _STD
can be case insensitive for example _Std, _std
etc then I recommend using INSTR
with vbTextCompare
as a parameter 您可以使用LIKE
但是如果_STD
不区分大小写,例如_Std, _std
等,那么我建议将INSTR
与vbTextCompare
用作参数 _STD
and then bulk replace _STD
in the header in the relevant row. 删除所有不具有_STD
列,然后在相关行的标题中批量替换_STD
。 For example 例如
'~~> You can reverse loop and delete columns which DO NOT have _STD
If InStr(1, cell.Value, "_STD", vbTextCompare) = 0 Then cell.EntireColumn.Delete
And use this code to bulk replace. 并使用此代码进行批量替换。 I am assuming that the headers are in row 1. Change as applicable. 我假设标题在第1行中。请进行适当的更改。
.Rows(1).Replace What:="_STD", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.