Support needed with vba programming for a pop up calendar - excel

I have no experience with vba and would much appreciate your assistance with the implementation of a pop-up calendar user form. This link will direct you to the source of the calendar that I am trying to introduce into my workbook:
http://www.ozgrid.com/forum/showthread.php?t=142603
I have copied the class module, the user form, and the module into my workbook. When I launch the macro, this opens up a user form where i can select the date wished on a calendar.
What I miss is to be able to send the date selected in the calendar to a specific cell in my workbook.
I would much appreciate if somebody could guide me through how to write a few lines of code that would send the date selected in the user form to a specific cell in my workbook!
Again, I am very new to this so let me know if anything is unclear in my explanation. I have spent a lot of time on this so any support is highly appreciated! It probably only takes a few moments for you but would mean a lot to me!

Try this post. It semes to give a better guide to work with datepicker control. However it shows coding to make an add-in.
Hence most basic approach for you would be to,
Add a Form
Add datepicker control
code from there
per this article.
But do remember calendar control in Excel/Access can sometimes disappear due this reason mentioned in my post.
If you are planning to use date picker control, here is the code to pass the value from form to anywhere you want ;)
Private Sub myDtPicker_Change()
Dim dtDateSelected as Date
dtDateSelected = myDtPicker.Value
'-- do anything else
End Sub

The class writes the selected Date into a Textbox. After you selected the Date, you can use the value of the textbox to set the value of the Cell.
Private Sub UserForm_Initialize()
Set clsCal = New clsCalendar 'Initialize the Class'
Set clsCal.Form(Me.TextBoxDate) = Me 'Tells the class to write the Selected date'
' into the textbox "Me.TextBoxDate"'
End Sub
So in that example, whenever you select a date, the class will automaticaly store the selected Date in the Textbox.
After you selected the date, you can use the following code to add the value to a cell:
Range("A1").value=TextBoxDate.Value

Related

Excel VBA - Save Userform Changes

