Use of System date and user-input date in Windows VS Mac - excel

I have been using this piece of code on my PC running windows and Excel 2013 to return a date.
As shown in the code, the systems prompts for the use of current system date VS use of user-input date.
Function GetRegDate() As Date
Dim RegToday As String
RegToday = MsgBox("Are the transaction(s) of today (System time)?",vbYesNo)
If RegToday = vbYes Then
' Use Current System Date
GetRegDate = Date
Else
' Use user-input date
Dim RegYear, RegMonth, RegDay As Long
RegYear = InputBox("Please enter the year of the transaction.")
RegMonth = InputBox("Please enter the month of the transaction (in number).")
RegDay = InputBox("Please enter the day of the transaction.")
GetRegDate = RegDay & "/" & RegMonth & "/" & RegYear
End If
' Some other codes
End Function
The piece of code has been consistently successful on my PC. But it isn't the case when my friend uses it on his Mac running Excel 2011.
Taking 26 Mar 2016 As an example, on my PC, by using the current system date or the user-input date method, the value "#26/3/2016#" is stored in the variable 'GetRegDate', as reflected from the local variable windows.
But as my friend told me, when using current system date, the value "##" is stored. When using the user-input date method, an error occurs at the line
GetRegDate = RegDay & "/" & RegMonth & "/" & RegYear
with the runtime error 13 type mismatch
I have no idea why this happened. Any help is appreciated!

This is a known bug. From Microsoft Communities:
This is known bug with some of the region settings, working OK when
you use the Dutch region but not for example when you use the French
settings
Got one user that fix it by running this in the Terminal window
defaults write NSGlobalDomain AppleICUDateFormatStrings -dict 1
dd/MM/yy
I hope they fix it soon
That said, you shouldn't implicitly cast to a Date with GetRegDate = RegDay & "/" & RegMonth & "/" & RegYear. Use DateSerial to avoid region issues:
GetRegDate = DateSerial(RegYear, RegMonth, RegDay)

Related

How to format a variable as currency inside of a string

