Skip to main content
Code Review

Return to Question

Make title describe what code does per site convention - see https://codereview.stackexchange.com/help/how-to-ask ; remove Thanks - say thanks by voting - see https://codereview.stackexchange.com/help/behavior and https://codereview.stackexchange.com/help/why-vote
Source Link

VBA code optimisation needed userform with textboxes and dates to show timeline

I am writing the following VBA code to make a timeline, which I will be using for other parts of the code later on for other calculations and output. I have a userform where thethe following number of text boxes and labels are present:

  • wkbox* and intbox* are textboxes which have weeks as input from user. wkbox* is duration of a stage, intbox* is interval between two stages.
  • date* are labels which give output as dates when each stage starts.
  • mpsd is the input date for the entire timeline start date from mpsd.text box, which is considered as the start date in my code below
Public mpsd as date 
Private Sub Setbutton_Click()
 
 Me.date1 = mpsd
 Me.date2 = DateAdd("d", ((wkbox1 * 5) + (intbox1 * 5)), date1)
 Me.date3 = DateAdd("d", ((wkbox2 * 5) + (intbox2 * 5)), date2)
 Me.date4 = DateAdd("d", ((wkbox3 * 5) + (intbox3 * 5)), date3)
 Me.date5 = DateAdd("d", ((wkbox4 * 5) + (intbox4 * 5)), date4)
 Me.date6 = DateAdd("d", ((wkbox5 * 5) + (intbox5 * 5)), date5)
 Me.date7 = DateAdd("d", ((wkbox6 * 5) + (intbox6 * 5)), date6)
 Me.date8 = DateAdd("d", ((wkbox7 * 5) + (intbox7 * 5)), date7)
 Me.date9 = DateAdd("d", ((wkbox8 * 5) + (intbox8 * 5)), date8)
 Me.date10 = DateAdd("d", ((wkbox9 * 5) + (intbox9 * 5)), date9)
 Me.date11 = DateAdd("d", ((wkbox10 * 5) + (intbox10 * 5)), date10)
 Me.date12 = DateAdd("d", ((wkbox11 * 5) + (intbox11 * 5)), date11)
 
End sub

I was trying to think if there is a way to optimise this code through for,case, etc. I

I am very new to VBA, so am not able to think how I can reduce this to a maybe 3 line for or case code to speed up the running of the code.

(This is just a part of a big schedule planner code, so any sec saved is good for me.) Is there a way to do it? Thank you.

VBA code optimisation needed

I am writing the following VBA code to make a timeline, which I will be using for other parts of the code later on for other calculations and output. I have a userform where the following number of text boxes and labels are present:

  • wkbox* and intbox* are textboxes which have weeks as input from user. wkbox* is duration of a stage, intbox* is interval between two stages.
  • date* are labels which give output as dates when each stage starts.
  • mpsd is the input date for the entire timeline start date from mpsd.text box, which is considered as the start date in my code below
Public mpsd as date 
Private Sub Setbutton_Click()
 
 Me.date1 = mpsd
 Me.date2 = DateAdd("d", ((wkbox1 * 5) + (intbox1 * 5)), date1)
 Me.date3 = DateAdd("d", ((wkbox2 * 5) + (intbox2 * 5)), date2)
 Me.date4 = DateAdd("d", ((wkbox3 * 5) + (intbox3 * 5)), date3)
 Me.date5 = DateAdd("d", ((wkbox4 * 5) + (intbox4 * 5)), date4)
 Me.date6 = DateAdd("d", ((wkbox5 * 5) + (intbox5 * 5)), date5)
 Me.date7 = DateAdd("d", ((wkbox6 * 5) + (intbox6 * 5)), date6)
 Me.date8 = DateAdd("d", ((wkbox7 * 5) + (intbox7 * 5)), date7)
 Me.date9 = DateAdd("d", ((wkbox8 * 5) + (intbox8 * 5)), date8)
 Me.date10 = DateAdd("d", ((wkbox9 * 5) + (intbox9 * 5)), date9)
 Me.date11 = DateAdd("d", ((wkbox10 * 5) + (intbox10 * 5)), date10)
 Me.date12 = DateAdd("d", ((wkbox11 * 5) + (intbox11 * 5)), date11)
 
End sub

I was trying to think if there is a way to optimise this code through for,case, etc. I am very new to VBA, so am not able to think how I can reduce this to a maybe 3 line for or case code to speed up the running of the code.(This is just a part of a big schedule planner code, so any sec saved is good for me.) Is there a way to do it? Thank you.

userform with textboxes and dates to show timeline

