My macro breaks when range has only one line of text

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
482
Office Version
  1. 365
Platform
  1. Windows
This is seemingly related to my last question since it also broke in a similar way with a single-line range (yet it's different): UserForm won't open because ListBox has only one line of text?

I have code that sends an email using a Range as the body of the email. After sending the email it writes an entry in a log using the Range as a comment (still with me?).

The email sends correctly, but the code stops when it tries to write the range data in a comment.

Similar to the linked issue above, when the range has two lines of data it work, when it's only a single line of data I get this error:

Run-time error '13':

Type mismatch


request1.png


Here's the code:

VBA Code:
Sub SendPlanRequestUpdateEmail()
   
    Dim Ans As VbMsgBoxResult
    Ans = MsgBox("Are you sure you want to Send a Floor Plan Request Update?", vbYesNo + vbQuestion)
    If Ans = vbNo Then Exit Sub
   
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
   
    Sheets("FloorPlanRequests").Visible = True
    Sheets("FloorPlanRequests").Select
    ActiveSheet.Unprotect

    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next
    Set rng = Selection.SpecialCells(xlCellTypeVisible)
    Set rng = Sheets("FloorPlanRequests").Range("FloorPlanRequests").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If

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

    On Error Resume Next
    With OutMail
        .To = Settings.DesignerEmailReturn.Value
        .CC = Settings.CCEmailReturn.Value
        .BCC = Settings.BCCEmailReturn.Value
        .Subject = "Floor Plan Request Update for " & Format(Now, "m/dd/yy")
        .htmlbody = "<span style=""font-size:14pt;"">In order of priority:" & RangetoHTML(rng) & "<br>" & Settings.FloorPlanRequestMessage.Value
        .Send
    End With
    On Error GoTo 0
   
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    Sheets("FloorPlanRequests").Visible = False
   
    'Insert Blank Row In MasterLog
    Sheets("MasterLog").Visible = True
    Sheets("MasterLog").Select
    ActiveSheet.Unprotect
    Rows("10:10").Select
    Selection.Insert Shift:=xlDown
    Worksheets("MasterLogTemplate").Range("1:1").Copy Worksheets("MasterLog").Range("10:10")
       
    'Log Timestamp
    Sheets("MasterLog").Range("A10") = [Text(Now(), "MM/DD/YYYY HH:MM:SS AM/PM")]
   
    'Log Update
    Dim X As Variant
    X = Sheets("FloorPlanRequests").Range("FloorPlanRequests").Value
    With Range("E10")
      .AddComment
      .Comment.Text Join(Application.Transpose(X), Chr(10))
      With .Comment.Shape.TextFrame.Characters.Font
         .Name = "Tahoma"
         .Size = 12
      End With
      .Comment.Shape.TextFrame.AutoSize = True
    End With
  
    'Log Recipient
    Sheets("MasterLog").Range("C10").Value = Settings.DesignerNames.Value
   
    'Log Action
    Sheets("MasterLog").Range("E10").Value = "Boise Plan Request Update Sent"
   
    'Log Name
    Sheets("MasterLog").Range("I10") = Application.UserName
         
    'Sort MasterLog
    Application.Goto Reference:="MasterLogTimeStampColumn"
    ActiveWorkbook.Worksheets("MasterLog").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("MasterLog").Sort.SortFields.Add2 Key:=Range( _
        "MasterLogTimeStampColumn"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("MasterLog").Sort
        .SetRange Range("MasterLogAllColumns")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
         
    With Range("MasterLogTimestampColumn")
    .Range("A" & .Rows.Count).EntireRow.Select
    End With
    ActiveWindow.SmallScroll Down:=-18
    Range("A" & (ActiveCell.Row)).Select
       
    'Goto Last Row of MasterLog
    With Range("MasterLogTimestampColumn")
    .Range("A" & .Rows.Count).EntireRow.Select
    End With
    Range("A" & (ActiveCell.Row)).Select
      
    Sheets("MasterLog").Visible = False
    Sheets("MasterLogTemplate").Visible = False
    Sheets("MasterLogGrid").Visible = False

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
   
    Sheets("Calendar").Select
  
End Sub

Single-line of data in Range does not work:

request2.png


Two (or more) lines of data in the Range works:

request3.png
 
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
How about
VBA Code:
    Dim X As Variant
    X = Sheets("FloorPlanRequests").Range("FloorPlanRequests").Value
    If IsArray(X) Then
      X = Join(Application.Transpose(X), Chr(10))
    End If
    With Range("E10")
      .AddComment
      .Comment.Text X
 
Upvote 0
Solution
How about
VBA Code:
    Dim X As Variant
    X = Sheets("FloorPlanRequests").Range("FloorPlanRequests").Value
    If IsArray(X) Then
      X = Join(Application.Transpose(X), Chr(10))
    End If
    With Range("E10")
      .AddComment
      .Comment.Text X

Bravo! Works great. Thanks.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,604
Messages
6,173,319
Members
452,510
Latest member
RCan29

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top