Only copy selected cells to clipboard

most

Board Regular
Joined
Feb 22, 2011
Messages
106
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. Mobile
If a select three cells with Ctrl and then Ctrl-C and Ctrl-V into a new cell I get the result as you can below, "b d f".

Screenshot_1.png


But if I paste it into a text file, I get "b c d e f"!? How come?
Is it possible to convert the clipboard data "correctly" with macro?

I'm using the clipboard data for this macro, which updates the auto filter with data from the clipboard.
I need to use clipboard since I some times copy data from other sources.
VBA Code:
Sub UpdateAutoFilterClipboard()
  Dim a As Variant
 If ActiveSheet.AutoFilterMode = False Then
   MsgBox "There is no autofilter in active sheet!"
  Else
    a = ClipToArray()
    ActiveCell.AutoFilter Field:=ActiveCell.Column, Criteria1:=a, Operator:=xlFilterValues
 End If
End Sub
Function ClipToArray() As Variant
     Dim clip As New MsForms.DataObject
     Dim lines As String
     clip.GetFromClipboard
     lines = clip.GetText
     lines = Replace(lines, vbCr, "")
     ClipToArray = Split(lines, vbLf)
End Function
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Here is a workaround :

1- Put this code in the ThisWorkbook Module:
VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function GetClipboardSequenceNumber Lib "user32" () As Long
#Else
    Private Declare Function GetClipboardSequenceNumber Lib "user32" () As Long
#End If

Private WithEvents cmbrs As CommandBars

Public ar As Variant

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Set cmbrs = Application.CommandBars
    Call cmbrs_OnUpdate
End Sub

Private Sub cmbrs_OnUpdate()

    Static lPrevSN As Long
    Dim oCell As Range, i As Long
    
    If GetClipboardSequenceNumber <> lPrevSN Then
        If Application.CutCopyMode = xlCopy Then
            On Error Resume Next
                Erase ar
            On Error GoTo 0
            ReDim ar(Selection.Cells.Count - 1)
            For Each oCell In Selection
                ar(i) = oCell.Value
                i = i + 1
            Next oCell
        End If
    End If
    lPrevSN = GetClipboardSequenceNumber

End Sub


2- In a Standard Module:
VBA Code:
Option Explicit

Sub UpdateAutoFilterClipboard()
    Dim a As Variant
    If ActiveSheet.AutoFilterMode = False Then
        MsgBox "There is no autofilter in active sheet!"
    Else
        a = GetArray()
        ActiveCell.AutoFilter Field:=ActiveCell.Column, Criteria1:=a, Operator:=xlFilterValues
    End If
End Sub

Function GetArray() As Variant
    GetArray = ThisWorkbook.ar
End Function
 
Upvote 0
Nice solution. Thanks a bunch!
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,917
Members
449,093
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top