0

I am trying to populate a dynamic named range into a msgBox, but getting error of type mismatch on line Set xRg = xTxt.

Below is the code:

Sub showOfferRange()
Dim xRg As Range
Dim xTxt As String
Dim xCell As Range
Dim xStr As String
Dim xRow As Long
Dim xCol As Long

On Error Resume Next
 xTxt = ThisWorkbook.Names("named_range")
Set xRg = xTxt

If xRg Is Nothing Then Exit Sub
    On Error Resume Next
    For xRow = 1 To xRg.Rows.Count
        For xCol = 1 To xRg.Columns.Count
            xStr = xStr & xRg.Cells(xRow, xCol).Value & vbTab
        Next
    xStr = xStr & vbCrLf
    Next

MsgBox xStr

End Sub

Much appreciated.

1 Answer 1

3

You cannot set a Range object to a String variable. Hence the type mismatch.

Do this:

Set rng = Range("named_range") 'since it's a workbook level name anyway

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! Worked as expected
@joell - great. consider marking as answered so others know moving forward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.