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.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Want to know when a workbook was last modified? Want to put that date within the header of your worksheet? Here's how to ...Discover More
If you've got a list of potential words, and you want to know which of those potential words are real, you'll appreciate ...Discover More
Macros give you a great deal of control over creating, finding, renaming, and deleting files. This tip focuses on this ...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.