Combination Date and Funtion

Gowodka

New Member
Joined
Aug 24, 2011
Messages
20
Hi,

So i have 10 buttons on my userform and if i press them i need to find some data according to the button pressed. All the 10 buttons have 1 thing in common they all need the Date, Yearnumber, Montnumber and weeknumber

I could copy the code below 10 times but i wan't to make some sort of Function or subprocedure so whatever button i press it always calculate the date, yearNumber, MonthNumber and the weeknumber

I just don't understand how to do this, i'm quite new to this

Code:
    Public Datum As Date           'the current Date
    Public DatumMaand As Single    'number of the Month
    Public DatumWeek As Single     'number of the week
    Public DatumJaar As Single     'Number of the Year
    
    Datum = Date
    DatumMaand = Month(Datum)
    DatumWeek = DatePart("ww", Datum, vbMonday, vbFirstFourDays)
    DatumJaar = DatePart("YYYY", Datum, vbMonday, vbFirstFourDays)


Thanks in advance
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Create a sub in the module section with public variables where you store the week, year and month.
In the code for the button you enter
Code:
Call Sub
'enter whatever you want to do with the data in the variables
 
Upvote 0
Thx,

I made a sub in a module called GlobalStuff there are some global variables in here and the Date code

But it still doesn't work, the code is the following

Code:
Sub ZoekDatum()
    Dim Datum As Date           'Datum
    Dim DatumMaand As Single    'MaandNummer
    Dim DatumWeek As Single     'WeekNummer
    Dim DatumJaar As Single     'JaarNummer
    
    Datum = Date
    DatumMaand = Month(Datum)
    DatumWeek = DatePart("ww", Datum, vbMonday, vbFirstFourDays)
    DatumJaar = DatePart("YYYY", Datum, vbMonday, vbFirstFourDays)
End Sub

Then i go to my userform and under a cmdbutton_click i put to code :

Code:
Private Sub cmdOkAenR_Click()

Call ZoekDatum

Worksheets("sheet1").Activate
RowCount = Workbooks("Back UP 4.xlsm").Sheets("sheet1").Range("A1").CurrentRegion.Rows.Count
With Worksheets("sheet1").Range("A1")
    .Offset(RowCount, 7) = DatumJaar
    .Offset(RowCount, 8) = DatumMaand
    .Offset(RowCount, 9) = DatumWeek
    .Offset(RowCount, 10) = Datum
End With

End Sub

The code executes but it won't put it in my workbook
 
Upvote 0
If i use

Code:
Public Datum as Date

in stead of

Code:
Dim Datum As date

it gives me the compile error Invalid Attribute in Sub or Function
 
Upvote 0
Try something like this, found at http://en.allexperts.com/q/Excel-1059/help-8.htm

Code:
Sub SetDatum()
   Dim ctrl As OLEObject
   For Each ctrl In Sheet1.OLEObjects
       If ctrl.progID Like "*CommandButton*" Then
         Datum = Date
         DatumMaand = Month(Datum)
         DatumWeek = DatePart("ww", Datum, vbMonday, vbFirstFourDays)
         DatumJaar = DatePart("YYYY", Datum, vbMonday, vbFirstFourDays)
         ctrl.Delete
       End If
   Next ctrl
End Sub

Succes
 
Upvote 0
Hoi, Ik zag dat je nederlander was, but i will talk in english

The OLEobject also doesn't work :confused:

I don't understand it, it won't work. It shouldn't be so hard i think im looking over something. So I will just explain what i wan't to do.

I have 10 command buttons on 10 different pages ( using the multipage )
On every page you have some textboxes

If i press a command button the data in the textboxes will be processed and put into the sheet, this works perfectly. ( not efficient but working on it )

Now i need a function or sub to add the date, week, month and year in the sheet in different cells.

How do i do this ? And where do i put what ?
 
Upvote 0
Hoi,

I've just tested the code below. Works like a charm.

Code:
Sub SetDatum()
   Dim ctrl As OLEObject
   For Each ctrl In Sheet1.OLEObjects
       If ctrl.progID Like "*CommandButton*" Then
            Range("A1").Value = ctrl.Name
       End If
   Next ctrl
End Sub

This gives me the name of the last command button in cell A1 on sheet1

Please try this code.
 
Upvote 0
waaaah :confused:

It doesn't work for me

I opened a new sheet, made a userform, made a commandbutton name: cmdOK and caption = Test.

I putted the code into the cmdOK_click code and in a module and in the general code space. I used a Call if it was in the module without the call, when it was in general i tried with and without call but it just won't work

What do i do??

Plzzz help me.

do i have to use the OLEObjects ? isn't there a very easy simple way to use a global variable and use this in a function and use this function under a cmd_click
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,704
Members
452,938
Latest member
babeneker

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