【问题标题】:cannot assign to array error for loop无法分配给循环的数组错误
【发布时间】:2023-03-18 18:08:01
【问题描述】:

我希望此代码过滤掉一些不同的值并将结果复制到新的电子表格中,将其保存为人名(带日期),然后将其作为附件发送。我有两个数组,一个包含姓名,另一个包含电子邮件。

但是,我遇到了一些问题。我在代码行中收到错误“无法分配给数组”:JCMails = Array("x@rvant.co.uk", "Y@rvant.co.uk")。

Sub LOOKUP()   
  Dim myFilename As String
  Dim JCNames(2) As String
  Dim JCMails(2) As String
 JCNames = Array("x", "Y")
 JCMails = Array("X@rvnt.co.uk", "Y@rvnt.co.uk")
 Dim i As Long
 Dim j As Integer

j = 0
For i = 0 To UBound(JCNames)
myFilename = JCNames(i) & Format(Now(), "ddmmyyyy")

Range("G1").Select
ActiveSheet.Range("A:N").AutoFilter Field:=7, Criteria1:=JCNames(i)

With ActiveSheet
         lRows = .Cells(.Rows.Count, "B").End(xlUp).Row
         lCols = .Cells(1, .Columns.Count).End(xlToLeft).Column
         .Range(Cells(1, 1), Cells(lRows, lCols)).Copy
End With
Workbooks.Add

Range("A1").Select
ActiveSheet.Paste
Columns.AutoFit

ActiveWorkbook.SaveAs Filename:=myFilename, FileFormat:=xlOpenXMLWorkbook

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
    .To = JCMails(i)
    .CC = ""
    .BCC = ""
    .Subject = "delays for" & JCNames
    .Body = "Hi there" & JCNames
    .Attachments.Add ActiveWorkbook.FullName
    .Display   'or use .Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
i = i + 1
j = j + 1
Next i
end sub

请有人告诉我我的循环做错了什么!? :(

【问题讨论】:

    标签: arrays excel vba loops


    【解决方案1】:

    Array 返回一个包含数组的变体。您需要简单地将 JCMails 和 JCNames 声明为 Variant 类型——而不是数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多