Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 88
Default How can I reduce repetative code

I have set up a userform with all the weeks of the year as optionbuttons to
save data too, with a command button to activate depending on which
optionbutton has been made true. My question: Instead of having to repeat the
same code over and over again stating open new workbook / name workbook /
copy data to workbook / save data / close workbook. a good 40 lines of code
for each option button. Is there away around this?
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,501
Default How can I reduce repetative code

Hi,

Pass the name of the button that was pressed to a single routine

Private Sub CommandButton1_Click()
ButtonPressed = CommandButton1.Caption
MyRoutine (ButtonPressed)
End Sub

Mike


"leerem" wrote:

I have set up a userform with all the weeks of the year as optionbuttons to
save data too, with a command button to activate depending on which
optionbutton has been made true. My question: Instead of having to repeat the
same code over and over again stating open new workbook / name workbook /
copy data to workbook / save data / close workbook. a good 40 lines of code
for each option button. Is there away around this?

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 88
Default How can I reduce repetative code

Hi Mike, I'm fairly new to this, could you please go into a bit more detail


"Mike H" wrote:

Hi,

Pass the name of the button that was pressed to a single routine

Private Sub CommandButton1_Click()
ButtonPressed = CommandButton1.Caption
MyRoutine (ButtonPressed)
End Sub

Mike


"leerem" wrote:

I have set up a userform with all the weeks of the year as optionbuttons to
save data too, with a command button to activate depending on which
optionbutton has been made true. My question: Instead of having to repeat the
same code over and over again stating open new workbook / name workbook /
copy data to workbook / save data / close workbook. a good 40 lines of code
for each option button. Is there away around this?

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,420
Default How can I reduce repetative code

Use called procedures

Private Sub OptionButton1_Click()

Call myProc
End Sub

Private Sub OptionButton2_Click()

Call myProc
End Sub

Private Function myProc()

Workbooks.Open ... 'etc
'other code
End Sub

If each button opens a different workbook, add an argument


Private Sub OptionButton1_Click()

Call myProc("C:\test\Book1.xls")
End Sub

Private Sub OptionButton2_Click()

Call myProc("C:\test\Book2.xls")
End Sub

Private Function myProc(ByVal Filename As String)

Workbooks.Open Filename
'other code
End Sub

--
__________________________________
HTH

Bob

"leerem" wrote in message
...
I have set up a userform with all the weeks of the year as optionbuttons to
save data too, with a command button to activate depending on which
optionbutton has been made true. My question: Instead of having to repeat
the
same code over and over again stating open new workbook / name workbook /
copy data to workbook / save data / close workbook. a good 40 lines of
code
for each option button. Is there away around this?



  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,501
Default How can I reduce repetative code

Hi,

It's difficult to provide too much help because you don't go into detail
about your code but the example I gave you would pass the caption of the
button that had been pressed to to a routine called "MyRoutine". You could
equally easily pass other parameters you wanted to your code depending on
which button had called it and could do this for multiple buttons.

The example give your other response shows how you could pass a workbook name.

Mike

"leerem" wrote:

Hi Mike, I'm fairly new to this, could you please go into a bit more detail


"Mike H" wrote:

Hi,

Pass the name of the button that was pressed to a single routine

Private Sub CommandButton1_Click()
ButtonPressed = CommandButton1.Caption
MyRoutine (ButtonPressed)
End Sub

Mike


"leerem" wrote:

I have set up a userform with all the weeks of the year as optionbuttons to
save data too, with a command button to activate depending on which
optionbutton has been made true. My question: Instead of having to repeat the
same code over and over again stating open new workbook / name workbook /
copy data to workbook / save data / close workbook. a good 40 lines of code
for each option button. Is there away around this?

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formula needed for repetative spreadsheet amatur wkr Excel Worksheet Functions 3 April 30th 08 08:47 PM
Calculating repetative data & presenting it via a (Venn)diagram Sar*[_2_] Excel Discussion (Misc queries) 0 September 25th 07 04:40 PM
Repetative text smack Excel Worksheet Functions 4 May 27th 07 12:27 AM
Reduce code Excel 2003 - SPB Excel Discussion (Misc queries) 2 May 10th 07 10:43 PM
Repetative Row Deletes scratching my head Excel Discussion (Misc queries) 1 May 30th 05 09:38 PM


All times are GMT +1. The time now is 02:57 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"