How can I print a range variable in the immediate window? Excel VBA
Asked Answered
F

2

6

I'm attempting something that should be very simple. However I've just started learning today and and can't quite understand.

This is my code so far:

Public Sub getCellData()
   Dim wb As Workbook: Set wb = ThisWorkbook
   Dim ws As Worksheet: Set ws = wb.Sheets(1)
   Dim rng As Range: Set rng = ws.Range(Cells(1, 2), Cells(4, 2))

   Debug.Print rng
End Sub

The data that I'm working with is this:

enter image description here

I keep getting the "Run-time error '13': Type mismatch" I googled the error and I'm still unsure of how to fix this. I want to print the variable rng in the immediate window.

Fruity answered 29/11, 2017 at 1:33 Comment(2)
Is it the values in the range that you are trying to print?Reathareave
@JohnColeman Yes, the values in the range.Fruity
A
5

Range is an object, not a value. To output the values, you can iterate the Range. Another way is to use the Transpose function on a single row or column and then Join to get a String value of the array of values within the Range.

Sample code:

Public Sub getCellData()
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet: Set ws = wb.Sheets(1)

    ' you need to prefix Cells with ws. to clarify the reference
    Dim rng As Range: Set rng = ws.Range(ws.Cells(1, 2), ws.Cells(4, 2))

    ' you cannot debug print the object itself
    'Debug.Print rng

    ' iterate the range
    Dim rngCell As Range
    For Each rngCell In rng
        Debug.Print rngCell.Value
    Next rngCell

    ' use the Transpose function for a single row or column
    Dim strData As String
    Dim wsf As WorksheetFunction: Set wsf = Application.WorksheetFunction
    strData = Join(wsf.Transpose(rng.Value), ",")
    Debug.Print strData


End Sub

Note I updated your Set rng = ... to:

Set rng = ws.Range(ws.Cells(1, 2), ws.Cells(4, 2))

And added ws. as a prefix to Cells in order that the references were explicitly defined.

Abaddon answered 29/11, 2017 at 1:43 Comment(1)
Thank you. That worked for me. I appreciate your help :)Fruity
R
6

You could write a simple sub for something like this:

Sub PrintRange(R As Range, Optional delim As String = ", ")
    Dim myRow As Range, V As Variant, i As Long
    For Each myRow In R.Rows
        ReDim V(1 To myRow.Cells.Count)
        For i = 1 To myRow.Cells.Count
            V(i) = myRow.Cells(1, i).Value
        Next i
        Debug.Print Join(V, delim)
    Next myRow
End Sub

Then PrintRange rng would work as expected.

Reathareave answered 29/11, 2017 at 1:51 Comment(0)
A
5

Range is an object, not a value. To output the values, you can iterate the Range. Another way is to use the Transpose function on a single row or column and then Join to get a String value of the array of values within the Range.

Sample code:

Public Sub getCellData()
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet: Set ws = wb.Sheets(1)

    ' you need to prefix Cells with ws. to clarify the reference
    Dim rng As Range: Set rng = ws.Range(ws.Cells(1, 2), ws.Cells(4, 2))

    ' you cannot debug print the object itself
    'Debug.Print rng

    ' iterate the range
    Dim rngCell As Range
    For Each rngCell In rng
        Debug.Print rngCell.Value
    Next rngCell

    ' use the Transpose function for a single row or column
    Dim strData As String
    Dim wsf As WorksheetFunction: Set wsf = Application.WorksheetFunction
    strData = Join(wsf.Transpose(rng.Value), ",")
    Debug.Print strData


End Sub

Note I updated your Set rng = ... to:

Set rng = ws.Range(ws.Cells(1, 2), ws.Cells(4, 2))

And added ws. as a prefix to Cells in order that the references were explicitly defined.

Abaddon answered 29/11, 2017 at 1:43 Comment(1)
Thank you. That worked for me. I appreciate your help :)Fruity

© 2022 - 2024 — McMap. All rights reserved.