VBA, Copy, Remove Ants, Keep Clipboard

bepedicino

Board Regular
Joined
Sep 29, 2014
Messages
73
I have the following VBA which copies a dynamic range. However, I want to clear the marching ants, but retain the clipboard so I can paste into another application. Can anyone assist?

VBA Code:
Sub CopyData()
Dim a As Integer
    a = Sheets("Classification").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("Classification").Range("A1:I" & a).Copy
   
    Application.CutCopyMode = True
   
    ActiveSheet.Range("A2").Select
   
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
so I can paste into another application


Unrefined example to paste into Word
VBA Code:
Sub PasteToWord()
    Dim a As Integer
    a = Sheets("Classification").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("Classification").Range("A1:I" & a).Copy
    With CreateObject("Word.Application")
        .Visible = True
        .documents.Add
        .Selection.Paste
    End With
    Application.CutCopyMode = True
End Sub



Paste into Notepad
amend
strTempFile = "c:\folder\subfolder\temp.txt"

code cribbed from Copy Data from Excel to Notepad

VBA Code:
Sub OpenNotepadWithTempFileWithClipboardContent()
    Dim a As Integer
    Dim rngData As Range
    Dim strData As String
    Dim strTempFile As String

    ' copy some range values
    a = Sheets("Classification").Range("A" & Rows.Count).End(xlUp).Row
    Set rngData = Sheets("Classification").Range("A1:I" & a)
    rngData.Copy

    ' get the clipboard data
    ' magic code is for early binding to MSForms.DataObject
    With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
        .GetFromClipBoard
        strData = .GetText
    End With

    ' write to temp file
    strTempFile = "c:\folder\subfolder\temp.txt"
    With CreateObject("Scripting.FileSystemObject")
        ' true to overwrite existing temp file
        .CreateTextFile(strTempFile, True).Write strData
    End With

    ' open notepad with tempfile
    Shell "cmd /c ""notepad.exe """ & strTempFile & """", vbHide
 
    Application.CutCopyMode = True
End Sub
 
Upvote 0
I have the following VBA which copies a dynamic range. However, I want to clear the marching ants, but retain the clipboard so I can paste into another application. Can anyone assist?

You may find this of interest:
 
Upvote 0
Thank you Yongle for your assistance. Is there a way to simply retain the selected data on the clipboard after it is copy. I want to manually be able to paste it into a variable email reply.
 
Upvote 0
I do not think I understand your problem
If I run your code (below) I am able to paste into an email and the original clipboard value is retained
Can you explain exactly what you are trying to do
- why are you trying to clear the marching ants?

VBA Code:
Sub CopyData()
    Dim a As Integer
    a = Sheets("Classification").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("Classification").Range("A1:I" & a).Copy
    ActiveSheet.Range("A2").Select
End Sub
 
Upvote 0
The issue could be resolved by simply pasting the selection back with formatting. How can I do this?
 
Upvote 0
You are asking for help but do not tell us what you are unable to do

Your code allows you to copy the range
You are then pasting that into an email

What are you trying to do next that is not working?
 
Upvote 0
Clear the marching ants, to reduce chaos (Excel 2010):

Selection.Borders(xlEdgeBottom).LineStyle = xlNone
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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