Macro that copies to clipboard

helmonder

New Member
Joined
Jul 27, 2007
Messages
1
I was thinking this would be an easy one to solve but I cannot get it right..

I need to have piece of code (used through a macro or a button) that will take the contents of a cell and copy it to the clipboard.

Simply CTRL-C on a cell will not work as it takes carriage-return feeds with it that mess up the data on the clipboard..

Any help is appreciated, I am not to big an Excel wizzard as you might imagine..
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Why not use right click> Copy?

Selection.Copy

If your copying a selected cell:

Sub copyme ()
Selection.Copy
End Sub


Attach to a button right click on top menu of excel choose the forms menu....from the forms menu choose 'button' it will prompt you to attach a macro, attach the one above or record your own using the macro recorder?
 
Upvote 0
This set of procedures will search the VBA modules for all code procedures, even the Private ones!
Then it lists them all, by name, and ask you which one you want. It then displays the one you picked, as much as will fit in a MsgBox and then copies the whole procedure, not just the pre-view just displayed, to the ClipBoard. So, you can then paste it where you want.

All this code goes into a Standard module, like: Module1.
Note: You will need to create a reference to:
"MicroSoft Visual Basic for Applications Extensibilty x.x" for, returning the VBA parts
and for the DataObject: "MicroSoft Forms x.x Object Library" reference!
Do this using the VBA Editor Toolbar: Tools - References.



Public strProcedureNms$, strThisProcedureText$

Function fuc_ModTypeNm(objComponent As VBComponent) As String
'Standard Module code, like: Module1.

Select Case objComponent.Type
Case vbext_ct_ActiveXDesigner
fuc_ModTypeNm = "ActiveX Designer Module"

Case vbext_ct_ClassModule
fuc_ModTypeNm = "Class Module"

Case vbext_ct_Document
fuc_ModTypeNm = "Sheet/Document Module"

Case vbext_ct_MSForm
fuc_ModTypeNm = "MS Form Module"

Case vbext_ct_StdModule
fuc_ModTypeNm = "Standard Module"

Case Else
fuc_ModTypeNm = " Module UnKnown?"
End Select
End Function

Private Sub fs_ListProcedures()
'Standard Module code, like: Module1.
Dim objComponent As VBComponent
Dim VBCodeMod As CodeModule
Dim lngStartLine&

strProcedureNms = ""

For Each objComponent In ActiveWorkbook.VBProject.VBComponents

Set VBCodeMod = ActiveWorkbook.VBProject.VBComponents(objComponent.Name).CodeModule

With VBCodeMod
lngStartLine = .CountOfDeclarationLines + 1

Do Until lngStartLine >= .CountOfLines

strProcedureNms = strProcedureNms & .ProcOfLine(lngStartLine, vbext_pk_Proc) & vbLf

lngStartLine = lngStartLine + _
.ProcCountLines(.ProcOfLine(lngStartLine, _
vbext_pk_Proc), vbext_pk_Proc)
Loop
End With
Next objComponent
End Sub

Private Sub fs_codeToClipBoard()
'Standard module code, like: Module1.
Dim objCodeData As Object

'Note: You will need to create a reference to:
'"MicroSoft Visual Basic for Applications Extensibilty x.x" for, returning the VBA parts
'and for the DataObject: "MicroSoft Forms x.x Object Library" reference!
'Do this using the VBA Editor Toolbar: Tools - References.


Set objCodeData = New DataObject

objCodeData.SetText strThisProcedureText
objCodeData.PutInClipboard
End Sub


Sub DisplayProceedureText()
'Standard Module code, like: Module1.
'Note: You will need to create a reference to MS VBA Extensibilty 5.3
'(in VBE go to Tools>References) to work this code, it was set in this version!


Dim strThisProcedureNm$, strProcNm$, strTestName$
Dim lng1stProcLineNum&, lngProcStartLineNum&, lngThisProcsLineCnt&, lngMyBad&
Dim VBCodeMod As CodeModule
Dim VBComp As VBComponent
Dim booGotOne As Boolean

myAgain:
strThisProcedureText = ""
strProcedureNms = ""

Call fs_ListProcedures

strProcNm = InputBox("To display a procedure's code text," & vbLf & _
"enter its name, from this list, below:" & vbLf & vbLf & _
strProcedureNms, _
"Display a Procedure's Code Lines!")

If strProcNm = "" Then GoTo myEnd

For Each VBComp In ActiveWorkbook.VBProject.VBComponents

Set VBCodeMod = ActiveWorkbook.VBProject.VBComponents(VBComp.Name).CodeModule

lng1stProcLineNum = VBCodeMod.CountOfDeclarationLines + 1

myNextProc:
If lng1stProcLineNum >= VBCodeMod.CountOfLines Then GoTo myNext
strThisProcedureNm = VBCodeMod.ProcOfLine(lng1stProcLineNum, vbext_pk_Proc)

If (strThisProcedureNm = strProcNm And strThisProcedureNm <> "") Then booGotOne = True

strTestName = VBCodeMod.ProcOfLine(lng1stProcLineNum, vbext_pk_Proc)

lng1stProcLineNum = lng1stProcLineNum + _
VBCodeMod.ProcCountLines(VBCodeMod.ProcOfLine(lng1stProcLineNum, _
vbext_pk_Proc), vbext_pk_Proc)
lngProcStartLineNum = VBCodeMod.ProcStartLine(strThisProcedureNm, vbext_pk_Proc)
lngThisProcsLineCnt = VBCodeMod.ProcCountLines(strThisProcedureNm, vbext_pk_Proc)

If booGotOne = True Then
strThisProcedureText = VBCodeMod.Lines(lngProcStartLineNum, lngThisProcsLineCnt)
GoTo myShowIt
Else
GoTo myNextProc
End If

myNext:
Next VBComp

myShowIt:
If InStr(1, strProcedureNms, strProcNm, vbTextCompare) = 0 Then GoTo badNm

Call fs_codeToClipBoard

MsgBox "Note: All of this procedure was sent to the ClipBoard," & vbLf & _
"even if only some is displayed here!" & vbLf & vbLf & _
strThisProcedureText, _
vbInformation + vbOKOnly, _
"Display of: " & "testCodeMacro"
GoTo myEnd

badNm:
lngMyBad = MsgBox("The Procedure name you typed:" & vbLf & vbLf & _
"""" & strProcNm & """" & vbLf & vbLf & _
"Was not fround!" & vbLf & _
"You may have entered an incorrect name?" & vbLf & vbLf & _
"Try Again?", _
vbCritical + vbYesNo, _
"Display Procedure from Module: Error!")

If lngMyBad = 6 Then GoTo myAgain

myEnd:
End Sub
 
Upvote 0
Joe,

Is this in response to a PM or something? Otherwise, I don't see where the OP asked about listing/copying contents of the VB project.
 
Upvote 0
I got side-tracked by the Subject "Macro to ClipBoard" part and thought if someone else did a search and wanted to send macro code to the clipboard this would show them how. Your link answers the "Cell to ClipBord" question.
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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