简体   繁体   English

Excel VBA删除第一个单元格以'开头的列

[英]Excel VBA to delete column with first cell starting with '

This is probably a simple question but I can't seem to find the answer anywhere and am still new to VBA. 这可能是一个简单的问题,但我似乎无法在任何地方找到答案,并且对VBA还是陌生的。 I have a program that exports data to excel. 我有一个程序可以将数据导出到excel。 I need VBA code to delete specific columns with headers equal to strings. 我需要VBA代码来删除标题等于字符串的特定列。 I am having issues because one column is exported with ' preceding the word. 我遇到了问题,因为导出的一列前有单词'。 Here is my code: 这是我的代码:

For i = 1 To 7
If Cells(1, i).Value = "A" Then
    Cells(1, i).EntireColumn.Delete
ElseIf Cells(1, i).Value = "'B" Then
    Cells(1, i).EntireColumn.Delete
ElseIf Cells(1, i).Value = "C" Then
    Cells(1, i).EntireColumn.Delete
End If
Next i

The issue is the "B" column. 问题是“ B”列。 I've tried = "B" and = "'B" and neither work. 我试过=“ B”和=“'B”,但都没有用。 Any help is greatly appreciated! 任何帮助是极大的赞赏! Let me know if I'm not being clear. 让我知道是否不清楚。

I've also tried: 我也尝试过:

ElseIf Cells(1, i).Value = Chr(39) & "B" Then
    Cells(1, i).EntireColumn.Delete

Simple fix here... Not sure why I didn't think of this. 这里的简单修复...不确定为什么我没有想到这一点。 Since I was deleting a column before "B", it was skipping right over it. 由于我要删除“ B”之前的列,因此它就跳过了。 Solution is to reverse. 解决办法是扭转。 Thanks for all the help everyone! 谢谢各位的帮助!

For i = 7 To 1 Step -1
If Cells(1, i).Value = "A" Then
    Cells(1, i).EntireColumn.Delete
ElseIf Cells(1, i).Value = "B" Then
    Cells(1, i).EntireColumn.Delete
ElseIf Cells(1, i).Value = "C" Then
    Cells(1, i).EntireColumn.Delete
End If
Next i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM