REAL-TIME

VBA Projects

Full Access with Source Code
  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

Effortlessly
Manage Your Projects

120+ Project Management Templates

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Share Post

VBA (Visual Basic for Applications) is a programming language used in Microsoft applications to automate tasks and create custom functions. The Minute function in VBA is used to extract the minute portion from a given time. It can be used in various scenarios, such as calculating overtime pay, tracking meeting durations, or scheduling tasks within a specific timeframe.

VBA Minute Function – Purpose, Syntax and Arguments

Purpose:

The Minute function is used to retrieve the minutes value from a given time and can be used in conjunction with other VBA functions to perform calculations or manipulate data.

Syntax:

Minute(time)

Arguments:

  • time: This is a required argument and represents the time value from which the minutes are to be extracted.

Example:

Suppose we have a list of meeting durations in cell A1:A5 as follows:

A1: 13:23
A2: 09:45
A3: 10:30
A4: 15:50
A5: 11:15

To extract the minutes from these times and calculate the total duration, we can use the following code:

Dim totalMinutes As Integer
For Each cell In Range("A1:A5")
    totalMinutes = totalMinutes + Minute(cell.Value)
Next cell
MsgBox "Total Meeting Duration: " & totalMinutes & " minutes."

The result will be a message box showing the total duration of all the meetings in minutes, which in this case is 60 minutes.

Remarks:

  • The Minute function can only be used on time values and will return an error if applied to non-time values.
  • If the time is represented in a string format, the Minute function will convert it to a time value before extracting the minutes.
  • The Minute function will always return a number between 0 to 59.
  • If the time value has no minutes component, the Minute function will return 0.

Important Notes:

  • The Minute function is not available in Excel but can be used in VBA code.
  • It is important to properly format the cells containing time values to ensure accurate results when using the Minute function.
  • The Minute function is just one of many date and time functions available in VBA for various data manipulation and calculation purposes.

Understanding VBA Minute Function with Examples

VBA Minute function, which returns the minutes portion of a specific time value. In this blog post, we will take a closer look at the Minute function and its usage in VBA, along with some examples to better understand its functionality.

Example 1: Basic Usage of the Minute Function

The most basic usage of the Minute function is to return the minutes component of a specific time value. It takes a time value or a string representing a time value as an argument and returns an integer between 0 and 59. Let’s take a look at the following code to understand it better:

Sub basicExample()
    Dim time As Date
    time = TimeValue("11:30:45 AM")
    Dim minutes As Integer
    minutes = Minute(time)
    MsgBox "The minutes portion is: " & minutes
End Sub
  1. The first line of the code declares a variable ‘time’ of the data type ‘Date’ to hold the time value.
  2. The next line uses the ‘TimeValue’ function to assign a specific time value to the variable ‘time.’ In this example, we have used ’11:30:45 AM.’
  3. The third line declares a variable ‘minutes’ of the data type ‘Integer’ to store the minutes component.
  4. We then use the Minute function by passing the variable ‘time’ as an argument to get the minutes value and assign it to the variable ‘minutes.’
  5. Finally, we display the result using the ‘MsgBox’ function, along with a string to make it more understandable.

The result of the above code will be a message box displaying “The minutes portion is: 30,” as the minutes component in the time value ’11:30:45 AM’ is 30.

Example 2: Handling Incorrect Time Values and Errors

The Minute function in VBA is also useful in handling incorrect time values and preventing errors in the code. Let’s look at an example where the user inputs a time value, and the code uses the Minute function to extract the minutes portion and handle any possible errors.

Sub errorHandlingExample()
    Dim userInput As String
    userInput = InputBox("Please enter a time value:")
    Dim time As Date
    
    'Using On Error statement to handle possible errors
    On Error GoTo errorHandler
    
    time = TimeValue(userInput)
    Dim minutes As Integer
    minutes = Minute(time)
    MsgBox "The minutes portion is: " & minutes
    
    'Code continues if no error occurs
    Exit Sub
    
errorHandler:
    'Show error message if an error occurs
    MsgBox "Please enter a valid time value."
End Sub
  1. The first line of the code uses the ‘InputBox’ function to prompt the user to enter a time value.
  2. The second line declares a variable ‘time’ of the data type ‘Date’ to hold the time value.
  3. We use the ‘On Error’ statement to specify the error handling routine, in this case, we have used ‘errorHandler.’
  4. The next line uses the ‘TimeValue’ function to assign the user input to the variable ‘time.’
  5. The Minute function is then used to extract the minutes component and assign it to the variable ‘minutes.’
  6. The ‘MsgBox’ function is used to display the result in a message box. If no error occurs, the code will continue, and the ‘Exit Sub’ statement will stop the error handling routine.
  7. If an error occurs, the code will jump to the ‘errorHandler’ label, and the ‘MsgBox’ function will display an error message.

This example shows how the Minute function, along with other VBA functions, can be used to handle errors and provide a more user-friendly experience.

Example 3: Combining Minute Function with Other Functions

The Minute function can also be used in combination with other VBA functions to perform more complex calculations. Let’s take a look at an example where we use the Minute function in conjunction with the ‘TimeSerial’ and ‘DateSerial’ functions to calculate the duration between two time values.

Sub combinedExample()
    Dim startTime As Date
    startTime = TimeValue("9:30:00 AM")
    Dim endTime As Date
    endTime = TimeValue("3:45:30 PM")
    
    'Using the DateSerial and TimeSerial functions to calculate duration
    Dim duration As Double
    duration = TimeSerial(Hour(endTime), Minute(endTime), Second(endTime)) - TimeSerial(Hour(startTime), Minute(startTime), Second(startTime))
    
    MsgBox "The duration is: " & duration & " seconds."
End Sub
  1. The first two lines of the code declare variables ‘startTime’ and ‘endTime’ of the data type ‘Date’ to hold the starting and ending time values, respectively.
  2. The ‘DateSerial’ function is used to create a valid date value by passing the hour, minute, and second components from the ‘endTime’ variable. This value is then subtracted from the date value created using the ‘TimeSerial’ function and the components from the ‘startTime’ variable.
  3. The result is stored in a variable ‘duration’ of the data type ‘Double.’
  4. The ‘MsgBox’ function displays the result in a message box, along with a string and the duration value in seconds, which is calculated by subtracting the two time values.

In this example, we have combined the Minute function with the ‘TimeSerial’ and ‘DateSerial’ functions to calculate the duration between two time values and display the result in seconds.

In Conclusion

In this blog post, we have explored the usage of the Minute function in VBA, along with three different examples. We have seen how it can be used to extract the minutes portion from a time value, handle errors, and perform complex calculations by combining it with other VBA functions. The Minute function is just one of the many useful functions in VBA that makes it a powerful and efficient programming language for creating various applications in Microsoft Office.

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBA FunctionsTags: , , , Last Updated: September 30, 2023

Leave A Comment