Macro to change a Macro Code

KyleG

Well-known Member
Joined
Jun 12, 2004
Messages
623
Another totally different question...which may be well over my head.

In a Module called Getinfo i have a sub called GetData. In this sub i have the following piece of code...

' Retrieve data from closed workbook - Change each YEAR
GetValuesFromAClosedWorkbook "L:\Stocktake Info 03-04", "Turnover 03 - 04.xls", _
"Daily Turnover", "A1:AG1000", "TempDailyTurnover"
GetValuesFromAClosedWorkbook "L:\Stocktake Info 03-04", "Turnover 03 - 04.xls", _
"Weekly Turnover", "A1:AG1000", "TempWeeklyTurnover"

EDIT: The bolded text is passed to another sub

The bolded section has to be changed each year. What i want is a macro that Looks in this sub for the Workbook it points to (only workbook linked in project) and displays the reference and asks the user to type in the new reference. The code is then changed and we are all go. This way the code is not accessed by anyone but the new workbook each year can be entered into the code.

Hope that makes sense.

thanks

Kyle
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi Kyle,
I haven't tried doing what you asked for but I would think this site might give you some ideas.
http://www.cpearson.com/excel/vbe.htm
I would begin with the "Deleting A Procedure From A Module" and the "Deleting All Code From A Module" sections to see what I could adapt & get what you're after.

Hope it helps,
Dan
 
Upvote 0
Been there, trying that. Is a Proceedure a sub or somthing else. And would you know if VBComponents is the module name or sub name?
Its finding a way to tell it to select that piece of code without using that piece of code as next time its going to be different.

Ill keep trying.
Thanks anyway guys

Kyle
 
Upvote 0
Heya Kyle,

Why not take a step back and try a different approach? How about grabbing the system date instead; then you don't have to involve your users at all:

<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> GettheDate()
    <SPAN style="color:#00007F">Dim</SPAN> ans <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
        ans = Right(Year(Date), 2)
            GetValuesFromAClosedWorkbook "L:\Stocktake Info 03-" & ans, "Turnover 03 - " & ans & ".xls", _
            "Daily Turnover", "A1:AG1000", "TempDailyTurnover"
            GetValuesFromAClosedWorkbook "L:\Stocktake Info 03-" & ans, "Turnover 03 - " & ans & ".xls", _
            "Weekly Turnover", "A1:AG1000", "TempWeeklyTurnover"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

Hope that helps,

Smitty

(Heya Dan - Checked your mail lately?)
 
Upvote 0
Not sure i quite understand that....

The file and location will be...
"L:\Stocktake Info 03-04", "Turnover 03 - 04.xls"

then it will be
"L:\Stocktake Info 04-05", "Turnover 04 - 05.xls"

Thanks
 
Upvote 0
My code takes the current system date/year (as in "04") and replaces your hard-coding.

What's the criteria for the month change? My code will only update to the current year.

You can add an Input Box requiring a Month # input, but you'll probably want to add some validation/error handling to handle a wrong # input. Maybe you could do that based on a cell reference?

Post back with more and I'll adjust what I can.

Smitty
 
Upvote 0
Hi Kyle,

Borrowing heavily from Chip's examples, the following will delete the original procedure (ie a sub or function) and replace it with a new one.
Code:
Sub SwapProcs()
    Dim wbkToChange As Workbook
    Dim VBProj As VBProject, VBComp As VBComponent
    Const strMod As String = "Getinfo"
    Const strProc As String = "Getdata"
    Dim strNewProc As String
    
    Set wbkToChange = Workbooks("KyleTest")
    'assume workbook already open (and that VBAProject is unlocked)
    
    Set VBProj = wbkToChange.VBProject
    Set VBComp = VBProj.VBComponents(strMod)
    strNewProc = "Sub Getdata()" & Chr(13) & _
        " Msgbox ""Your new data here"" " & Chr(13) & _
        "End Sub"
    'data re the code to be changed

    Call DeleteProcedure(VBComp, strProc)
    'remove old routine
    
    Call AddProcedure(VBComp, strNewProc)
    'add new routine

End Sub

Sub DeleteProcedure(VBComp As VBComponent, strProc As String)
    Dim VBCodeMod As CodeModule
    Dim StartLine As Long
    Dim HowManyLines As Long
    
    Set VBCodeMod = VBComp.CodeModule
    With VBCodeMod
        StartLine = .ProcStartLine(strProc, vbext_pk_Proc)
        HowManyLines = .ProcCountLines(strProc, vbext_pk_Proc)
        .DeleteLines StartLine, HowManyLines
    End With

End Sub

Sub AddProcedure(VBComp As VBComponent, strProc As String)
    Dim VBCodeMod As CodeModule
    Dim LineNum As Long
    
    Set VBCodeMod = VBComp.CodeModule
    With VBCodeMod
        LineNum = .CountOfLines + 1
        .InsertLines LineNum, strProc
    End With

End Sub
You will need to set a reference to the VBA Extensibility library.

HTH
 
Upvote 0
Thanks for all your help guys. Have eliminated the need for the macro now. I've found a source that doesnt change its file name but still contains all the data i need. Thanks heaps
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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