如果我从过滤列中复制一些单元格(只包含数字),我该如何从VBA中进一步引用这些数据?
如果我试试这个:
Dim DataObj As MsForms.DataObject
Set DataObj = New MsForms.DataObject
DataObj.GetFromClipboard
Debug.Print DataObj.GetText(1)
我收到此错误:
DataObject:GetText Invalid FORMATETC structure
有没有其他方法可以从复制的过滤单元格中访问数据?
答案 0 :(得分:0)
这适合我。
Sub Tester()
Dim DataObj As MsForms.DataObject
Dim rng As Range, c As Range, rngV As Range
Set rng = Sheet1.Range("A2:A20") 'filtered range
On Error Resume Next
Set rngV = rng.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rngV Is Nothing Then
Debug.Print "No visible cells!"
Exit Sub
End If
'using Clipboard
rngV.Copy
Set DataObj = New MsForms.DataObject
DataObj.GetFromClipboard
Debug.Print DataObj.GetText '>> vbCrLf-delimited string
'direct read
For Each c In rngV.Cells
Debug.Print c.Value
Next c
End Sub