I came across a problem, I try to export a date by using IF-function in excel als followed,
=WENN(C2="Y";TEXT(HEUTE();"JJJJMMTT");"")
it is in German, which in English is,
=IF(C2='Y'; TEXT(TODAY();"YYYYMMDD");'')
The language of My computer is in German, so it works well, but my colleagues in USA had problem, which they can't export the date with correct date, it shows today as example "JJJJ16TT", it can't show the year and day...
I don't know how to set it right. So I try to ask help from you.
Thanks and best regards.
Two possible methods:
Don't use the TEXT function.
Change your formula to:
=WENN(C2="Y";HEUTE();"")
- and apply a number format to the cell without the LCID.
Especially if your code is part of a longer function, you can
Use a Defined Name for the date string
Create a Workbooks_Open event to change that Name depending on the language of the user
Change your formula to: =WENN(C2="Y";TEXT(HEUTE();dtFormat);"")
eg:
To enter this Macro (Sub), <alt-F11> opens the Visual Basic Editor.
In the Project Explorer window, select ThisWorkbook under the relevant VBA Project.
Paste the code below into the window that opens.
Option Explicit
'change text function date code
Private Sub Workbook_Open()
Dim yrCode As String, mnthCode As String, dyCode As String
Dim dtCode As String
Dim nM As Name
With Application
yrCode = WorksheetFunction.Rept(.International(xlYearCode), 4)
mnthCode = WorksheetFunction.Rept(.International(xlMonthCode), 2)
dyCode = WorksheetFunction.Rept(.International(xlDayCode), 2)
End With
'Can only add a name if it is absent
For Each nM In ThisWorkbook.Names
If nM.Name = "dtFormat" Then
nM.Delete
Exit For
End If
Next nM
dtCode = yrCode & mnthCode & dyCode
ThisWorkbook.Names.Add _
Name:="dtFormat", _
RefersTo:="=""" & dtCode & """", _
Visible:=False
End Sub
Related
I'm trying to have the current date recognized in Excel using an =TEXT(TODAY(),"mmmm d yyyy") function, but I have run into issues with Canadian users based in Quebec, who may have a system install set to English or French, and likewise an Office install in one language or the other. For today's date, my spreadsheet pulls March 10 2021 on my computer, but the result has been askew for each Quebec-based user I have tested with.
In order to resolve this, I set up a VBA function to pull the application's language setting, with the intention to use a simple IF to check the language code and change from "mmmm d yyyy" to "mmmm j aaaa" (the letters to match the French journée for day and année for year, as per consulting the formats available on French Excel's TEXTE function). My function worked... but the output has come back incorrect each time.
Public Function LangCheck()
Dim lang_code As Long
lang_code = Application.LanguageSettings.LanguageID(msoLanguageIDUI)
LangCheck = lang_code
End Function
So that did its job, but the outputs should have been March 10 2021 or mars 10 2021. Instead from two different users I have received:
March j Wednesday, from a user with a French Excel setting, confirmed by the code outputting language ID 1036.
mars d yyyy, from a user with an English Excel setting on a French OS, confirmed by the code outputting language ID 1033.
Excel is translating every function to its French cognate, but won't translate the formats for the TEXT function on its own. Does anyone have a suggestion for a VBA or function-based solve to this conundrum so that I can ensure consistent dates return regardless of language?
You can use the Application.International properties to extract the correct DMY tokens, and use a named reference for the text function format code:
Change your formula to:
=TEXT(TODAY(),"dtFormat")
And store the below code into the ThisWorkbook module
Wherever the file is opened, the Sub will run and ensure that the dtFormat name has the appropriate DMY codes for the locale.
Option Explicit
'change text function date code
Private Sub Workbook_Open()
Dim yrCode As String, mnthCode As String, dyCode As String
Dim dtCode As String
Dim nM As Name
With Application
yrCode = WorksheetFunction.Rept(.International(xlYearCode), 4)
mnthCode = WorksheetFunction.Rept(.International(xlMonthCode), 4)
dyCode = WorksheetFunction.Rept(.International(xlDayCode), 1)
End With
'Can only add a name if it is absent
For Each nM In ThisWorkbook.Names
If nM.Name = "dtFormat" Then
nM.Delete
Exit For
End If
Next nM
dtCode = mnthCode & " " & dyCode & " " & yrCode
ThisWorkbook.Names.Add _
Name:="dtFormat", _
RefersTo:="=""" & dtCode & """", _
Visible:=False
End Sub
I have the following vlookup in my code, but the tab 'ASN since 12.1.20 w FO' won't always be named that. However, it will always be the first tab in the workbook. How would I refer to tab/worksheet 1 instead of the name of the tab?
"=VLOOKUP(RC[-6],'ASN since 12.1.20 w FO'!C[-7]:C[9],17,FALSE)&"",""&TEXT(VLOOKUP(RC[-6],'ASN since 12.1.20 w FO'!C[-7]:C[9],14,),""MM/DD/YYYY"")"
'Build' a Formula in VBA
When 'building' fairly complicated formulas, it is a good idea to create a procedure to do just that. Every now and then you run it, until you're satisfied with the result.
If you prefer, you can use a message box instead of Debug.Print.
Option Explicit
Sub FormulaBuilder()
Dim wsName As String: wsName = ThisWorkbook.Worksheets(1).Name
Dim strFormula As String
strFormula = "=VLOOKUP(RC[-6],'" & wsName _
& "'!C[-7]:C[9],17,FALSE)&"",""&TEXT(VLOOKUP(RC[-6],'" & wsName _
& "'!C[-7]:C[9],14,),""MM/DD/YYYY"")"
Debug.Print strFormula
'MsgBox "strFormula"
End Sub
Disclaimer: I've read the relevant questions/answers regarding Error 1004 and I couldn't find a solution.
I have a macro that I distributed within the team and it works perfectly on all computers (including mine) except one. She's running the same version of Excel on the same system and she can use other macros except this one, I get the "Error 1004 SaveAs function failed" message. It is by a very long shot but did I miss some security setting or is there some problem with the code that could cause this? This is the code in question:
Sub PSSaveFile()
Dim myVal2 As Variant
Dim myValn2 As String
Dim myDate As String
Dim mFilePath As String
myVal2 = InputBox("Please enter today's date in mm-dd format")
myValn2 = Replace(myVal2, "-", "\")
myDate = Date
mFilePath = "\\xxxxxxxx003\xxx_emea\TCU_REPORTS\APPS\Reports\Regional\xxxxx for PC Web xx\2017\" & myValn2
ActiveWorkbook.SaveAs FileName:=mFilePath & "\xxxRHLogs-" & myDate & "_checked"
End Sub
Let me guess - the guy with the other computer is from another country, thus using his own regional settings on the PC. Thus the date format is a bit fancier, e.g. dd/mm/yyyy and it is not allowed to save it as a file because of this.
Try to change the date like this:
myDate = year(date) & month(date) & day(date)
and try again. This would eliminate the fancy settings part.
My coworkers often have to send out invites for interviews (up to dozens at a time). I am writing a macro to help them autogenerate Outlook meeting invites based on all of the interviewees' information, which we store in an Excel data file.
E.g., "Dear [INSERT NAME], We would like to invite you to an interview about [INSERT SUBJECT] on [INSERT DATE]. Please call into the interview using [INSERT PHONE NUMBER] and be prepared to talk about patients with [INSERT CONDITION]."
Because my coworkers do not know VBA I'm trying to have them write it in an input sheet and have the program read it and the formatting and store it in a variable called MeetingInviteBody. So I need to take a cell's value and read it as a variable definition. The problem is the entire thing is entered as a string, even if the cell's contents are part string and part reference to another variable. Is there a way to fix this?
Thank you in advance!
Addendum: I'm hoping this will clarify what I was trying to do. This was the macro I wrote as a test:
Sub MultipleDataTypeInput()
Dim FirstLineofText As Variant
Dim PhysicianName As String
PhysicianName="Dr. Smith"
FirstLineofText=Sheets("Sheet1").Cells(1,1).Value
MsgBox(Prompt:=FirstLineofText)
End Sub
I put "Dear" & PhysicianName & "," in cell A1, What I was hoping was for Excel to then read the macro as
FirstLineofText="Dear" & PhysicianName & ","
If that happened the MsgBox would say "Dear Dr. Smith,"
Instead the prompt was ""Dear " & PhysicianName & ",""
Does this make sense?
One way to do this would be to use the replace function in VBA.
However, in cell A1, write "Dear PhysicianName," and get rid of your & concatenation. (The way you wrote it would work if you were writing it strictly in VBA. The code would concatenate the values together. For example: FirstLineofText = "Dear " & PhysicianName & ",")
Sub MultipleDataTypeInput()
Dim FirstLineofText As Variant
Dim PhysicianName As String
PhysicianName="Dr. Smith"
FirstLineofText=Sheets("Sheet1").Cells(1,1).Value
FirstLineofText = Replace(FirstLineOfText,"PhysicianName",PhysicianName)
MsgBox(Prompt:=FirstLineofText)
End Sub
I suggest this way because you said the coworkers are writing their own scripts and i didn't want to suggest an entire new methodology as it may confuse you. That said, I think there are way more efficient ways to design this.
i think that you have your logic backward
you probably want this
Sub MultipleDataTypeInput()
Dim PhysicianName As String
Dim PatientName As String
PhysicianName = Sheets("Sheet1").Range("A1").Value ' this cell should hold "Dr. Smith"
PatientName = Sheets("Sheet1").Range("B1").Value
MsgBox "Dear" & PhysicianName & ", We would like to invite you to an interview about " & PatientName
End Sub
In VBA Help for the RefersTo Property, they give this example of listing all the Names in a Wkb (fleshed out so you can run it as is)
Sub showNames()'from VBA Help for "RefersTo"
Dim newSheet As Worksheet
Set newSheet = Worksheets.Add
Dim i As Long, nm As Name
i = 1
For Each nm In ActiveWorkbook.Names
newSheet.Cells(i, 1).Value = nm.Name
newSheet.Cells(i, 2).Value = "'" & nm.RefersTo
i = i + 1
Next
newSheet.Columns("A:B").AutoFit
End Sub
When I run that on my current project, it turns up many Names that I thought were long gone. But here they are still hanging around and referring to places that no longer exist. I think this is what's slowing up my system and I'd love to get rid of those Names, but they don't show up in the Define Name window so where do I find them?
edit: Meant to mention that the Links item is greyed out for this Wbk.
Update
option 1
A manual method to delete corrupt names using R1C1 (I can recall JKP stating on another forum he had code to do this but he wasn't prepared to provide it for free)
Select Tools, Options and click the General tab.
Click the check box next to "R1C1 Reference Style", so that you change the current setting.
Press OK.
Excel will prompt you to change the name of any name (in all open workbooks!) that contains illegal characters.
Select Insert, name, define to delete the newly renamed names.
Set the R1C1 Reference style back the way you prefer using Tools, Options, General.
option 2
Chris Neilsen posted this at Any chance to delete programatically corrupt ranged names (with spaces) in Excel (2007/2010)
But, here's a possible alternative: SaveAs your workbook as a .xlsm
You should get a dialog complaining about invalid names, with a option
to rename and a Ok to All button. Once saved, close and reopen the
file, Save As an .xls and you should be good to go
Initial Post
Download Name Manager which is the stand out addin by Jan Karel Pieterse and Charles Williams for managing names
It will handle Names that
now error out as the ranges have been deleted (your issue),
link to other Workbooks,
are now corrupt
Plus it will convert global names to local sheet names, and vice versa and so on
- Updated Answer -
Since you know the names of the invalid ranges but can't see them in the Name Manager, you can try to delete them manually from the VBA Immediate window. The name you gave GrPix!patternListRange indicates a worksheet name so you should be able to delete it by typing
ActiveWorkbook.Names("GrPix!patternListRange").Delete
or
Sheets("GrPix").Names("patternListRange").Delete
in the Immediate Window
Original Answer
Have you tried deleting the invalid names via code? i.e.
For Each nm In ActiveWorkbook.Names
If InStr(nm.RefersTo, "OldFileName.xls") > 0 Then
nm.Delete
End If
Next nm
Here are two more solutions that may work for others searching on this topic, but these still don't fix my own particular Workbook.
I'm still looking.
This is from Aaron Blood and shows the R1C1 method mentioned by brettdj:
Sub RemoveDemonLinks()
Dim wbBook As Workbook
Dim nName As Name
Dim i As Long
Set wbBook = ActiveWorkbook
i = 0
If wbBook.Names.Count > 0 Then
With Application
.ReferenceStyle = xlR1C1
.ReferenceStyle = xlA1
End With
For Each nName In wbBook.Name
If InStr(nName.RefersTo, "#REF!") > 0 Then nName.Delete
i = i + 1
Next nName
If i > 0 Then MsgBox i & " corrupted names was deleted from " & wbBook.Name
End If
End Sub
This is from MS Help
' Module to remove all hidden names on active workbook
Sub Remove_Hidden_Names()
' Dimension variables.
Dim xName As Variant
Dim Result As Variant
Dim Vis As Variant
' Loop once for each name in the workbook.
For Each xName In ActiveWorkbook.Names
'If a name is not visible (it is hidden)...
If xName.Visible = True Then
Vis = "Visible"
Else
Vis = "Hidden"
End If
' ...ask whether or not to delete the name.
Result = MsgBox(prompt:="Delete " & Vis & " Name " & _
Chr(10) & xName.Name & "?" & Chr(10) & _
"Which refers to: " & Chr(10) & xName.RefersTo, _
Buttons:=vbYesNo)
' If the result is true, then delete the name.
If Result = vbYes Then xName.Delete
' Loop to the next name.
Next xName
End Sub