Excel VBA 对多个工作表使用嵌套 If 语句
Excel VBA that uses nested If statements for multiple worksheets
我正在尝试为工作簿中的多个工作表获取 U 列中的输出。 return 字符串将是 "Yes" 或 "No" 取决于哪一列不为空以及两个日期之间的差异是否 > 150。这是我编写的代码,但没有任何显示在 U 列中。谁能帮我弄清楚为什么这不起作用?
Sub Compliance()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Dim i As Integer
Dim listLength
listLength = ws.Cells(Rows.Count, "M").End(xlUp).Row - 1
For i = 2 To listLength + 2
If IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And IsEmpty(ws.Range("N" & i)) = True And DateDiff("d", ws.Range("M" & i), ws.Range("K" & i)) > 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And DateDiff("d", ws.Range("O" & i), ws.Range("N" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
Else
ws.Range("U" & i) = "No"
End If
Next
Next ws
End Sub
当您使用 For Each ws In ThisWorkbook.Worksheets
开始循环时,Excel 从 Sheet1 开始处理,我是确保你的列表在另一个 sheet.
So, the reason nothing changes in your U column is, because you are
looking at wrong Worksheet.
只要您想循环浏览所有作品sheet,请开始关注 Sheet1 中的列表。 如果您想循环不同的顺序,你应该在你的代码中定义它。
在下面我编辑了您的部分代码并添加了 msgbox ws.name
以向您展示 sheet Excel 现在正在工作。
Why Use Integer Instead of Long?
Option Explicit
Sub Compliance()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Dim i As Long
Dim listLength
MsgBox ws.Name
listLength = ws.Cells(ws.Rows.Count, "M").End(xlUp).Row - 1
For i = 2 To listLength + 2
If IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And IsEmpty(ws.Range("N" & i)) = True And DateDiff("d", ws.Range("M" & i), ws.Range("K" & i)) > 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And DateDiff("d", ws.Range("O" & i), ws.Range("N" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
Else
ws.Range("U" & i) = "No"
End If
Next
Next ws
End Sub
我正在尝试为工作簿中的多个工作表获取 U 列中的输出。 return 字符串将是 "Yes" 或 "No" 取决于哪一列不为空以及两个日期之间的差异是否 > 150。这是我编写的代码,但没有任何显示在 U 列中。谁能帮我弄清楚为什么这不起作用?
Sub Compliance()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Dim i As Integer
Dim listLength
listLength = ws.Cells(Rows.Count, "M").End(xlUp).Row - 1
For i = 2 To listLength + 2
If IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And IsEmpty(ws.Range("N" & i)) = True And DateDiff("d", ws.Range("M" & i), ws.Range("K" & i)) > 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And DateDiff("d", ws.Range("O" & i), ws.Range("N" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
Else
ws.Range("U" & i) = "No"
End If
Next
Next ws
End Sub
当您使用 For Each ws In ThisWorkbook.Worksheets
开始循环时,Excel 从 Sheet1 开始处理,我是确保你的列表在另一个 sheet.
So, the reason nothing changes in your U column is, because you are looking at wrong Worksheet.
只要您想循环浏览所有作品sheet,请开始关注 Sheet1 中的列表。 如果您想循环不同的顺序,你应该在你的代码中定义它。
在下面我编辑了您的部分代码并添加了 msgbox ws.name
以向您展示 sheet Excel 现在正在工作。
Why Use Integer Instead of Long?
Option Explicit
Sub Compliance()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Dim i As Long
Dim listLength
MsgBox ws.Name
listLength = ws.Cells(ws.Rows.Count, "M").End(xlUp).Row - 1
For i = 2 To listLength + 2
If IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And IsEmpty(ws.Range("N" & i)) = True And DateDiff("d", ws.Range("M" & i), ws.Range("K" & i)) > 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And DateDiff("d", ws.Range("O" & i), ws.Range("N" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
Else
ws.Range("U" & i) = "No"
End If
Next
Next ws
End Sub