2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Excel VBA プラットフォーム(OS)判定

Last updated at Posted at 2024-07-14

はじめに

Excel(VBA)が動作するプラットフォーム(OS)として、大きく WindowsMacの 2つがあります。
また、Windowsの中でも、32ビット版と64ビット版のExcelが選べます。

WindowsMacでは、OS依存のAPIはまったく異なりますし、パスの区切り文字も異なります。

  • Windows バックスラッシュ\(円マーク)
  • Mac スラッシュ/

動作システムのパスの区切り文字は、プラットフォーム(OS)を判定せずとも、
Application.PathSeparatorで取得できます。


しかし、プラットフォーム(OS)の判定が必要になるだろう、次のような場面が想定されます。

  • 現在実行しているプラットフォームを判定して、使用するAPIを切り分ける
    (32ビット版と64ビット版、WindowsとMac、など)
  • 前提にしているプラットフォーム以外の環境で動作させれた場合に、エラーメッセージを表示する(Sorry for this platform not supported)
  • etc

そのような場合の、プラットフォーム(OS)の判定方法をまとめます。

#If文による処理の切り分け

Excel VBA にも #Ifが用意されています。

Mac / Win16 / Win32 / Win64 を判定できます。
(コンパイル時の条件らしいが、本当ですかね?)

64ビット版でも"Win32"の定数が定義されているため、
Win64 → Win32 → Win16 の順に判定する必要がある。

Sub Test01()
#If Mac Then
    Debug.Print "Mac"
#ElseIf Win64 Then
    Debug.Print "Win64"
#ElseIf Win32 Then
    Debug.Print "Win32"
#ElseIf Win16 Then
    Debug.Print "Win16"
#Else
    Debug.Print "unknown"
#End If
End Sub


VBAのバージョン( VBA6 / VBA7 )も判定できます。

VB7版でも"VB6"の定数が定義されているため、VB7 → VB6 の順に判定する必要がある。

Sub Test03()
#If VBA7 Then
    Debug.Print "VBA7"
#ElseIf VBA6 Then
    Debug.Print "VBA6"
#End If
End Sub

Application.OperatingSystemによる判定

OSのシステム情報のような内容が取得できます。
"Windows""Mac"の文字列を含むようなので、実行時にOSの判定に使えます。

Sub Test02()
    If Application.OperatingSystem Like "*Win*" Then
        'for Windows
    ElseIf Application.OperatingSystem Like "*Mac*" Then
        'for Mac
    Else
        'unknown
    End If
End Sub

取得例

実際に取得した結果は、以下のとおりです。

  • Windows 11 Pro 23H2 OSビルド 22631.3880
    Microsoft® Excel® for Microsoft 365 MSO (バージョン 2406 ビルド 16.0.17726.20078) 64ビット

scrw.png

以下の打ち消し線の文章は、判定する順番に誤りがあったためです。
#Ifは安心して使えます。(訂正してお詫びいたします)

64ビットOSで、Excelも64ビット版ですが、なぜか"Win32"と判定されました。互換性のためでしょうか?

VB6 / VB7 の判定を追加して再度実行すると、結果が先ほどとは違って、"Win64"になりました??(えっ!)
"Win64"なら"VB7"になるはずですが、#If怪しすぎです!!!

#IfよりApplication.OperatingSystemの方が信用できそう?)


 
Windows10の32ビット版が周りに無いので、WindowsOSの32ビット版と64ビット版をApplication.OperatingSystemで判定できるのか、確かめられません。


 

  • M1 Mac Sonoma バージョン14.5(23F79)
    Microsoft® Excel for Mac バージョン 16.86(24060916)
scr os-path.png

↑スクショにありませんが、このMacでも"VB6"となりました。

判定する順番を正したところ、"VB7"となりました。
 

  • Intel Mac Monterey 12.6(21G115)
scrrr.png

ARM / Intel を判別できます。
Windowsでも将来 ARM / Intel を判別できるようになるのでしょうかね。

公式情報

↑ 公式サイトに VB7とVB6の両方で動作させるコードの書き方として、次のコードが紹介されています。

"Win64"は、OSではく、Officeの64ビット判定と書いてあります。
ですが、Declare文の64ビット対応は"VBA7"で判定していますね・・・

#if Vba7 then 
'  Code is running in the new VBA7 editor 
     #if Win64 then 
     '  Code is running in 64-bit version of Microsoft Office 
     #else 
     '  Code is running in 32-bit version of Microsoft Office 
     #end if 
#else 
' Code is running in VBA version 6 or earlier 
#end if 
 
#If Vba7 Then 
Declare PtrSafe Sub...
#Else 
Declare Sub...
#EndIf

以上

2
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?