I am writing the following VBA code to make a timeline, which I will be using for other parts of the code later on for other calculations and output. I have a userform where the following number of text boxes and labels are present:

  • wkbox* and intbox* are textboxes which have weeks as input from user. wkbox* is duration of a stage, intbox* is interval between two stages.
  • date* are labels which give output as dates when each stage starts.
  • mpsd is the input date for the entire timeline start date from mpsd.text box, which is considered as the start date in my code below
Public mpsd as date 
Private Sub Setbutton_Click()
 
 Me.date1 = mpsd
 Me.date2 = DateAdd("d", ((wkbox1 * 5) + (intbox1 * 5)), date1)
 Me.date3 = DateAdd("d", ((wkbox2 * 5) + (intbox2 * 5)), date2)
 Me.date4 = DateAdd("d", ((wkbox3 * 5) + (intbox3 * 5)), date3)
 Me.date5 = DateAdd("d", ((wkbox4 * 5) + (intbox4 * 5)), date4)
 Me.date6 = DateAdd("d", ((wkbox5 * 5) + (intbox5 * 5)), date5)
 Me.date7 = DateAdd("d", ((wkbox6 * 5) + (intbox6 * 5)), date6)
 Me.date8 = DateAdd("d", ((wkbox7 * 5) + (intbox7 * 5)), date7)
 Me.date9 = DateAdd("d", ((wkbox8 * 5) + (intbox8 * 5)), date8)
 Me.date10 = DateAdd("d", ((wkbox9 * 5) + (intbox9 * 5)), date9)
 Me.date11 = DateAdd("d", ((wkbox10 * 5) + (intbox10 * 5)), date10)
 Me.date12 = DateAdd("d", ((wkbox11 * 5) + (intbox11 * 5)), date11)
 
End sub

I was trying to think if there is a way to optimise this code through for,case, etc.

I am very new to VBA, so am not able to think how I can reduce this to a maybe 3 line for or case code to speed up the running of the code.

(This is just a part of a big schedule planner code, so any sec saved is good for me.) Is there a way to do it?

Post Reopened by πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ
Post Closed as "Not suitable for this site" by πάντα ῥεῖ, Billal BEGUERADJ, J_H
edited tags
Link
cleaned up code
Source Link
PeterT
  • 2.2k
  • 10
  • 15
  • wkbox* and intbox* are textboxes which have weeks as input from user. wkbox* is duration of a stage, intbox* is interval between two stages.

    wkbox* and intbox* are textboxes which have weeks as input from user. wkbox* is duration of a stage, intbox* is interval between two stages.
  • date* are labels which give output as dates when each stage starts.

    date* are labels which give output as dates when each stage starts.
  • mpsd is the input date for the entire timeline start date from mpsd.text box, which is considered as the start date in my code below

    Public mpsd as date
    Private Sub Setbutton_Click()

     Me.date1 = mpsd
     Me.date2 = DateAdd("d", ((wkbox1 * 5) + (intbox1 * 5)), date1)
     Me.date3 = DateAdd("d", ((wkbox2 * 5) + (intbox2 * 5)), date2)
     Me.date4 = DateAdd("d", ((wkbox3 * 5) + (intbox3 * 5)), date3)
     Me.date5 = DateAdd("d", ((wkbox4 * 5) + (intbox4 * 5)), date4)
     Me.date6 = DateAdd("d", ((wkbox5 * 5) + (intbox5 * 5)), date5)
     Me.date7 = DateAdd("d", ((wkbox6 * 5) + (intbox6 * 5)), date6)
     Me.date8 = DateAdd("d", ((wkbox7 * 5) + (intbox7 * 5)), date7)
     Me.date9 = DateAdd("d", ((wkbox8 * 5) + (intbox8 * 5)), date8)
     Me.date10 = DateAdd("d", ((wkbox9 * 5) + (intbox9 * 5)), date9)
     Me.date11 = DateAdd("d", ((wkbox10 * 5) + (intbox10 * 5)), date10)
     Me.date12 = DateAdd("d", ((wkbox11 * 5) + (intbox11 * 5)), date11)
     End sub
    
    mpsd is the input date for the entire timeline start date from mpsd.text box, which is considered as the start date in my code below
