0

セルの範囲にある複数の受信者の電子メールを追加しようとしています。

シートでメールの範囲を選択できます。

ただし、この不一致エラーが発生し続け、解決方法がわかりません。

私は解決策を探し回っていて、同じ手順を実行しました。

私を許してください、私はVBAが初めてです。大変お世話になりました。私のコードは以下のとおりです。

    Private Sub CommandButton1_Click()

        Dim olapp As Object
        Dim olmail As Object
        Dim recip As String


    lastr = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row 
    'this is for the range of data to be copied on the body but have yet to do it

    lastr2 = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 7).End(xlUp).Row


        recip = ThisWorkbook.Sheets("Sheet1").Range("G3:G" & lastr2).Value 
        'mismatch after this step

        Set olapp = CreateObject("Outlook.Application")

        Set olmail = olapp.CreateItem(0)

        With MItem
         .to = recip
         .Subject = "hello"
         .Body = "whats up"
         .display
         End With

なぜこれが起こっているのですか?

4

1 に答える 1

1

You're trying to assign an array (a range of multiple cells is an Array) to a string variable. WIthout testing, I know you can resolve this with a For Each loop, as Jaycal's comment suggested:

Dim cl as Range
For each cl in ThisWorkbook.Sheets("Sheet1").Range("G3:G" & lastr2).Cells
    recip = recip & ";" & cl.Value    
Next

But you could simplify by using the string Join function. The Join function effectively performs this loop on an array of strings, so it saves you an unnecessary loop. I modify to use a range variable for legibility:

Dim sendRange as Range
Set sendRange = ThisWorkbook.Sheets("Sheet1").Range("G3:G" & lastr2)
recip = Join(Application.Transpose(sendRange.Value), ";")

Whichever method you use, you will be able to use the same With block.

    With MItem
     .to = recip
     .Subject = "hello"
     .Body = "whats up"
     .display
     End With
于 2013-11-13T14:30:12.133 に答える