Copy Code From a one excel to other through Macro

  • Thread starter Pavan Kishore K. S.
  • Start date
P

Pavan Kishore K. S.

Hi,

I have excel1.xls and excel2.xls. In excel1.xls i have a certain bit of code
that i need to copy into excel2.xls. The code is for the command button i
have in excel2.xls. This code runs when i click the command button.

the code i am using to copy is:
Sub CopyModule(SourceWB As Workbook, strModuleName As String, TargetWB As
Workbook)

' copies a module from one workbook to another
' example:
' CopyModule Workbooks("Book1.xls"), "Module1", Workbooks ("Book2.xls")
Dim strFolder As String, strTempFile As String
strFolder = SourceWB.Path
If Len(strFolder) = 0 Then strFolder = CurDir
strFolder = strFolder & ""
strTempFile = strFolder & "~tmpexport.bas"
On Error Resume Next
SourceWB.VBProject.VBComponents(strModuleName).Export strTempFile
TargetWB.VBProject.VBComponents(5).Import strTempFile

Kill strTempFile
On Error GoTo 0
End Sub

The code that needs to be copied is something like this..
Private Sub CommandButton1_Click()
MsgBox "hi"
End Sub

This code needs to be copied into the "Sheet1". But when i copy the code it
goes into a new module of the excel2.xls instead of Sheet1.

What is the code i should follow inorder to copy the code into the sheet1
instead of a module?
 
P

pikus

in your VBA editor, go to the main menu.
View-->Project Explorer.

Once you have your code copied, use the PE to navigate to the prope
module and double-click it to open the code window. Paste your cod
there. - piku
 
P

Pavan Kishore K. S.

Hi Pikus,

Thanks for the answer, The situation is as follows:
1) The excel sheet is being used by about 100 associates of my organization
and i cant ask them to copy and paste it manually.
2) Hence, i need to get it done automatically on the click of a button.
Hence i require to do it through a macro.

Any ideas about the same.

Thanks

Pavan
 
P

Pavan Kishore K. S.

Ok.. i managed to solve this.. here is the code for others to use

rivate Sub CommandButton1_Click()
'Define an excel object
Dim objExcel As Object, ExcelToBeOpened As String, sFileName As String

'Open the excel sheet which needs to be changed
Set objExcel = CreateObject("Excel.Application")
ExcelToBeOpened = Application.GetOpenFilename(filefilter:="Excel Files,
*.xls")
'Introduce error handling. We will have an error when we run out of files
On Error GoTo fileNotOpened
objExcel.Workbooks.Open Filename:=ExcelToBeOpened
objExcel.Visible = True

'Replace text of a cell in the 'Risk, Issue Metrics' worksheet
objExcel.Sheets("Risk, Issue Metrics").Select
objExcel.Cells(9, 1).Value = "No. of minor risks open (i.e. with risk
exposure between 3.5 and 0)"

Application.Visible = True
'Check if the computer has access to the vbprojects
Dim VBP As Object ' as VBProject
If Val(Application.Version) >= 10 Then
On Error Resume Next
Set VBP = ActiveWorkbook.VBProject
If Err.Number <> 0 Then
MsgBox "Your security settings do not allow this procedure to
run." _
& vbCrLf & vbCrLf & "To change your security setting:" _
& vbCrLf & vbCrLf & " 1. Select Tools - Macro - Security." &
vbCrLf _
& " 2. Click the 'Trusted Sources' tab" & vbCrLf _
& " 3. Place a checkmark next to 'Trust access to Visual Basic
Project.'", _
vbCritical
Exit Sub
End If
End If

'Delete all code inside a VBProject
'
Dim VBComp As VBIDE.VBComponent
Dim VBComps As VBIDE.VBComponents
Set VBComps = objExcel.Workbooks(1).VBProject.VBComponents
For Each VBComp In VBComps
Select Case VBComp.Type
Case vbext_ct_StdModule, vbext_ct_MSForm, vbext_ct_ClassModule
VBComps.Remove VBComp
Case Else
With VBComp.CodeModule
.DeleteLines 1, .CountOfLines
End With
End Select
Next VBComp

'Copy module with the new code
'
CopyModule ActiveWorkbook, "RiskIssueLog", objExcel.Workbooks(1)

objExcel.Workbooks(1).Save
objExcel.Workbooks.Close
objExcel.Quit
Set objExcel = Nothing

fileNotOpened:
'Diable error handling
On Error GoTo 0


End Sub

-------
Sub CopyModule(SourceWB As Workbook, strModuleName As String, TargetWB As
Workbook)

' copies a module from one workbook to another
' example:
' CopyModule Workbooks("Book1.xls"), "Module1", Workbooks ("Book2.xls")
Dim strFolder As String, strTempFile As String
strFolder = SourceWB.Path
If Len(strFolder) = 0 Then strFolder = CurDir
strFolder = strFolder & ""
strTempFile = strFolder & "~tmpexport.txt"
On Error Resume Next
SourceWB.VBProject.VBComponents(strModuleName).Export strTempFile
'TargetWB.VBProject.VBComponents.Import strTempFile
TargetWB.VBProject.VBComponents("Sheet6").CodeModule.AddFromFile
strTempFile
TargetWB.VBProject.VBComponents("RiskIssueLog").Name = "Sheet6"

'Code to view all the VBProject components in the excel sheet being opened
'
'Dim VBComp As VBComponent
'Dim Msg As String
' For Each VBComp In TargetWB.VBProject.VBComponents
' Msg = Msg & VBComp.Name & " Type: " & CompTypeToName(VBComp) &
Chr(13)
'Next VBComp
'MsgBox Msg

'MsgBox TargetWB.VBProject.VBComponents(5).Name


Kill strTempFile
On Error GoTo 0

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top