我想在Excel中运行宏来循环遍历多行,将过滤器应用于具有此人姓名的电子表格,截取屏幕截图,并使用屏幕截图向该人发送电子邮件。
我当前的代码不会遍历范围(只有1条记录),也不会截取屏幕截图并插入电子邮件。
非常感谢对此的帮助。
我目前的代码:
Sub SendEmailtoEachResource_Click()
' Macro - Intended functionality
' For each person (resource) apply a filter to the 'Allocation'
' tab, and take a screenshot. Send an email with the screenshot
' to each person.
Dim Resoucename As String
Dim ResouceEmail As String
'Current State: Apply filter, and send 1 email to the below details
ResourceName = Range("D4")
resourceEmail = Range("E4")
'ACTION - Future State:
'For each person in column D
'Send email to email address on same row in Coumn E
' ##Start Loop
'Go to Allocation Tab, Apply Filter of resouce name
Sheets("Allocation").Select
Range("A1:BH28").Select
ActiveSheet.Range("$A$8:$BI$826").AutoFilter Field:=5, Criteria1:= _
ResourceName
ActiveWindow.SmallScroll Down:=-21
ActiveWindow.ScrollRow = 9
Range("A1:BV836").Select
' ACTION: Take Screenshot of filtered results
'setup email
Dim aOutlook As Object
Dim aEmail As Object
Dim outlookResName As String
Dim SendAddress As String
Set aOutlook = CreateObject("Outlook.Application")
Set aEmail = aOutlook.CreateItem(0)
outlookResName = ActiveCell.Value
SendAddress = "me@email.com"
aEmail.To = resourceEmail
aEmail.Subject = "Resource assignment Report for " & ResourceName
aEmail.HTMLBody = "Your report is below {Insert Screenshot}"
'ACTION: Paste screenshot HERE
aEmail.display
' Will change to .send when VBA is working fully. This could send ~100 emails
' ## End LOOP
End Sub
答案 0 :(得分:0)
在我看来,你在这里汇总了两个问题:(1)如何循环遍历电子表格的各行;(2)如何截取屏幕截图并将其插入电子邮件中。也许您应该考虑发布两个单独的问题。
考虑到这一点,我将解决循环问题。有很多方法可以达到你想要的效果。
A)您可以使用行号
For i = 7 To 9
ResourceName = Cells(i, 4)
ResourceEmail = Cells(i, 5)
' The rest of your code here
Next i
B)你可以从第一行开始并继续向下移动,直到找到一个空单元格。
i = 7
Do Until Cells(i, 4) = ""
ResourceName = Cells(i, 4)
ResourceEmail = Cells(i, 5)
' The rest of your code here
i = i + 1
Loop
C)您可以为包含资源列表的单元格命名(例如," resources")并遍历其行。
Set MyList = ActiveWorkbook.Names("resources").RefersToRange
For Each person In MyList.Rows
ResourceName = person.Cells(1, 4)
ResourceEmail = person.Cells(1, 5)
' The rest of your code here
Next person
为什么不选择一种方法,然后我们会看到我们从哪里去?