在 table 中按名称循环浏览一行中的每一列
Cycle through each column in a row by name in a table
我有一个 table,其中有七列包含日期。每列都有使用不同公式为每列分配的日期。我的最终目标是将日期与今天进行比较,如果在一定时间内,则发送电子邮件,然后更新日期。我的问题,首先是尝试获取每一行,然后 运行 遍历每一列。我一直在暗中尝试
Dim ColWeekly, ColBiWeekly, ColMonthly, ColThMonth, _
ColSxMonth, ColYearly, ColBiYearly As Range
Dim ColVar As Variant
Dim PMTime As Long
Set ColWeekly = Range("PM[Weekly]")
For Each Row In [PM].Rows
For Each Column In [PM].Columns
Set ColVar = Column.Name
Select Case ColVar
Case ColWeekly
If Date - Cell.Value = 1 Then
Call Email
Set Cell.Value = TODAY + 8
End If
但是当它到达 Case ColWeekly
行时,我一直收到 "Type Mismatch error"。如果我使用 Case ColWeekly.name
也不起作用。
我敢肯定这实际上是一个非常简单的任务,但我一直在循环工作,所以如果你能提供任何东西,我们将不胜感激。
如果 header 的行是静态的并且你的数据集很小(假设你想发送电子邮件,我会假设它是)你可以使用这种检查方法。
在我的示例中,headers 在 行 5 中,我使用了 Like 参数。
For i = 6 To LastRow
For j = 1 To LastColumn
Select Case True
Case CStr(Cells(5, j).Value2) Like "*megjegyzés*" 'megjegyzések sortöréseinek eltüntetése
Cells(i, j).Value = Replace(CStr(Cells(i, j).Value2), vbLf, "")
Cells(i, j).Value = Replace(CStr(Cells(i, j).Value2), vbCr, "")
Cells(i, j).Value = Trim(CStr(Cells(i, j).Value2))
Case CStr(Cells(5, j).Value2) Like "*Tárgy*"
Cells(i, j).Value = Replace(CStr(Cells(i, j).Value2), vbLf, "")
Cells(i, j).Value = Replace(CStr(Cells(i, j).Value2), vbCr, "")
Cells(i, j).Value = Trim(CStr(Cells(i, j).Value2))
Case CStr(Cells(5, j).Value2) Like "*Kiállítás*" 'dátumok formátumváltása
Cells(i, j).Value = Year(Cells(i, j).Value) & IIf(Len(Month(Cells(i, j).Value)) = 1, "0" & Month(Cells(i, j).Value), Month(Cells(i, j).Value)) & IIf(Len(Day(Cells(i, j).Value)) = 1, "0" & Day(Cells(i, j).Value), Day(Cells(i, j).Value))
Cells(i, j).NumberFormat = "General"
Case CStr(Cells(5, j).Value2) Like "*Esedékes*"
Cells(i, j).Value = Year(Cells(i, j).Value) & IIf(Len(Month(Cells(i, j).Value)) = 1, "0" & Month(Cells(i, j).Value), Month(Cells(i, j).Value)) & IIf(Len(Day(Cells(i, j).Value)) = 1, "0" & Day(Cells(i, j).Value), Day(Cells(i, j).Value))
Cells(i, j).NumberFormat = "General"
Case CStr(Cells(5, j).Value2) Like "*Teljesítés*"
Cells(i, j).Value = Year(Cells(i, j).Value) & IIf(Len(Month(Cells(i, j).Value)) = 1, "0" & Month(Cells(i, j).Value), Month(Cells(i, j).Value)) & IIf(Len(Day(Cells(i, j).Value)) = 1, "0" & Day(Cells(i, j).Value), Day(Cells(i, j).Value))
Cells(i, j).NumberFormat = "General"
End Select
Next
Next
你的例子有很多问题。如果没有关于您的工作簿格式的更多详细信息,我将无法更正它们。
相反,我将提供一个简单示例,说明如何正确使用 Select Case
并进行简单的列比较。将范围名称 Weekly
和 Monthly
分配给列 A:C
中的某处,以便使用此示例。
Option Explicit
Public Sub IterateThroughColumns()
' Create some variables
Dim col As Range, colWeekly As Long, colMonthly As Long
' Assigns the column number for each range
colWeekly = Range("Weekly").Column
colMonthly = Range("Monthly").Column
For Each col In Sheet1.Range("A:C").Columns
Select Case col.Column
Case colWeekly
MsgBox "Found Weekly range in column " & col.Column
Case colMonthly
MsgBox "Found Monthly range in column " & col.Column
Case Else
MsgBox "No range was found in column " & col.Column
End Select
Next
End Sub
这应该让您了解如何设置一个简单的 For Each
循环来遍历列,但按列号进行比较,而不是直接比较范围对象。
我有一个 table,其中有七列包含日期。每列都有使用不同公式为每列分配的日期。我的最终目标是将日期与今天进行比较,如果在一定时间内,则发送电子邮件,然后更新日期。我的问题,首先是尝试获取每一行,然后 运行 遍历每一列。我一直在暗中尝试
Dim ColWeekly, ColBiWeekly, ColMonthly, ColThMonth, _
ColSxMonth, ColYearly, ColBiYearly As Range
Dim ColVar As Variant
Dim PMTime As Long
Set ColWeekly = Range("PM[Weekly]")
For Each Row In [PM].Rows
For Each Column In [PM].Columns
Set ColVar = Column.Name
Select Case ColVar
Case ColWeekly
If Date - Cell.Value = 1 Then
Call Email
Set Cell.Value = TODAY + 8
End If
但是当它到达 Case ColWeekly
行时,我一直收到 "Type Mismatch error"。如果我使用 Case ColWeekly.name
也不起作用。
我敢肯定这实际上是一个非常简单的任务,但我一直在循环工作,所以如果你能提供任何东西,我们将不胜感激。
如果 header 的行是静态的并且你的数据集很小(假设你想发送电子邮件,我会假设它是)你可以使用这种检查方法。
在我的示例中,headers 在 行 5 中,我使用了 Like 参数。
For i = 6 To LastRow
For j = 1 To LastColumn
Select Case True
Case CStr(Cells(5, j).Value2) Like "*megjegyzés*" 'megjegyzések sortöréseinek eltüntetése
Cells(i, j).Value = Replace(CStr(Cells(i, j).Value2), vbLf, "")
Cells(i, j).Value = Replace(CStr(Cells(i, j).Value2), vbCr, "")
Cells(i, j).Value = Trim(CStr(Cells(i, j).Value2))
Case CStr(Cells(5, j).Value2) Like "*Tárgy*"
Cells(i, j).Value = Replace(CStr(Cells(i, j).Value2), vbLf, "")
Cells(i, j).Value = Replace(CStr(Cells(i, j).Value2), vbCr, "")
Cells(i, j).Value = Trim(CStr(Cells(i, j).Value2))
Case CStr(Cells(5, j).Value2) Like "*Kiállítás*" 'dátumok formátumváltása
Cells(i, j).Value = Year(Cells(i, j).Value) & IIf(Len(Month(Cells(i, j).Value)) = 1, "0" & Month(Cells(i, j).Value), Month(Cells(i, j).Value)) & IIf(Len(Day(Cells(i, j).Value)) = 1, "0" & Day(Cells(i, j).Value), Day(Cells(i, j).Value))
Cells(i, j).NumberFormat = "General"
Case CStr(Cells(5, j).Value2) Like "*Esedékes*"
Cells(i, j).Value = Year(Cells(i, j).Value) & IIf(Len(Month(Cells(i, j).Value)) = 1, "0" & Month(Cells(i, j).Value), Month(Cells(i, j).Value)) & IIf(Len(Day(Cells(i, j).Value)) = 1, "0" & Day(Cells(i, j).Value), Day(Cells(i, j).Value))
Cells(i, j).NumberFormat = "General"
Case CStr(Cells(5, j).Value2) Like "*Teljesítés*"
Cells(i, j).Value = Year(Cells(i, j).Value) & IIf(Len(Month(Cells(i, j).Value)) = 1, "0" & Month(Cells(i, j).Value), Month(Cells(i, j).Value)) & IIf(Len(Day(Cells(i, j).Value)) = 1, "0" & Day(Cells(i, j).Value), Day(Cells(i, j).Value))
Cells(i, j).NumberFormat = "General"
End Select
Next
Next
你的例子有很多问题。如果没有关于您的工作簿格式的更多详细信息,我将无法更正它们。
相反,我将提供一个简单示例,说明如何正确使用 Select Case
并进行简单的列比较。将范围名称 Weekly
和 Monthly
分配给列 A:C
中的某处,以便使用此示例。
Option Explicit
Public Sub IterateThroughColumns()
' Create some variables
Dim col As Range, colWeekly As Long, colMonthly As Long
' Assigns the column number for each range
colWeekly = Range("Weekly").Column
colMonthly = Range("Monthly").Column
For Each col In Sheet1.Range("A:C").Columns
Select Case col.Column
Case colWeekly
MsgBox "Found Weekly range in column " & col.Column
Case colMonthly
MsgBox "Found Monthly range in column " & col.Column
Case Else
MsgBox "No range was found in column " & col.Column
End Select
Next
End Sub
这应该让您了解如何设置一个简单的 For Each
循环来遍历列,但按列号进行比较,而不是直接比较范围对象。