not having to repeat code

daveyc18

Well-known Member
Joined
Feb 11, 2013
Messages
648
Office Version
  1. 365
  2. 2010
so i got various macros in a workbook referring to the date

Code:
Dim Today As Date
Dim data


Set mywbk = ActiveWorkbook
Set procedures = mywbk.Sheets("Reports")


showdate = Day(procedures.Range("A29"))
showmonth = Month(procedures.Range("A29"))
showyear = Year(procedures.Range("A29"))


If Len(showdate) = 1 Then
    showdate = "0" & showdate
End If


If Len(showmonth) = 1 Then
    showmonth = "0" & showmonth
End If


showdate0 = Day(procedures.Range("A29"))
m5 = longm(showmonth)

is there anyway i can just make this apply to the entire workbook so i dont have to apply it to each module?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I don't think there is a way to define a variable from a worksheet value completely outside of a separate subroutine, however, you CAN create a generic subroutine which can set/edit the variables and can be called as just a single line of code (in this instance InitializeDates)

Code:
'Global variables available to all subroutines
Public showDate As String
Public showMonth As String
Public showYear As String
'NOTE: since these variables are declared globally,
'   they shouldn't be declared locally in any other Subs
Sub InitializeDates()
Dim tmpDate As Date
tmpDate = ActiveWorkbook.Sheets("Reports").Range("A29").Value
    showDate = Format(tmpDate, "DD")
    showMonth = Format(tmpDate, "MM")
    showYear = Format(tmpDate, "YYYY")
End Sub
Sub SUB1()
    InitializeDates
    '....other code
    MsgBox showDate & "-" & showMonth & "-" & showYear
End Sub
Sub SUB2()
    InitializeDates
    '....other code
    MsgBox showDate & "-" & showMonth & "-" & showYear
End Sub

Note that the MsgBox lines are simply there to demonstrate that it works and can/should be removed.
 
Upvote 0

Forum statistics

Threads
1,203,483
Messages
6,055,679
Members
444,807
Latest member
RustyExcel

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