Print GMT Time Using Excel VBA
DOWNLOAD USED EXCEL FILE FROM HERE>>
In today’s post we will tell you how to print GMT time using excel VBA, in this article we will explain you how to print GMT time using excel VBA. The current GMT time requirement has become more important as Excel improves its external data connections and is used globally. Incoming data feeds may contain date/time information for time zones other than the local computer. Stock data feed is an example of this.
Excel clients have to use some VBA to get the latest time and time zone offset information from Windows. There are many VBA code examples shared on the Internet, some more complex than others. We went to find a (relatively) simple solution to show you. Today we will use MICROSOFT WMI SCRIPTING V1.2 Library have to activate. Let us now understand that we will print GMT time, below are some examples which we will explain.
How to Print GMT Time Using Excel VBA
Well, you must know that How to Print GMT Time Using Excel VBA we will learn in this article. VBE, let’s understand. So, we have to go like last time, first go to the Developer Tab then click on the option of Visual Basic as shown in the image below.
On opening in VBE, you have to go to Insert and then Module has to be inserted, as can be seen in the image.
Microsoft WMI Scripting V1.2 Library must be activated to when using ‘wbemscripting‘. to activate go to Tools option and then References and on that we select Microsoft WMI Scripting V1.2 Library and click OK.
Once the module is inserted, and the library is activated, we’ll write a subroutine to Sub Print GMT Time ().
Sub PrintGMTTime() End Sub
For Variant, we will declare the variables being used in the project.
Sub PrintGMTTime() Dim cTime As Variant End Sub
Now we will define instant variable for WbemScripting. then useSWbemDateTime function of wbenscripting.
Sub PrintGMTTime() Dim cTime As Variant Dim DateObj As New WbemScripting.SWbemDateTime End Sub
Now we will store current time in cTime.
Sub PrintGMTTime() Dim cTime As Variant Dim DateObj As New WbemScripting.SWbemDateTime cTime = Now End Sub
We will use the SetVarDate Function here. The SetVarDate method of the SWbemDateTime object converts a date in the VT_DATE format to the CIM datetime format. bIsLocal [in, optional] If the value is FALSE, the value is interpreted as UTC with a zero (0) offset.
Sub PrintGMTTime() Dim cTime As Variant Dim DateObj As New WbemScripting.SWbemDateTime cTime = Now DateObj.SetVarDate cTime, True End Sub
Now we will use debug.Print to print the date with dateobj
Sub PrintGMTTime() Dim cTime As Variant Dim DateObj As New WbemScripting.SWbemDateTime cTime = Now DateObj.SetVarDate cTime, True Debug.Print "GMT Time:" & DateObj.GetVarDate(False) End Sub
Now we will Run Code
Now you can see here GMT (Greenwich Time) time is printed with date.
Now we will print with local time with debug.print to true in getVardate function of dateobj
Sub PrintGMTTime() Dim cTime As Variant Dim DateObj As New WbemScripting.SWbemDateTime cTime = Now DateObj.SetVarDate cTime, True Debug.Print "GMT Time:" & DateObj.GetVarDate(False) Debug.Print "Local Time:" & DateObj.GetVarDate(True) End Sub
Now we will Run Code
Now you can see here GMT (Greenwich Time) time with local time is printed with date.
Now we will print day. To print the day, we have to write the code. Here the date will be printed instead of the name of the day.
Sub PrintGMTTime() Dim cTime As Variant Dim DateObj As New WbemScripting.SWbemDateTime cTime = Now DateObj.SetVarDate cTime, True Debug.Print "GMT Time:" & DateObj.GetVarDate(False) Debug.Print "Local Time:" & DateObj.GetVarDate(True) Debug.Print DateObj.Day End Sub
Now you can see here GMT (Greenwich Time) time with local time is printed with date.
Now you can see in the immediate window that the GMT (Greenwich Time) time is printed along with the date along with the local time plus there is also a separate date. which is the current date.
And now we will print the date, month as well as year. So now we’ll add the year and month codes.
Sub PrintGMTTime() Dim cTime As Variant Dim DateObj As New WbemScripting.SWbemDateTime cTime = Now DateObj.SetVarDate cTime, True Debug.Print "GMT Time:" & DateObj.GetVarDate(False) Debug.Print "Local Time:" & DateObj.GetVarDate(True) Debug.Print DateObj.Day & "-" & DateObj.Month & "-" & DateObj.Year End Sub
Now we will click run button then see immediate window date is printed.
So, I hope you have understood How to Print GMT time using Excel VBA and for more information, you can follow us on Twitter, Instagram, LinkedIn, and YouTube as well.
DOWNLOAD USED EXCEL FILE FROM HERE>>
You can also see well-explained video here about How to Print GMT Time using Excel VBA