Find My PC Screen Resolution in Excel using VBA & Javascript

What is Display Resolution?

The number of Pixels displayed in your Monitor is called Screen Resolution.

It is represented as how many Pixels displayed in Screen height x Screen width. To get the screen height and width use these codes as per your programming language. The computer or mobile devices do not have standard screen resolutions.

It varies with every manufacturer and every computer model. So, the developers have to design their application to be compatible with the user’s device resolution limits.

Be it an application developer or a Web developer, everyone have to use any of these codes at some point of time.

1. What is my Screen Resolution? – VBA using Windows API

The VBA code below retrieves screen resolution parameters using 2 different methods, both using the Win api functions.

Option Explicit
'For Method 1
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
'For Method 2
Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long

Private Sub VBA_Screen_Resolution()
    'Method 1
    Dim rScreen As RECT
    GetClientRect CLng(GetDesktopWindow()), rScreen
    ThisWorkbook.Sheets(1).Cells(1, 1) = "Left"
    ThisWorkbook.Sheets(1).Cells(2, 1) = "Right"
    ThisWorkbook.Sheets(1).Cells(3, 1) = "Top"
    ThisWorkbook.Sheets(1).Cells(4, 1) = "Bottom"
    ThisWorkbook.Sheets(1).Cells(1, 2) = rScreen.Left
    ThisWorkbook.Sheets(1).Cells(2, 2) = rScreen.Right
    ThisWorkbook.Sheets(1).Cells(3, 2) = rScreen.Top
    ThisWorkbook.Sheets(1).Cells(4, 2) = rScreen.Bottom
    'Method 2
    MsgBox "Screen Resolution - " & GetSystemMetrics(SM_CXSCREEN) & " x " & GetSystemMetrics(SM_CYSCREEN)

End Sub

Note: Function used in method 2 in above example can be used in C Programming to get screen width & height. Include Windows.h and then use the function with same parameters in your C program.

Also Read: JavaScript Browser Detection

2. Get Display resolution in Javascript

It is easy to get screen width and height with Javascript. You just need to use the ‘Screen’ Object as in below code sample.

var wscr = “Screen Width: “ + screen.width
var hscr = “Screen Height: “ + screen.height
var awscr = “Screen Width excluding Taskbar: “ + screen.availWidth
var ahscr = “Screen Width excluding Taskbar: “ + screen.availHeight

The above can be tested right from your internet browser Firefox, Chrome or IE. To test it in Firefox, press Ctrl + Shift + q, then choose ‘Console’. Type this command ‘console.log (wscr);’ and press enter.

Firefox console will display the screen width.

3. Find Windows Screen Height & Width from Command Prompt

Open command prompt and type this command. Just so simplest of all.

wmic desktopmonitor get screenheight, screenwidth
'Or this command to open the windows screen resolution display settings.
rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,3

By now you would be able to answer ‘What is Display resolution’ of your computer using the above free codes. If you require the coding in any specific programming language, please leave us a comment.