Range
是一个对象,而不是一个值。要输出值,您可以迭代Range
. 另一种方法是Transpose
在单个行或列上使用该函数,然后Join
获取.String
Range
示例代码:
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
请注意,我将您的更新Set rng = ...
为:
Set rng = ws.Range(ws.Cells(1, 2), ws.Cells(4, 2))
并添加ws.
为前缀Cells
,以便显式定义引用。