由于某种原因,它不会转到范围内的下一个单元格来检查值。
分解将要发生的事情
子调用 Modules1.Getdata
这会检查每一行的通知标签(“真/假”)。如果为真,它会获取 CompanyNumber 调用 Module3.Check
Moduel3.Check 使用 CompanyNumber 检查 Samevalue 的另一个工作表/范围(转到 Module1.Getdata 中的下一个迭代) 如果下一个单元格为空,请输入公司编号等。
希望这是有道理的。
子
Sub Workbook_open()
Call Module1.GetData
End Sub
Module1.GetData
Public EmailAddress As String
Public CompanyNumber As String
Public Name As String
Public Comp As String
Public ID As Integer
Function GetData()
Dim LastRow As String
Dim rng As Range
Worksheets("DDregister").Activate
Range("K2").Select
LastRow = Cells(Rows.Count, "K").End(xlUp).Row
For Each rng In Range("K2:K" + LastRow)
If Not rng.Value = vbNullString Then
Worksheets("DDregister").Activate
Range("K2").Select
Select Case rng.Value
Case 1
Case Is = "True"
rng.Select
Let EmailAddress = ActiveCell.Offset(0, -5).Value
Let CompanyNumber = ActiveCell.Offset(0, -9).Value
Let Name = ActiveCell.Offset(0, -8).Value
Let Comp = ActiveCell.Offset(0, -7).Value
ID = ActiveCell.Offset(0, -10).Value
Call Module3.Check(EmailAddress, CompanyNumber, Name, Comp)
Case 2
Case Is = "False"
End Select
ElseIf rng.Value = vbNullString Then
ThisWorkbook.Save
Application.DisplayAlerts = True
'ThisWorkbook.Close
End If
Next
End Function
Module3.检查
Function Check(EmailAddress As String, CompanyNumber As String, Name As String, Comp As String)
Dim rngCheck As Range
Dim LastRowCheck As String
Dim NewRange As Range
Worksheets("Check").Activate
ActiveSheet.Range("B2").Select
LastRowCheck = Cells(Rows.Count, "B").End(xlDown).Row
For Each rngCheck In Range("B2:B" + LastRowCheck)
Select Case rngCheck.Value
Case 1
Case Is = CompanyNumber
'Go to next iteration
Case 2
Case Is = vbNullString
ActiveCell.Value = CompanyNumber
ActiveCell.Offset(0, 1).Value = "True"
ActiveCell.Offset(0, -1).Value = ID
Call Module2.Email(EmailAddress, CompanyNumber, Name, Comp)
Next
End Function
Module2.电子邮件
Function Email(EmailAddress As String, CompanyNumber As String, Name As String, Comp As String)
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Subject " & (Comp)
objMessage.From = "EmailAddress@Address.com"
objMessage.Cc = "EmailAddress@Address.com"
objMessage.To = (EmailAddress)
'MsgBox (EmailAddress)
objMessage.TextBody = "Stuff"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "x.x.x.x"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
objMessage.Send
End Function