Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Calculating Time Differences between Two Machines.
by Allen Wyatt
(last updated August 23, 2014)
Don has a computer that has access to several different servers over his office network. These servers do not have their clocks synchronized, and Don was wondering if there was a way, in VBA, to determine the time discrepancy between a given server and his machine.
In order to figure out the time discrepancy, you'll need to figure out two times—one on your machine and one on the server—and then compare them. Getting the time on your own machine is easy enough; just use the Time function in VBA. Getting the time on another machine is more difficult, as there is no built-in function that facilitates this task.
You can, however, use an API function call to determine the time on a remote server. Exactly how you would develop such a function call is beyond the scope of this tip, but you can find a pretty good tutorial online for accomplishing the task:
Take a look at the page, and you can adapt the code to fit your needs. The fGetServerTime function returns a string that contains the complete date and time. You can then use the TimeValue function in your macro to convert this string into a native Excel time value. Once converted, you can compare the value to the internal system time to determine the discrepancy you need.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9268) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Calculating Time Differences between Two Machines.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Want to figure out the day of the month represented by a particular date? You can use the Day function in VBA to get the ...Discover More
Want to print a document by using a macro? One way is to display the Print dialog box and allow the user to interact with ...Discover More
If your workbook contains links, you are normally given the opportunity to update those links when you open the workbook. ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.