gpt4 book ai didi

excel - For Each - 在没有 for 的情况下编译错误

转载 作者:行者123 更新时间:2023-12-04 20:59:58 24 4
gpt4 key购买 nike

我得到 Next 没有 For 错误消息 - 我错过了什么? “下一个 aCell”上的错误

enter image description here

此代码的目的是确定 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

用 .cells 尝试过,但两者都没有给出下标错误。

最佳答案

这是您的代码,有一些更改:

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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com