Распознавание числовых значений в начале текстовых строк с помощью функции Val в коде VBA Excel. Определение текущей версии приложения Excel.
Val — это функция, которая распознает цифры с начала строки и до первого нецифрового символа, и возвращает их как числовое значение соответствующего типа.
Синтаксис
string — строка; переменная или выражение, возвращающие строку.
Примечание
Функция Val прекращает чтение строки на первом символе, который она не может распознать как цифру. Если среди цифр встречаются точки (.), то первая интерпретируется как десятичный разделитель, а вторая — уже как текст, на ней распознавание числа прекращается.
Примеры
Примеры с функцией Val
Sub Primer() MsgBox Val(«15 2 .3 38 попугаев») ‘Результат: 152,338 MsgBox Val(«15.03.2022») ‘Результат: 15,03 MsgBox Val(«.03.2022») ‘Результат: 0,03 MsgBox Val(«Моя дата: 15.03.2022») ‘Результат: 0 MsgBox Val(«+7 900 000 11 22») ‘Результат: 79000001122 MsgBox Val(«+7(900)000-11-22») ‘Результат: 7 MsgBox Val(«-7-900-000-11-22») ‘Результат: -7 End Sub |
Определение версии Excel
Определение текущей версии приложения Excel из кода VBA с помощью функций Application.Version и Val:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Sub ВерсияПриложения() Dim a As String Select Case Val(Application.Version) Case Is = 9 a = » (Excel 2000)» Case Is = 10 a = » (Excel 2002)» Case Is = 11 a = » (Excel 2003)» Case Is = 12 a = » (Excel 2007)» Case Is = 14 a = » (Excel 2010)» Case Is = 15 a = » (Excel 2013)» Case Is = 16 a = » (Excel 2016)» Case Is = 17 a = » (Excel 2019)» Case Is < 9 a = » (старее Excel 2000)» Case Is > 17 a = » (новее Excel 2019)» Case Else a = «» End Select MsgBox «Версия приложения « & Application.Version & a End Sub |
Здесь функция Val необходима из-за того, что функция Application.Version возвращает номер версии приложения в текстовом формате:
Определение используемой версии Excel
‘http://www.cyberforum.ru/vba/thread1851214.html
Sub Bitnost() MsgBox «Welcome to Microsoft Excel version » & _ Application.Version & » running on » & _ Application.OperatingSystem & «!» End Sub |
11.12.2013, 17:27 |
[ Файл-пример (59.0 Kb) ] |
|
Для определения текущей версии Excel используйте такой код: Sub Version() Dim Ver as String Select Case Val(Application.Version) Case Is <= 8: Ver = «Microsoft Excel 20-го века» Case 9: Ver = «Microsoft Excel 2000» Case 10: Ver = «Microsoft Excel 2002» Case 11: Ver = «Microsoft Excel 2003» Case 12: Ver = «Microsoft Excel 2007» Case 14: Ver = «Microsoft Excel 2010» Case 15: Ver = «Microsoft Excel 2013» Case 16: Ver = «Microsoft Excel 2016» Case Else: Ver = «Версия не определена» End Select MsgBox «Вы работаете в » & Ver End Sub |
|
Добавил: Serge_007 | |
Для определения текущей версии Excel используйте такой код:
Sub Excel_Version() Dim Ver As String Select Case Val(Application.Version) Case Is <9: Ver = "Microsoft Excel 20-го века" Case 9: Ver = "Microsoft Excel 2000" Case 10: Ver = "Microsoft Excel 2002" Case 11: Ver = "Microsoft Excel 2003" Case 12: Ver = "Microsoft Excel 2007" Case 14: Ver = "Microsoft Excel 2010" Case 15: Ver = "Microsoft Excel 2013" Case 16: Ver = "Microsoft Excel 2016" Case 17: Ver = "Microsoft Excel 2019" Case 18: Ver = "Microsoft Excel 2021" Case Else: Ver = "Версия не определена" End Select MsgBox "Вы работаете в " & Ver End Sub
To determine the version of Excel of the user, and thus perform a different action depending on the version in use, you can use Val(Application.Version)
which returns (the number of) the version of Excel.
Here are the versions numbers of Excel to be used to conduct tests:
8 | Excel 97 (Mac: 98) |
9 | Excel 2000 (Mac: 2001) |
10 | Excel 2002 |
11 | Excel 2003 (Mac: 2004) |
12 | Excel 2007 (Mac: 2008) |
14 | Excel 2010 (Mac: 2011) |
15 | Excel 2013 (Mac: 2016) |
16 | Excel 2016 |
For example to test if the version of Excel is inferior to 2007, you may use this code:
If Val(Application.Version) < 12 Then
'If version of Excel is inferior to 2007
Else
'If version of Excel is equal or superior to 2007
End If
-
#1
Is there a way in vba to test to see what version of Excel is being used?
My macro would crash if their data would exceed the 65K row limit in Excel 2003. I have a message that pops up if they do that, and it handles the problem just fine, but if they are ALREADY using Excel 2007 (or whatever comes later) I don’t want the message to appear.
How do I test?
Jennifer
Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
-
#2
Application.Version
12.0 = Excel 2k7
-
#3
Application.Version
12.0 = Excel 2k7
Thank you. So how would I use that exactly? (Still on my first project here…)
Would this be right to test for Excel 2007 or later?
Code:
if application.version >= 12.0 then
'code goes here
end if
Thanks. I’d test it myself but I’m using 2003.
Last edited: Dec 7, 2008
-
#4
It depends on what you want to do, but there’s really no need to check if the version is greater than 12.0. If you only want your code to run if the user has 2007, just check for = 12.0.
-
#5
Ok, but when I enter 12.0 VBA changes the code to read 12# Is that what is supposed to happen?
I’m actually trying to make sure they have 2007 OR LATER so I don’t have to redo the code if they upgrade someday. I suppose testing for if what they are running = Excel 2003 instead would work. Do you happen to know the version number for 2003?
-
#6
9tanstaafl9,
Code:
Sub ReturnExcelVersion()
If Application.Version = "12.0" Then
MsgBox "You are using Excel 2007."
ElseIf Application.Version = "11.0" Then
MsgBox "You are using Excel 2003."
ElseIf Application.Version = "10.0" Then
MsgBox "You are using Excel 2002."
ElseIf Application.Version = "9.0" Then
MsgBox "You are using Excel 2000."
ElseIf Application.Version = "8.0" Then
MsgBox "You are using Excel 97."
ElseIf Application.Version = "7.0" Then
MsgBox "You are using Excel 95."
End If
End Sub
Have a great day,
Stan
-
#8
Thank you both. I’ve got it now. Sorry if this was a dumb one, but the search terms were too generic for me to find anything (or my searches were anyway). I appreciate the help. —Jennifer
G12
Board Regular
-
#9
Perfect. This is exactly what I needed to see also. Solved different problem and, for the record, if someone else looks back at this Excel 2010 is ?Application.Version = 14.0
-
#10
What happened to version 13???? Another MS conspiracy?!?!!?