VBA对过滤的Excel进行截图,并在迭代中发送到每一行

我想在Excel中运行一个macros来循环遍历一些行,将filter应用到电子表格中,并用这个人的名字,截图,然后用截图发送一封电子邮件给那个人。

我目前的代码不会遍历一个范围(只有一个logging),并不会截图并插入到电子邮件。

将不胜感激此援助。

我目前的代码:

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 

在我看来,在这里有两个问题汇总在一起:(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)你可以从第一行开始,继续往下走,直到find一个空单元格。

 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 

你为什么不select一种方法,然后我们看到我们去哪里?