Public mpsd as date 
Private Sub Setbutton_Click()
 
 Me.date1 = mpsd
 Me.date2 = DateAdd("d", ((wkbox1 * 5) + (intbox1 * 5)), date1)
 Me.date3 = DateAdd("d", ((wkbox2 * 5) + (intbox2 * 5)), date2)
 Me.date4 = DateAdd("d", ((wkbox3 * 5) + (intbox3 * 5)), date3)
 Me.date5 = DateAdd("d", ((wkbox4 * 5) + (intbox4 * 5)), date4)
 Me.date6 = DateAdd("d", ((wkbox5 * 5) + (intbox5 * 5)), date5)
 Me.date7 = DateAdd("d", ((wkbox6 * 5) + (intbox6 * 5)), date6)
 Me.date8 = DateAdd("d", ((wkbox7 * 5) + (intbox7 * 5)), date7)
 Me.date9 = DateAdd("d", ((wkbox8 * 5) + (intbox8 * 5)), date8)
 Me.date10 = DateAdd("d", ((wkbox9 * 5) + (intbox9 * 5)), date9)
 Me.date11 = DateAdd("d", ((wkbox10 * 5) + (intbox10 * 5)), date10)
 Me.date12 = DateAdd("d", ((wkbox11 * 5) + (intbox11 * 5)), date11)
 
End sub
  • wkbox* and intbox* are textboxes which have weeks as input from user. wkbox* is duration of a stage, intbox* is interval between two stages.

  • date* are labels which give output as dates when each stage starts.

  • mpsd is the input date for the entire timeline start date from mpsd.text box, which is considered as the start date in my code below

    Public mpsd as date
    Private Sub Setbutton_Click()

     Me.date1 = mpsd
     Me.date2 = DateAdd("d", ((wkbox1 * 5) + (intbox1 * 5)), date1)
     Me.date3 = DateAdd("d", ((wkbox2 * 5) + (intbox2 * 5)), date2)
     Me.date4 = DateAdd("d", ((wkbox3 * 5) + (intbox3 * 5)), date3)
     Me.date5 = DateAdd("d", ((wkbox4 * 5) + (intbox4 * 5)), date4)
     Me.date6 = DateAdd("d", ((wkbox5 * 5) + (intbox5 * 5)), date5)
     Me.date7 = DateAdd("d", ((wkbox6 * 5) + (intbox6 * 5)), date6)
     Me.date8 = DateAdd("d", ((wkbox7 * 5) + (intbox7 * 5)), date7)
     Me.date9 = DateAdd("d", ((wkbox8 * 5) + (intbox8 * 5)), date8)
     Me.date10 = DateAdd("d", ((wkbox9 * 5) + (intbox9 * 5)), date9)
     Me.date11 = DateAdd("d", ((wkbox10 * 5) + (intbox10 * 5)), date10)
     Me.date12 = DateAdd("d", ((wkbox11 * 5) + (intbox11 * 5)), date11)
     End sub
    
  • wkbox* and intbox* are textboxes which have weeks as input from user. wkbox* is duration of a stage, intbox* is interval between two stages.
  • date* are labels which give output as dates when each stage starts.
  • mpsd is the input date for the entire timeline start date from mpsd.text box, which is considered as the start date in my code below
Public mpsd as date 
Private Sub Setbutton_Click()
 
 Me.date1 = mpsd
 Me.date2 = DateAdd("d", ((wkbox1 * 5) + (intbox1 * 5)), date1)
 Me.date3 = DateAdd("d", ((wkbox2 * 5) + (intbox2 * 5)), date2)
 Me.date4 = DateAdd("d", ((wkbox3 * 5) + (intbox3 * 5)), date3)
 Me.date5 = DateAdd("d", ((wkbox4 * 5) + (intbox4 * 5)), date4)
 Me.date6 = DateAdd("d", ((wkbox5 * 5) + (intbox5 * 5)), date5)
 Me.date7 = DateAdd("d", ((wkbox6 * 5) + (intbox6 * 5)), date6)
 Me.date8 = DateAdd("d", ((wkbox7 * 5) + (intbox7 * 5)), date7)
 Me.date9 = DateAdd("d", ((wkbox8 * 5) + (intbox8 * 5)), date8)
 Me.date10 = DateAdd("d", ((wkbox9 * 5) + (intbox9 * 5)), date9)
 Me.date11 = DateAdd("d", ((wkbox10 * 5) + (intbox10 * 5)), date10)
 Me.date12 = DateAdd("d", ((wkbox11 * 5) + (intbox11 * 5)), date11)
 
End sub
added 58 characters in body
Source Link
Loading
added 6 characters in body
Source Link
Loading
deleted 114 characters in body
Source Link
Loading
added 51 characters in body
Source Link
Loading
added 13 characters in body
Source Link
Loading
added 23 characters in body
Source Link
Loading
added 29 characters in body
Source Link
Loading
Source Link
Loading
lang-vb

AltStyle によって変換されたページ (->オリジナル) /