excel - 更改代码以使用选择来填充数组
问题描述
我有一个工作代码,它可以做它应该做的事情。我只想更改myStrings
变量的填充方式。
从:
myStrings = Array("Test23", "Test-12", "54-Test")
至:
myStrings = Selection.Value
如果我运行它,我会收到运行时错误 9;下标超出范围。
目标是选择一些带有文本的单元格,始终是同一列,只是不同的行,并且 VBA 宏应该从工作簿的每个工作表中删除所有带有该文本的行。
我是 VBA 的新手,我从不同的来源结合了这个,我不知道如何解决这个错误。
如果它做同样的事情,我很感激任何帮助,甚至可能是不同的解决方案。
谢谢你。
Option Explicit
Sub LoopThroughWorksheets()
Dim sh As Worksheet
For Each sh In ThisWorkbook.Worksheets
Dim calcmode As Long
Dim ViewMode As Long
Dim myStrings As Variant
Dim FoundCell As Range
Dim I As Long
Dim myRng As Range
Set myRng = sh.Range("A:N")
myStrings = Array("Test23", "Test-12", "54-Test")
With Application
calcmode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
With sh
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
With myRng
For I = LBound(myStrings) To UBound(myStrings)
Do
Set FoundCell = myRng.Find(What:=myStrings(I), _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If FoundCell Is Nothing Then
Exit Do
Else
FoundCell.EntireRow.Delete
End If
Loop
Next I
End With
End With
With Application
.ScreenUpdating = True
.Calculation = calcmode
End With
ActiveWindow.View = ViewMode
Next sh
MsgBox ("Completed")
End Sub
解决方案
推荐阅读
- c++ - 尝试将指针与结构一起使用时出错
- java - JsonParser nextToken() 挂起并且永不返回
- openvas - Openvas 校验和文件签名错误或缺失(空)
- gson - GSON 中具有未知字段的 JSON
- python - groupby.apply() 的行为不一致
- amazon-ec2 - Terraform aws:在 aws_autoscaling_group 中标记卷
- java - 应用程序在循环通过屏幕像素时冻结
- xml - go - 标签中的属性/隐藏父标签:SOAP 信封
- unicode - 替换“从字节到字节”,而不是“从字符到字符”
- django - 在 Django 测试中设置标头(API 版本控制)