Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

excel - Get the data from copied filtered cells

If I copy some cells (simply containing numbers) from filtered column, how can I further reference this data from VBA?

If I try this:

Dim DataObj As MsForms.DataObject
Set DataObj = New MsForms.DataObject

DataObj.GetFromClipboard

Debug.Print DataObj.GetText(1)

I get this error:

DataObject:GetText Invalid FORMATETC structure

Is there any other way to access the data from copied filtered cells?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This works for me.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...