All of the following is happening within an If Statement
I have a variable, Difference, that is the result of a subtraction of two monetary values.
I then have a another variable, Message, which looks as follows:
If AssessedValue < ProposedValue Then
Difference = Format(ProposedValue - AssessedValue, "Currency")
Message = "Average value is " & Difference & " more than the current
appraised value. Do NOT recommend negotiations."
Else
Difference = Format(AssessedValue - ProposedValue, "Currency")
Message = "Average value is " & Difference & " less than the current
appraised value. Recommend negotiations."
End If
MY issue is that in the message that displays, the difference does not display as currency but just an un-formatted number instead of currency (Example of the message below)
Average value is 21587 more than the current appraised value. Do NOT
recommend negotiations.
How can I get the 21587, in this example, to appear in the message as $21,587.00?
Thanks in advance for any help with this issue.
Kirk
There are a couple of ways to fix your issue:
Format Difference using a "Currency" format prior to including it in the string, e.g.
Message = "Average value is " & Format(Difference, "Currency") & " more than the current appraised value. Do NOT recommend negotiations."
Declare Difference to be a String rather than a numeric type. (I'm guessing you have declared it currently as a Currency type.)
Dim Difference As String
This will ensure that the result of Format(ProposedValue - AssessedValue, "Currency") is not converted back to a numeric value in order to be stored in your Difference variable.

Now() is giving an error

I am getting an error in the below code.
Dim CurrentTime As Date
CurrentTime = Format(Now(), "hh:mm AM/PM")
If CurrentTime = ActiveSheet.Range("I1") Then
Call SendEMail
End If
When the time is right, then the macro is debugging and Now is highlighted.
Could anyone solve this problem?
You are not getting an actual error are you? It is just not working as expected.
Matt Cremeens has identified your issue I believe, you have declared CurrentTime as a date data type. A date data type stores a number representing a time/date, however you are asking for it to store string information (AM/PM) too which is can't so it is getting stripped out.
The results is cell one may hold a value like '10:30 AM' but the Format(Now(), "hh:mm AM/PM") code going into the Date variable is resulting in '10:30:00', so the two are never matching as strings. Copy as Matt has suggested and this should work (Dim CurrentTime As String).
Better yet, use the date comparison function DateDiff:-
If DateDiff("s",ActiveSheet.Range("I1"),Time()) > 0 then
SendEmail
End If
This is saying if the time now is greater than the value in I1 (to the second) then run SendEmail.
I don't have the environment to test the solution right now but from what I remember you don't need the brackets in 2007 and also you don't need the format.
Try the following code and see if that fits your need:
If Hour(Now) = ActiveSheet.Range("I1") Then
(...)
End If

VBA SaveAs Method

After browsing forums for well over an hour, I can't seem to figure out why my save as code isn't working.
My goal is to save a new copy of the workbook under a different file type. (The current file type is .csv.) I'm not trying to save in a new or different location, the current directory is where I want it to save to.
I've tried more variations of the following code than I can remember, so I'll just post my current syntax:
CurrentDir = CurDir()
dateVal = Date
ActiveWorkbook.SaveAs Filename:="" & CurrentDir & "ALS Week of " & dateVal - 4 & ".xlsx", FileFormat:=51
I've looked at examples of how to open a dialog box wherein the user inputs information in order to save. But I'm hoping for a hands-off approach. If anyone can see where my error lies, please let me know!
EDIT:
The error that I get is "Method 'SaveAs' of object '_Workbook' failed
Your date has illegal characters in it. Format your date with dashes and not slashes and this won't happen.
The following reserved characters:
< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
You forgot a backslash:
ActiveWorkbook.SaveAs Filename:= CurDir() & "\ALS Week of " & (Date - 4) & ".xlsx", FileFormat:=51

VBA: Get current month in russian language

I`m trying to get current month while my system language changed to "Russia",it gives "??????".
Code:
Dim presDate as String
presDate = Format(Date,"ddmmmmyyyy")
my above code gives output as "??????".I`m using Excel 2010.
Please suggest an answer.
Cyrillic letters should be displayed properly in VBE Editor, including debug window, if you change Language for non-Unicode programs to Russian in Control Panel - Regional and Language Options.
This code works. It would be called as '=mydate(date_value)', where date_value can be a reference to a cell with a date in it, a string value such as '"11/2/96"', an Excel function that returns the system's current date (e.g., '=mydate(TODAY())' or '=mydate(NOW()).
Option Explicit
Function mydate(cel As Variant) As String
If (TypeName(cel) = "Range") Then
mydate = Format(cel.Value, "mmmm dd yyyy")
ElseIf TypeName(cel) = "String" Then
mydate = Format(datevalue(cel), "mmmm dd yyyy")
ElseIf TypeName(cel) = "Double" Then
mydate = Format(Round(cel, 0), "mmmm dd yyyy")
Else
mydate = CVErr(xlErrValue)
End If
End Function
When I do this in the immediate window
Debug.Print Format(now,"ddmmmmyyyy")
in the immediate window I get
26???????2013
and when I do
cells(1,1)=Format(now,"ddmmmmyyyy")
in cell A1 I get
26февраля2013
I think the VBA IDE just can not do non-latin characters (probably limited to ASCII).

Format a cell as arbitrary currency regardless of locale, using VBA

This is really bugging me as it seems pretty illogical the way it's working.
I have a macro to format a cell as a currency using a bit of code to obtain the currency symbol.
Here is the code involved:
Dim sym As String
sym = reportConstants(ISOcode)
'Just use the ISO code if there isn't a symbol available
If sym = "" Then
sym = ISOcode
End If
With range(.Offset(0, 3), .Offset(3, 3))
.NumberFormat = sym & "#,##0;(" & sym & "#,##0)"
Debug.Print sym & "#,##0;(" & sym & "#,##0)"
End With
reportConstants is a dictionary object with currency symbols defined as strings. E.g. reportConstants("USD") = "$". This is defined earlier in the macro.
When the macro runs it gets the ISO code and should then format the cell with the corresponding currency symbol.
When I run it in one instance the ISO code is "USD" - so sym is defined as "$" - but it still formats the cell with a pound sign (£). When I debug.print the format cell string it shows $#,##0;($#,##0) so, as long as I got my syntax correct, it should use a dollar sign in the cell. But it uses a £ sign instead. (I am running a UK version of excel so it may be defaulting to £-sign, but why?)
Any help greatly appreciated.
I just recorded a macro to set the format to $xx.xx and it created this: [$$-409]#,##0.00. Looks like the -409 localises the currency to a particular country; it works without it - try changing yours to .NumberFormat = "[$" & sym & "]#,##0.00"
Btw guess I read your question somewhat after posting ;) Excel is well influenced by the regional settings of your computer for currency, language, dates... Using numberformat can force it to keep the sign you require. if it is a matter of rounding up you can try to: On Excel 2010, go to File - Options - Advanced and scroll down to "When calculating this workbook" and click on the "set precision as displayed" and OK out. 
Try this: given your values are numerics/ integers/decimals....
Range("a2").Style = "Currency"
Or you can use format:
Format(value, "Currency")
Format(Range(a2).value, "Currency")
References:
http://www.mrexcel.com/forum/excel-questions/439331-displaying-currency-based-regional-settings.html
http://www.addictivetips.com/microsoft-office/excel-2010-currency-values/
(PS: I am on mobile, you may try these two links)

Resources