【问题标题】:VBA Loop across one row to find a value that contains a cell and delete columns before that columnVBA循环遍历一行以查找包含单元格的值并删除该列之前的列
【发布时间】:2018-05-24 10:19:09
【问题描述】:

我需要检查从 C7(C7、D7、E7 等)开始的第 7 行中的每个单元格,并找到一个包含特定日期的字符串值的单元格(例如,字符串中的“9/30/2017” “2017 年 6 月 30 日到 2017 年 9 月 30 日”)并删除 C 列中的所有列到单元格所在的任何列。我将如何使用 VBA 代码执行此操作?我是 VBA 的新手,并且已经尝试了所有我能找到的东西。感谢您的任何建议

我的代码:

Sub DeleteUnnecessaryColumns(specifiedWorksheet) 

Dim lastCol As Long 
lastCol = Cells.Find(What:="*", After:=[C1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column 

For i = 1 To lastCol 
    If InStr(Cells(7, 0), specifiedDate, vbTextCompare) = 0 Then 
        Columns(i).Delete 
    End If 
Next i 

End Sub

【问题讨论】:

  • 所以如果我们在 F 列中找到日期,您是否要删除 CEC、D、F??
  • 如果日期在F7,我想删除C、D、E列。
  • @activexplor3 如果您发布到目前为止您已完成的工作,我们将很乐意为您提供帮助。
  • 请添加您的代码
  • @activexplor3 我已将您的代码添加到您的帖子中 - 下次,使用代码编辑您的帖子,而不是添加新评论。

标签: vba excel


【解决方案1】:

一些变化:

  1. 使用With 块明确定位指定的工作表
  2. Cells 集合之前添加. 以显式链接到指定的工作表(通过With 块)
  3. 使用正确的列索引开始循环 C = 3 (For i = 3 ...)
  4. 您错过了InStr 函数的第一个参数:起始字符(假定为1)MSDN
  5. InStr 的第二个参数未设置为 i 变量。现在将循环遍历每列中的值
  6. 一旦找到搜索文本,循环就不会停止。所以我添加了一个Else 子句,一旦找到specifiedDate 就退出For 循环,防止脚本删除目标列右侧的列。
  7. specifiedDate 从未传递给 Sub 或声明,因此我将其添加为参数。您也可以在此过程中声明和设置它。如果它被声明为公共变量,则删除该参数。
  8. .Find 方法仅适用于范围对象,因此我改用了适用于单元格的方法
  9. 删除一列后,其余列的索引减一,因此必须对控制循环的变量执行相同操作

如果在第 7 行中找不到搜索文本,它现在应该会正确删除一列,一旦找到第一个匹配项,它将停止执行,而其余列保持不变。

Sub DeleteUnnecessaryColumns(specifiedWorksheet, specifiedDate) 

Dim lastCol As Long
With specifiedWorksheet
    lastCol = .Cells(7, .Columns.Count).End(xlToLeft).Column
    For i = 3 To lastCol
        If InStr(1, .Cells(7, i), specifiedDate, vbTextCompare) = 0 Then 
            .Columns(i).Delete
            i = i - 1
            lastCol = lastCol - 1
        Else
            Exit For
        End If 
    Next i 
End With

End Sub

【讨论】:

  • 感谢您的回复!我已经尝试了代码,但在这一行出现 Object Required 错误:lastCol = .Cells.Find(What:="*", After:=[C1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
  • 查看编辑。 .Find 方法适用于 Range 对象。这似乎是额外的开销,所以我转换为常用的 ctrl + shift + end 方法。这并不总是正确的,但却是一个不错的选择。
  • 代码好像删除了C列,然后就停止了。
  • 在 Col D 中找到 specifieddate 吗?
  • 不,它是单元格 AF7 中字符串的一部分。
【解决方案2】:

类似...你需要从其他地方调用它,使用类似DeleteUnnecessaryColumns "Sheet1", "9/30/2017"的语法

Sub DeleteUnnecessaryColumns(specifiedWorksheet as worksheet, SpecifiedDate as string) 

dim r as range   ' set up a range for row 7, columns C to the end
set r = specifiedWorksheet.rows(7)
set r = r.resize(Columnsize:= r.columns.count - 2)
set r =  r.offset(0,2)

Dim lastCol As Long, i as long

for i = 1 to r.columns.count  'loop through the cells in the r range
    if instr(string:=r.cells(i), substring:= SpecifiedDate) > 0 then
        lastcol = i  'this ends the loop and sets the value for lastcol
    end if 
next

SpecifiedWorksheet.range(cells(1, 3), cells(1, lastcol - 1).entirecolumn.delete

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多