Is it possible to save changes to a Label Caption in a userform in Excel VBA, so that they are permanently saved, and only changed when you enter a new change?
I have checked, that the code is changing the caption, but I cannot get it to stick, so that it is still there next time I open the userform.
Thank you in advance
Private Sub cmdSubmit_Click()
'resets participants email and name
If Me.optProg.Value = True Then
Me.NameLabelProg.Caption = Me.CB_Part.Value
Me.MailLabelProg.Caption = Me.TB_Mail.Value
ElseIf Me.optTester.Value = True Then
Me.MailLabelTest.Caption = Me.CB_Part.Value
Me.NameLabelTest.Caption = Me.TB_Mail.Value
End If
End Sub
Check out this userform example and you'll find answers to many of your questions.
Source of userform
Forgive the lengthy answer initially, but there is a bit of a risk involved with making permanent changes to UserForms through VBA.
In order to change the caption of a Label (or UserForm or any other Control permanently, you will have to "Trust access to the VBA project object model" in order to do it via VBA code. Now, while this is possible it is not usually recommended because it can seriously put a user's PC at Risk should they encounter a Macro developed for nefarious purposes.
(To clarify in case the question is raised, your end user(s) will have to make this Trust setting change on their PC's as well . . . you cannot make the change on your PC, setup the code to work and then hand the file over to another user and have it work on their PC without them making the same change.)
There are methods to do this programmatically, but, this falls into the "nefarious Macro" rabbit hole and would need to be disclosed to the end user you are making this change. . . Research at your own risk.
If you are OK with putting yourself at risk, you can do it using VBA similar to the following snippet I found here. You will have to substitute your UserForm name and Label name as appropriate. I tested it on my own UserForm and it works as expected.
Sub Change_Userform()
ThisDocument.VBProject.VBComponents("Userform1").Designer.Controls("Label1").Caption = "Some new caption text"
End Sub
You will need to do some research on how to "Trust access to the VBA project object model" yourself and understand the risks to do this in order for the above code to work.
If I understand the intent of what you are trying to accomplish correctly though, you could achieve this effect without having to put yourself or your end user(s) at risk.
(Most end user(s) typically would not have direct access to the VBA Designer where they would see the UserForm's un-initialized environment.) To do this, you would have to place your code in the UserForm's event.
The following assumes your Me.optProg.Value and Me.optTester.Value are Option Buttons which a user would change. If you create a "Settings" sheet in the file, you can place values in the cells of this sheet and then hide it so the users do not modify them directly. Then, reference the values of the cells and change the appearances of the Option Buttons at the same time as the UserForm's launch. (Additionally, you can set the Click events of the Option Buttons to change the values of the same cells and provide that change to affect the UserForm's Initialize event when called, but this should get you going in the right direction.)
Sub UserForm_Initialize()
'The Range below is completely up to you.
'Since you are using Boolean True/False, a simple "1" or "0" _
is easy to use to make the changes.
If Thisworkbook.Sheets("Some_Settings").Range("A1").Value = "1" Then
Me.optProg.Value = True
Else
Me.optTester.Value = True
End If
'do some other code here as needed to finish initializing the UserForm
If Me.optProg.Value = True Then
Me.NameLabelProg.Caption = Me.CB_Part.Value
Me.MailLabelProg.Caption = Me.TB_Mail.Value
ElseIf Me.optTester.Value = True Then
Me.MailLabelTest.Caption = Me.CB_Part.Value
Me.NameLabelTest.Caption = Me.TB_Mail.Value
End If
End Sub

From cell to DTPicker and back to cell again - VBA - Userform

I am relatively new to all this so please be kind.
I have some DTPickers, these DTPickers are supposed to fetch data from cells and contain that data when the userform is opened. In this case I have the below code to do that when a button to navigate to the mutliform page is clicked.
DTPicker1.Value = Sheets("FirstName").Range("B2").Value
DTPicker1.CustomFormat = "dd/MM/yyyy"
Furthermore I would like the date to be changed and for the value to be updated within the userform, so I got myself some code to do that as follows:
Sheets("FirstName").Range("B2").Value = DTPicker1.Value
This is run during the DTPicker change event. This is all well and good except there needs to be 20 of these DTPickers. And that means an awful lot of coding the exact same thing again and again and again.
Is there any way to create a loop where I can specify a range for all the information to populate into the 1-20 DTPickers as the multiform page is opened and then when any of their information is changed for that to be stored back into those cells, without having to put the code above into every single DTPicker change event.
IF there is anything at all please help me out, and please if you could also specify where to put any code as well because I'm really not great at this. Thank you.

Manual date selection in vba

I am trying to find a way to allow a manual date + time. I have a worksheet that has a button for which when the user updates the table, they click the button and it inputs the current date and time. The challenges is I currently have the Now() function and every time I open the workbook, the date gets updated based on the now date. I want this to be a manual action.
I've searched here and on the internet, and all I can find is the automated NOW() or others that seem to automate the process.
You can use VBA to write the current date (plus time) into a cell on a button click:
Public Sub MyButton_Click()
Worksheets("Sheet1").Range("A1").Value = Now
End Sub
Just link that procedure with a button and it will write the date+time into A1 every time you click the button.

I cannot identify the 'VBA.Date' code component

I have found many blocks of code that containVBA.Date, for example;
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 2 Then Exit Sub
Target.Offset(0, 1).Value = Format(VBA.Date, “MM/DD/YYYY”)
End Sub
Or
Sub YearSheets()
Dim i As Integer
i = 0
Do
Sheets.Add(After:=Sheets(Sheets.Count)).Name = Format(VBA.Date + i, “MM-DD-YYYY”)
i = i + 1
Loop Until i = 365
End Sub
I have tried these steps:
I have pushed F1 for VBA's help but it shows "Keyword Not Found".
Searched my VBA books but no one shows something because they take such a piece of information as unimportant or too easy (I guess).
I have tried to Google it --> You can imagine what happens with a combination of such common words.
I have pushed F2 in the VBE Window and opened the Object Browser
i. I searched for VBA and I got the library ... not very bright
ii. I searched for the Date Property and I found it to be preceded by the DateTime Module and going more in seniority from the VBA Library.
Question-born-from-question: Is it possible to be the VBA library here VBA.Date?
Frustrating-thing-that-happens: Once I type VBE IntelliSense shows Date Property, so it's hard-coded somewhere in my machine.
What really is VBA.Date - an object, a library that can be entered as as object?
Date() and Time() are Properties of the DateTime Class. Found in Excel VBA. They are of DataType Variant that contain the current system date when Date is used and current system time when Time is used.
They do not need to be preceeded by the VBA or the DateTime in order to be used they can simply be used with the terms Date, and Time.
Date, and if the calendar is Gregorian, Date$ behavior is unchanged by the Calendar property setting. If the calendar is Hijri, Date$ returns a 10-character string of the form mm-dd-yyyy, where mm (01-12), dd (01-30) and yyyy (1400-1523) are the Hijri month, day and year. The equivalent Gregorian range is Jan 1, 1980 through Dec 31, 2099.
In the future the best way to get information on specific function, properties, methods, classes and other members of the VBA Language you can use the Object Browser.
Marked one is the object browser and when clicked on will open this window (it can also be accessed with pressing F2) In this window enter what you are searching for
One you get results scroll through and look for a more specific item:
Now there are 2 points in the above image:
1) At the bottom of the screen it gives basic detail about this item, in this case
it states that it belongs to the `DateTime` Class, and is a property of that
class, and that it is a Variant.
2) Also in the picture above I have right clicked on the item and selected Help from
the context menu, this will bring up even more details about this item.
As you can see here Microsoft has built in support for this item and gives you details on what it is, what data type it would use and what it returns. Also, how to use and some common notes for when you use it!
VBA.Date and VBA.Time are functions that return the current date and the current time, respectively.
In your editor Tools / References you'll see 'Visual Basic For Applications' is selected. I have absoutely no proof but my idle conjecture is that this is where your VBA.Date (and VBA, everything) lies. Furthermore your F2 investigations show you the truth - It's in the VBA module there. Note you can use VBA.Date or just Date and they are the same thing.
In short it's hard coded in that DLL file referred to in Tools/References.

Retrieving Data back into a form in excel vba

I am pretty new to VBA in Excel. I have built a form which users enter data and at the press of a button it drops all that data into an excel spreadsheet and clears the form. What i need now is code for the user to push a button (off a list or combo box that shows already entered data) and have that data be brought back into the form for changes. I am trying to keep them out of the spreadsheet. Thanks
I think must be like this:
Private Sub Textbox1_Change()
Worksheets("worksheetname").Activate
TextBox1 = Cells(1,1).Text
End Sub
In "Cells()" you have to selecet the specific cell which contains the value. But if you want to use excel as a kind of a database you should think about if it's the right tool, maybe access would be better.

Resources