Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA:如何使用“LCASE”标识符清除多个工作表的所有内容_Excel_If Statement_Vba - Fatal编程技术网

Excel VBA:如何使用“LCASE”标识符清除多个工作表的所有内容

Excel VBA:如何使用“LCASE”标识符清除多个工作表的所有内容,excel,if-statement,vba,Excel,If Statement,Vba,这是我目前拥有的vba。我需要它删除任何以“Demand”开头的工作表中的所有数据。代码正在运行,但未删除任何数据。感谢您的帮助 Option Explicit Sub ClearExcelContent() Dim DemandData As Worksheet For Each DemandData In ActiveWorkbook.Worksheets If LCase(Left(DemandData.Name, 6)) = "Demand" Then Dem

这是我目前拥有的vba。我需要它删除任何以“Demand”开头的工作表中的所有数据。代码正在运行,但未删除任何数据。感谢您的帮助

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
上面的语句与任何工作表都不匹配,因为LHS都是小写,而RHS是大写的
D
。将
Demand
替换为小写的
Demand

If LCase(Left(DemandData.Name, 6)) = "demand" Then

以下是字符串编辑的3个基本函数:

LCase
UCase
工作表功能。正确的

这是他们将返回的:

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
Sub TestMe()
    Debug.Print "Vit" = "VIT"
End Sub

非常感谢。这起作用了。我很可能会添加比较文本,以确保它捕获所有内容,但这解决了我的问题。嗨,Vityta,谢谢你,我本来应该添加这个的。谢谢你的回复。
Option Compare Text
Sub TestMe()
    Debug.Print "Vit" = "VIT"
End Sub