3つの異なる範囲をループさせて、それぞれの変数に値を入れるために、キー付きコレクションコード(thanks @ Mat'sMug!)を変更しようとしています。最初のキー付きのコレクションが正常に動作しますが、二番目の(そしてそれが二過ぎて到達したら、私は三分の一を推測している)、寸法やDim(x to y)
またはReDim(x to y)
y
でredimensioningときラインReDim ccAddresses(0 To ccRecipients.Count - 1)
VBA:複数のキー付きコレクションの再調整=エラー9 '下付き文字が範囲外です。'
Private Sub AddUniqueItemToCollectionzz(ByVal value As String, ByVal items As Collection)
On Error Resume Next
items.Add value, Key:=value
On Error GoTo 0
End Sub
Sub Sampletest()
Dim toRecipients As Collection
Set toRecipients = New Collection
Dim ccRecipients As Collection
Set ccRecipients = New Collection
Dim cc2Recipients As Collection
Set cc2Recipients = New Collection
'===============Copy primary email addresses=============
With toRecipients
For Each cell In Range("H1:H350")
If cell.value Like "*@*.*" Then
AddUniqueItemToCollectionzz cell, toRecipients
End If
Next
End With
ReDim toAddresses(0 To toRecipients.Count - 1)
Dim toAddress As Variant, toItem As Long
For Each toAddress In toRecipients
toAddresses(toItem) = CStr(toAddress)
toItem = toItem + 1
Next
Dim sendToPrim As String
sendToPrim = Join(toAddresses, ";")
'=====================Copy cc email addresses======================
With ccRecipients
For Each cell In Range("J1:J350")
If cell.value Like "*@*.**" Then
AddUniqueItemToCollectionzz cell, ccRecipients
End If
Next
End With
ReDim ccAddresses(0 To ccRecipients.Count - 1)
Dim ccAddress As Variant, ccItem As Long
For Each ccAddress In ccRecipients
ccAddresses(ccItem) = CStr(ccAddress)
ccItem = ccItem + 1
Next
Dim sendToCC As String
sendToCC = Join(ccAddresses, ";")
'====================Copy cc2 email addresses================
With cc2Recipients
For Each cell In Range("A1:a350")
If cell.value Like "*.uSA.TACO*" Then
AddUniqueItemToCollectionzz cell, cc2Recipients
End If
Next
End With
ReDim cc2Addresses(0 To cc2Recipients.Count - 1)
Dim cc2Address As Variant, cc2Item As Long
For Each ccAddress In cc2Recipients
cc2Addresses(cc2Item) = CStr(cc2Address)
cc2Item = cc2Item + 1
Next
Dim sendToCC2 As String
sendToCC2 = Join(cc2Addresses, ";")
「cc2Recipients」とは何が呼び出されるのですか?私はどこに何を設定するか、最初にコレクションに追加することはありません。その行の前に 'debug.print cc2Recipients.count'を置くと、エラーが出るか、' 0'が返されます。 – BruceWayne
プロシージャの開始と終了の場所を指定するのは難しいです - あなたのコード[ここ](http://rubberduckvba.com/indentation)(免責事項:私はそのWebサイトを所有しています)を貼り付け、 "インデント!ボタン=) –
@ BruceWayne解決済み。上記のアップデートをご覧ください。 – Gurrito