Excel Macro Help

Joined
Sep 6, 2008
Messages
8
Reaction score
0
Hi all,

I am new to VBA in excel and have written the following code =
Private Sub CommandButton1_Click()
Dim strtDate As Date, fnshDate As Date
Dim n As Integer
strtDate = DateValue(Now)
fnshDate = DateValue("Mar 22,2010")
n = DateDiff("d", strtDate, fnshDate)
MsgBox n
Msg = MsgBox("Do you want to Transfer the Answer?", vbYesNo + vbQuestion, "SumMsgBox")
End Sub

I was hoping that someone could help me?

I am trying to get the answer from the Message Box transfered into a cell in Excel say A15 and everytime the button is pressed it moves down a line

also

I am trying to get it to add the date that the button was pressed in say B15.

Hope someone can help?

If this is not possible could someone sugest another way?

Thanks
 
Last edited:
Joined
Dec 5, 2008
Messages
6
Reaction score
0
Private Sub CommandButton1_Click()
Dim strtDate As Date, fnshDate As Date
Dim n As Integer

strtDate = DateValue(Now)
fnshDate = DateValue("Mar 22,2010")
n = DateDiff("d", strtDate, fnshDate)

'if user choose YES then enter date in cell in column A and row, which is actually selected
If MsgBox(n & vbcr & vbcr & "Do you want to Transfer the Answer?", vbYesNo + vbQuestion, "SumMsgBox") = vbYes Then Thisworkbook.Worksheets("Sheet1").Range("A" & ActiveCell.Row) = n

End Sub
 
Last edited:
Joined
Sep 6, 2008
Messages
8
Reaction score
0
Re: Macro Help

Hi,

Thanks for your help, but by any chance do you know of a way to get it to transfer the date the button was pressed into column B.

Thanks again

Spike
 
Joined
Dec 5, 2008
Messages
6
Reaction score
0
The better way is to write own function.

1. Open VBE window -> ALT+F11
2. Insert Module (menu Insert -> module)
3. Copy function:
Option Explicit 'declare variants

Function GetDateDiff(dStartDate As Date, dEndDate As Date) As Integer
GetDateDiff = DateDiff("d", dStartDate, dEndDate)
End Function
4. In Sheet1 enter the fallowing values/formulas (you will recognize formulas by "=")
A1 -> =TODAY()
B1 -> date, for example: Mar 22,2010
C1 -> =GetDateDiff(A1;B1)
5. Save you work
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top