For Each - 下一个没有for的编译错误

时间:2016-06-27 22:40:15

标签: excel excel-vba vba

我收到Next而没有For错误消息 - 我错过了什么? "下一个aCell"

上的错误

enter image description here

此代码的目的是确定Gmail电子邮件中的“发件人”,“收件人”。标准基于excel工作簿中的表。

Sub SetsEmailAddress()

Dim aCell As Range
Dim aCell_1 As Long
Dim strFrom As String
Dim strTo As String
Dim MyString As String

For Each aCell In Sheets("Contact_Rev").Range("A2:A5").Cells
If (aCell.Offset(1, 0).Value) = Sheets("Contact_Rev").Range("P2").Cells Then
   If aCell.Offset(1, 2).Value = "From" Then
        strFrom = aCell.Offset(1, 3).Value
    End If
    Select Case aCell.Offset(1, 2).Value
        Case "Val"
            strTo = (aCell.Offset(1, 3).Value)
        Case "Invoice"
            If IsNumeric(aCell.Offset(1, 3).Value) Then
                For aCell_1 = 1 To aCell.Offset(1, 3).Value
                    strTo = ""
                    strTo = strTo & "" & (aCell.Offset(1, 3).Value) & strTo & "'"
                Next aCell_1
            End If
    End Select
    End If
 Next aCell
end sub

使用.cells尝试并且两者都没有给出下标错误。

2 个答案:

答案 0 :(得分:0)

以下是您的代码,其中包含一些更改:

Option Explicit

Sub SetsEmailAddress()

Dim aCell As Range
Dim aCell_1 As Long
Dim strFrom As String
Dim strTo As String
Dim MyString As String

Dim bolFound As Boolean
Dim ws As Worksheet

bolFound = False
For Each ws In ThisWorkbook.Worksheets
    If ws.Name = "Contact rev" Then bolFound = True
Next ws
If bolFound = False Then
    MsgBox "Couldn't find the required sheet." & Chr(10) & "Aborting..."
End If

With ThisWorkbook.Worksheets("Contact rev")
    For Each aCell In .Range("A2:A9")
        If aCell.Offset(0, 1).Value2 = .Range("M2").Value2 Then
            If aCell.Offset(0, 2).Value2 = "From" Then
                strFrom = aCell.Offset(0, 3).Value2
            End If
            Select Case aCell.Offset(0, 2).Value2
                Case "Val"
                    strTo = "'" & aCell.Offset(0, 3).Value2 & "'"
                Case "Invoice"
                    strTo = strTo & ",'" & aCell.Offset(0, 3).Value2 & "'"
            End Select
        End If
    Next aCell
End With

End Sub

的变化:

  1. 我按照上面评论中的建议删除了第一个.Cells循环结束时的For...Each
  2. 第二个For...Each没有意义。因此,我删除了它。
  3. 现在,strTo会在偏移单元格中找到每个“收件人”电子邮件地址的连接。要分隔这些电子邮件地址,请添加,。如果您需要,可以将其更改为;
  4. Offset大部分不正确(根据提供的样本数据进行了调整)。
  5. 最后(最重要的是)sub现在在使用之前搜索工作表Contact_Rev。事实证明(在提供的示例文件中)没有该名称的工作表。工作表名称为Contact rev(没有下划线)。因此,找不到工作表,代码无法工作。

答案 1 :(得分:0)

你错过了,并且“结束如果”接近尾声 - 试试这个:

Sub SetsEmailAddress()
Dim aCell As Range
Dim aCell_1 As Range
Dim strFrom As String
Dim strTo As String
Dim MyString As String

For Each aCell In Sheets("Contact_Rev").Range("A2:A5").Cells

    If (aCell.Offset(1, 0).Value) = Sheets("Contact_Rev").Range("P2").Cells Then
       If aCell.Offset(1, 2).Value = "From" Then
            strFrom = aCell.Offset(1, 3).Value
        End If
        Select Case aCell.Offset(1, 2).Value
            Case "Val"
                strTo = (aCell.Offset(1, 3).Value)
            Case "Invoice"
                For Each aCell_1 In aCell.Offset(1, 3).Value
                    strTo = ""
                    strTo = strTo & "" & (aCell.Offset(1, 3).Value) & strTo & "'"
                Next aCell_1
        End Select
    End If

Next aCell
End Sub
相关问题