提问人:Emma Sjöberg 提问时间:9/11/2023 最后编辑:marc_sEmma Sjöberg 更新时间:9/12/2023 访问量:44
为什么我的 VBA 使用偏移量不适用于某些列?
Why is my VBA using Offset not working for some columns?
问:
我对 VBA 非常陌生,所以希望这是一个简单的修复方法。
我的VBA代码正在创建自动电子邮件。只要我不将 N.Offset 放在某些列 (6-9) 上,一切正常。我尝试引用的列都是工作表中的所有 IF 公式。不确定这会影响它吗?有人可以帮忙吗?
Sub TRAININGCreateAutomatedEmails2()
Sheets("EmailCode").Select
Dim EApp As Object
Set EApp = CreateObject("Outlook.Application")
Dim EItem As Object
Dim NList As Range
Set NList = Range("A3", Range("A3").End(xlDown))
Dim N As Range
For Each N In NList
Set EItem = EApp.CreateItem(0)
strbody = "<P STYLE='font-family:Arial;font-size:10pt'>"
If N.Offset(0, 20) = "Send email" Then
With EItem
.To = N.Offset(0, 22)
.Subject = "Your progress in TRAINING"
.CC = N.Offset(0, 22)
.HTMLBody = "<P STYLE='font-family:Arial;font-size:10pt;color: rgb(0,0,0)'>Dear " & N & ", " & "<br>" & "<br>" _
& "I hope all is going well for you." & "<br>" _
& "This email is to update you on the progress you have made so far on the Digital" _
& " content of the TRAINING program based on your start date in role of; " & N.Offset(0, 3) _
& "<br>" & "<br>" & "<br>" & "<u>""TRAINING 1-90""</u>" & "<br>" _
& "Expected Comp: " & N.Offset(0, 6) & "%." _
& "<br>" & "Actual Comp: " & N.Offset(0, 4) & "%." _
& "<br>" & "<br>" & "- " & N.Offset(0, 8) _
& "<br>" & "<br>" & "<u>""TRAINING 91-180""</u>" & "<br>" _
& "Expected Comp: " & N.Offset(0, 7) & "%." & "<br>" _
& "Actual Comp: " & N.Offset(0, 5) & "%." _
& "<br>" & "<br>" & "- " & N.Offset(0, 9) _
& "<br>" & "<br>" & "<br>" & "See you soon, " & "<br>" & "<br>" & "NAME MInstLM" _
& "<br>" & "Training | Leadership Manager" & "<br>" & "<br>" & "COMPANY Ltd" & "<br>" & "No. 1 Circle Square | STREET" & "<br>" & "CITY| POST CODE" _
.Display
End With
End If
Next N
End Sub
答:
0赞
Notus_Panda
9/11/2023
#1
最好在每个循环开始时检查值,这样就不会不必要地创建项目。我最终对偏移量进行了硬编码,因为我不确定检查错误是否可以遍历整个单元格的行。
Sub TRAININGCreateAutomatedEmails2()
Sheets("EmailCode").Select
Dim EApp As Object
Set EApp = CreateObject("Outlook.Application")
Dim EItem As Object
Dim NList As Range, strbody As String
Set NList = Range("A3", Range("A3").End(xlDown))
Dim N As Range, arr, arrOffset, i As Long
arrOffset = Array(20, 22, 3, 6, 4, 8, 7, 5, 9) ' these were all the ones used in your code
'you could check every cell for an error but not sure that's what is intended here
For Each N In NList
arr = N.Resize(, 23).Value 'arr is a variant array with dimensions (1 to 1, 1 To 23)
'these are all the values from A to W in the row of the current N
For i = 0 To UBound(arrOffset)
'Time to loop the values to see if any of the Offsets would throw an error
If IsError(arr(1, arrOffset(i) + 1)) Then GoTo Skip '+1 so it's an actual offset
'arr starts with index 1 (A-column), so .Offset(,22) = arr(1,23)
Next i
'No error was found so we continue as usual
Set EItem = EApp.CreateItem(0)
strbody = "<P STYLE='font-family:Arial;font-size:10pt'>"
If N.Offset(0, 20) = "Send email" Then
With EItem
.To = N.Offset(0, 22)
.Subject = "Your progress in TRAINING"
.CC = N.Offset(0, 22)
.HTMLBody = "<P STYLE='font-family:Arial;font-size:10pt;color: rgb(0,0,0)'>Dear " & N & ", " & "<br>" & "<br>" _
& "I hope all is going well for you." & "<br>" _
& "This email is to update you on the progress you have made so far on the Digital" _
& " content of the TRAINING program based on your start date in role of; " & N.Offset(0, 3) _
& "<br>" & "<br>" & "<br>" & "<u>""TRAINING 1-90""</u>" & "<br>" _
& "Expected Comp: " & N.Offset(0, 6) & "%." _
& "<br>" & "Actual Comp: " & N.Offset(0, 4) & "%." _
& "<br>" & "<br>" & "- " & N.Offset(0, 8) _
& "<br>" & "<br>" & "<u>""TRAINING 91-180""</u>" & "<br>" _
& "Expected Comp: " & N.Offset(0, 7) & "%." & "<br>" _
& "Actual Comp: " & N.Offset(0, 5) & "%." _
& "<br>" & "<br>" & "- " & N.Offset(0, 9) _
& "<br>" & "<br>" & "<br>" & "See you soon, " & "<br>" & "<br>" & "NAME MInstLM" _
& "<br>" & "Training | Leadership Manager" & "<br>" & "<br>" & "COMPANY Ltd" & "<br>" & "No. 1 Circle Square | STREET" & "<br>" & "CITY| POST CODE" _
.Display
End With
End If
'Debug.Print "Sent email to: " & N.Offset(0, 22) 'used for testing
Skip: 'immediately goes to the next N
Next N
End Sub
如果有什么不清楚的地方,请告诉我。
评论
0赞
Emma Sjöberg
9/11/2023
我不太确定这一切是如何设置的,但它效果很好!非常感谢!
0赞
Notus_Panda
9/11/2023
不客气,我在代码中做了更多的评论,所以它更清楚(我希望):)
评论