作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我得到 Next 没有 For 错误消息 - 我错过了什么? “下一个 aCell”上的错误
此代码的目的是确定 Gmail 电子邮件中的 From、To。标准基于 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
最佳答案
这是您的代码,有一些更改:
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
.Cells
在第一个 For...Each
的末尾按照上面评论中的建议循环。 For...Each
没有意义。因此,我将其删除。 strTo
每次在偏移单元格中找到“收件人”电子邮件地址时连接起来。要分隔这些电子邮件地址,请使用 ,
被添加。您可以将其更改为 ;
如果这是你需要的。 Offset
大多不正确(基于提供的样本数据并已进行调整)。 sub
现在搜索工作表 Contact_Rev
在使用它之前。事实证明(在提供的示例文件中)没有该名称的工作表。工作表名称为 Contact rev
(没有下划线)。因此,找不到工作表,代码无法工作。 关于excel - For Each - 在没有 for 的情况下编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38064319/
我是一名优秀的程序员,十分优秀!