我正在尝试编写VBA宏,但是我对此并不陌生。 (我是德国人,抱歉我的英语不好)。我在StackOverflow上找到了一些有用的代码段,但是我无法适应我的需求。 我有一本包含几张纸的工作簿,其中有些纸具有相同的结构。在C列的这些表中,都有一个日期。如果日期类似于“ 00.01.1900”,则宏应在这些工作表中查找,然后删除该行。我尝试了两个版本,但没有一个起作用。只是什么都没有发生,所以也许单页的演练是错误的?还是字符串匹配不起作用?
版本1:
Dim str As String, w As Long, m As Variant, wss As Variant
wss = Array("Schritt3-WEA1", "Schritt3-WEA2", "Schritt3-WEA3", "Schritt3-WEA4", _
"Schritt3-WEA5", "Schritt3-WEA6", "Schritt3-WEA7", "Schritt3-WEA8", "Schritt3-WEA9" _
, "Schritt3-WEA15", "Schritt3-WEA16", "Schritt3-WEA17", "Schritt3-WEA18", _
"Schritt3-WEA19", "Schritt3-WEA20", "Schritt3-WEA21", "Schritt3-WEA22", _
"Schritt3-WEA23", "Schritt3-WEA28", "Schritt3-WEA29", "Schritt3-WEA36")
str = "00.01.1900"
If CBool(Len(str)) And str <> "False" Then
With ThisWorkbook
For w = LBound(wss) To UBound(wss)
With .Worksheets(wss(w))
m = Application.Match(str, .Columns(3), 0)
Do While Not IsError(m)
.Cells(m, "A").EntireRow.Delete
m = Application.Match(str, .Columns(3), 0)
Loop
End With
Next w
End With
End If
版本2:
Dim wks As Worksheet
Dim arrSheets As Variant
Dim iShCount As Integer
arrSheets = Array("Schritt3-WEA1", "Schritt3-WEA2", "Schritt3-WEA3", "Schritt3-WEA4", _
"Schritt3-WEA5", "Schritt3-WEA6", "Schritt3-WEA7", "Schritt3-WEA8", "Schritt3-WEA9" _
, "Schritt3-WEA15", "Schritt3-WEA16", "Schritt3-WEA17", "Schritt3-WEA18", _
"Schritt3-WEA19", "Schritt3-WEA20", "Schritt3-WEA21", "Schritt3-WEA22", _
"Schritt3-WEA23", "Schritt3-WEA28", "Schritt3-WEA29", "Schritt3-WEA36")
For Each wks In Worksheets
For iShCount = 0 To UBound(arrSheets)
If wks.Name = arrSheets(iShCount) Then
'** Ermittlung der letzten Zeile in Spalte C
lz = Cells(Rows.Count, 3).End(xlUp).Rows.Row
'** Durchlauf aller Zeilen
For t = lz To 15 Step -1
'Z?hlung r?ckw?rts bis Zeile 15
'Abfragen, ob in der dritten Spalte "00.01.1900" steht
If Cells(t, 3).Value = "00.01.1900" Then
Rows(t).Delete Shift:=xlUp
End If
Next t
End If
Next
Next
非常感谢您!
答案 0 :(得分:1)
编辑:将.value更改为.value2并插入“退出”
非常感谢,现在可以使用了
Dim wks As Worksheet
Dim arrSheets As Variant
Dim iShCount As Integer
arrSheets = Array("Schritt3-WEA1", "Schritt3-WEA2", "Schritt3-WEA3", "Schritt3-WEA4", _
"Schritt3-WEA5", "Schritt3-WEA6", "Schritt3-WEA7", "Schritt3-WEA8", "Schritt3-WEA9" _
, "Schritt3-WEA15", "Schritt3-WEA16", "Schritt3-WEA17", "Schritt3-WEA18", _
"Schritt3-WEA19", "Schritt3-WEA20", "Schritt3-WEA21", "Schritt3-WEA22", _
"Schritt3-WEA23", "Schritt3-WEA28", "Schritt3-WEA29", "Schritt3-WEA36")
For Each wks In Worksheets
For iShCount = 0 To UBound(arrSheets)
If wks.Name = arrSheets(iShCount) Then
'** Ermittlung der letzten Zeile in Spalte C
lz = wks.Cells(Rows.Count, 3).End(xlUp).Rows.Row
'** Durchlauf aller Zeilen
For t = lz To 15 Step -1
'Z?hlung r?ckw?rts bis Zeile 15
'Abfragen, ob in der dritten Spalte "00.01.1900" steht
If wks.Cells(t, 3).Value2 = 0 Then
wks.Rows(t).Delete Shift:=xlUp
End If
Next t
Exit For
End If
Next
Next
答案 1 :(得分:0)
转到“主页”并选择00.01.1900单元格之一,并将其格式从日期更改为一般,您看到它变为0了吗?
需要知道单元格是存储值0还是文本“ 00.01.1900”
如果确实更改为0,则只需使用版本2代码,但将其更改为下面的行(从“ 00.01.1900”更改为0)
If Cells(t, 3).Value = 0 Then
答案 2 :(得分:0)
假设您的数据列C
包含实际日期(而不是字符串),请使用第二个变体,但检查一个(数字)值0
。字符串"00.01.1900"
只是一个(取决于语言的)表示形式,以0作为日期。
请确保访问Cells(t, 3).Value2
(而不是Value
,因为当单元格格式为Date
时,这将返回一个字符串)。有关详细信息,请参见What is the difference between .text, .value, and .value2?。
请注意,当您遍历数据以删除其中一些数据时,总是应该向后工作(如第二个示例所示),否则您可能会错过一些数据:假设您删除了第3行,然后删除了前一行4将获得新的第3行,但是您的循环将继续检查第4行-永远不会检查以前的第4行(现在是第3行)。