[英]Excel VBA: How to clear all contents of multiple worksheets Using a `LCASE` Identifier
这是我目前拥有的vba。 我需要它删除以“需求”开头的任何工作表中的所有数据。 代码正在运行,但是没有数据被删除。 任何帮助表示赞赏!
Option Explicit
Sub ClearExcelContent()
Dim DemandData As Worksheet
For Each DemandData In ActiveWorkbook.Worksheets
If LCase(Left(DemandData.Name, 6)) = "Demand" Then
DemandData.Rows("2:" & Rows.Count).ClearContents
End If
Next DemandData
MsgBox "All Demand Data has been Deleted from Consolidation Tab"
End Sub
If LCase(Left(DemandData.Name, 6)) = "Demand" Then
上面的语句将不匹配任何工作表,因为LHS都是小写,而RHS带有大写D
替换Demand
与小写demand
。
If LCase(Left(DemandData.Name, 6)) = "demand" Then
这些是用于字符串编辑的3个基本功能:
LCase
, UCase
和WorksheetFunction.Proper
他们将返回以下内容:
Demand Is Diamand
demand is diamand
DEMAND IS DIAMAND
如果您像这样运行它们:
Public Sub TestMe()
Dim strText As String
strText = "demaNd is dIamand"
Debug.Print WorksheetFunction.Proper(strText)
Debug.Print LCase(strText)
Debug.Print UCase(strText)
End Sub
为了比较函数的结果,您应该将其与相似的字符串进行比较。 也许最确定的是将相同的字符串函数放在比较的两个部分中。 像这样:
LCase(Left(DemandData.name, 6)) = LCase("demand") Then
通常,如果要确保Vit=VIT
考虑在模块顶部添加“ Option Compare Text
”。 那么这将是正确的:
Option Compare Text
Sub TestMe()
Debug.Print "Vit" = "VIT"
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.