在Excel VBA中将循环从每个单元格更改为特定范围

在Excel VBA中将循环从每个单元格更改为特定范围,excel,loops,for-loop,vba,Excel,Loops,For Loop,Vba,我有下面的一段代码,它可以批量发送电子邮件 Sub Sengrd_Files() Dim OutApp As Object Dim OutMail As Object Dim sh As Worksheet Dim cell As Range Dim FileCell As Range Dim rng As Range para2 = "" para3 = "" para232 = Range("AA2").Value With Ap

我有下面的一段代码,它可以批量发送电子邮件

Sub Sengrd_Files()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim cell As Range
    Dim FileCell As Range
    Dim rng As Range
para2 = ""
para3 = ""

para232 = Range("AA2").Value

    With Application
        .EnableEvents = False
        .ScreenUpdating = True
    End With

    Set sh = Sheets("Sheet1")

    Set OutApp = CreateObject("Outlook.Application")

    For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)

        'Enter the path/file names in the C:Z column in each row
        Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")

        If cell.Value Like "?*@?*.?*" And _
           Application.WorksheetFunction.CountA(rng) > 0 Then
            Set OutMail = OutApp.CreateItem(0)

            With OutMail
                .to = cell.Value
                .Subject = "Circle Profitability Report for the period ended 30-NOV-2017"
                .Body = "Dear Sir/Madam," _
& vbNewLine _
    & para232 & vbNewLine _
    & vbNewLine & para2 & vbNewLine _
    & Remark & vbNewLine & vbNewLine _
    & para3 & vbNewLine & vbNewLine


                For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                    If Trim(FileCell) <> "" Then
                        If Dir(FileCell.Value) <> "" Then
                            .Attachments.Add FileCell.Value
                        End If
                    End If
                Next FileCell

                .Send  'Or use .Display
            End With

            Set OutMail = Nothing
        End If
    Next cell

    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub
我无法定义变量I并将上面的行更改为

**For Each i =1 to 5 sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)** 

由于语法错误。有谁能帮我用语法将“For each cell in”替换为一个有限范围。

这是如何使批量邮件发件人只发送到给定范围(在本例中为B2-B5):


别忘了——垃圾邮件很糟糕。

为什么你的代码看起来像是被卡车撞到的?至少,删除多余的换行符。为什么要使用
i
变量?是什么让您认为这行是问题所在?您尝试过单步调试代码吗?@Michel-在不使用
.Cells.SpecialCells(xlcelltypestants)
部分的情况下对其进行修改。当我更改为“For Each cell In sh.Range(“B2:B5”).Cells.SpecialCells(xlcelltypestants)”时,它将限制为B5,但会发送第1封邮件4项:(@Michel-当您只为sh.Range(“B2:B5”)中的每个单元格写入
时,它会将其发送到
B2
B3
B4
B5
。如果您想包含
B6
,则为sh.Range(“B2:B6”)中的每个单元格写入
。并且不要写入
。Cells.SpecialCells(cellTypeConstants)
part.非常感谢……本应避免询问……我忙了1到5个小时,几乎忘了我可以在接下来的几小时内修改sh.Columns(“B”)words@Michel-提问总是好的,尤其是当你给出你的代码和你迄今为止尝试过的东西时。
**For Each i =1 to 5 sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)** 
For Each cell In sh.Range("B2:B5")