问题
I'm designing a set of related excel file which are related between them. The objective is that the macros which refere to each other document, can work in any given computer/path. For this reason I have used a set of relative path which lets the macros work well.
I have used the follwoing functions:
=+CELDA("nombrearchivo";$A$1)
"nombredearchivo"
means "filename"
in english.
The problem here is that this function only works when the computer is setup in Spanish, but when the files are transferred to a English set up computer, it translates de function CELDA to CELL, but not the "nombrearchivo".
To solve it I have thought about trying to show in cell the language in which Excel is setup and then write an if function with three main languages which would display the nombrearchivo, filename or the same in any other language.
Is it possible to show in a cell the language in which excel is setup??
The objective is that the macros ccan work at any given computer and path.
回答1:
This answer is totally extracted from this source:
https://www.mrexcel.com/forum/excel-questions/617870-language-code-french-spanish-spanish.html
So all credits go to original author Leith Ross:
' Written: March 01, 2012
' Author: Leith Ross
' Summary: Converts a Language Code Identifier (LCID) into the language name.
Private Declare Function GetLocaleInfoA _
Lib "kernel32.dll" _
(ByVal Locale As Long, _
ByVal LCType As Long, _
ByVal lpLCData As String, _
ByVal cch As Long) _
As Long
Function GetLanguageName(ByVal LCID As Long) As String
Const LOCALE_SENGLANGUAGE As Long = &H1001
Const LOCALE_SENGCOUNTRY As Long = &H1002
Dim Buffer As String
Dim BuffSize As Long
Dim RetVal As Long
BuffSize = GetLocaleInfoA(LCID, LOCALE_SENGLANGUAGE, 0, 0)
Buffer = String(BuffSize, Chr(0))
RetVal = GetLocaleInfoA(LCID, LOCALE_SENGLANGUAGE, Buffer, BuffSize)
If RetVal > 0 Then GetLanguageName = Left(Buffer, BuffSize - 1)
End Function
To test it, just type in a cell =GetLanguageName(1034)
(decimal value) but it works also with Hex value, like =GetLanguageName("&H40A")
I got this in my Excel:
And to get the decimal number you need to type, you can use something like this:
Function GetLanguage() As String
GetLanguage = GetLanguageName(Application.LanguageSettings.LanguageID(msoLanguageIDUI))
End Function
So typing in a cell GetLanguage()
will return the user's language.
Hope you can adapt this to your needs.
回答2:
This will return you a lang code go here to understand what LCID is..
And Here to get the list of all LCID code signification
for exemple : 1036 = French - Standard
dim lang_code as long
lang_code = Application.LanguageSettings.LanguageID(msoLanguageIDUI)
Application.LanguageSettings Doc Here
回答3:
I believe you are asking for X but your actual problem is Y. See What is the X-Y-Problem. Therfore I write an answer for Y and not for what you have actually asked for:
To get the path of the current workbook in VBA use the Workbook.Path property
ThisWorkbook.Path
Use this in your macros to build your relative path.
For example if ThisWorkbook.Path
is something like C:\My\Path\To\Workbook
then you can use it like
ThisWorkbook.Path & "\Subfolder"
to access the subfolder of the path where the current workbook was savedC:\My\Path\To\Workbook\Subfolder
.
Paths in formulas should adjust automatically if you move all the files into anothor folder.
回答4:
It is a petty that you cannot request the language of the user. It is not only needed in date-format functions, many times we need to know the languange in indirect-functions as well. All indirect parts of formulas (which are used between quotes) is fixed text, while formulas are automatically 'translated' to the users language. For parts in a formula which is between quotes (like a format of a date), excel has no solution. So we cannot request the language, however, from Excel 2016 and above, there is a possibility to get a formula in text. The English name of that function is FORMULATEXT (please find the translated function name of your own language). This means that we can use this function to create our own way to detect a language. If you wnat to create an international sheet, it is a lot of work, but of you only need to support 2 or 3 languages, it can be used.
Place in Cell A1 the formula: =IF(A2>20,A2,A2) This formula doesn't do anything.
Place in Cell A2 the formula: =MID(LEFT(FORMULATEXT(A1),FIND("(",FORMULATEXT(V2))-1),2,20)
Now we know the language. If cell A2="IF", the language is English, if it is "WENN" it is German, if it is "COMME" it is French and if it is "ALS" it is Dutch, etc.
Be aware that if you have users with older versions of excel, this function isn't available and you cannot detect the language (at least not in this way). I added an ISERROR to this. In case of an error (older versions), I only support Dutch (in my case :) )
来源:https://stackoverflow.com/questions/57198673/detect-the-language-in-which-excel-is-setup-and-show-it-in-a-cell-of-the-file