This tutorial shows you how to enter worksheet functions in Excel VBA.
How to enter worksheet function in Excel VBA
The function must be preceded by the word "WorkheetFunction" and then place "." (period).
Download the workbook used in the examples shown below.
The data are shown in the image below:
Example 1 : Sum numbers in a range
Suppose you have data in range A2:A8. You want to sum numbers entered in the range A2:A8.
Example 2 : Sum numbers in two columns
Suppose you have data in column A and B. You want to sum the columns of numbers.
Example 3 : Count the number of used cells in two columns
Suppose you have data in column A and B. You want to count the number of used cells in the columns.
Example 4 : Conditional count of a range of numbers
Suppose you have data in range A2:A8. You want to count the number of cells that have value greater than 20.
The data used in this example is shown in the image below :
How to enter worksheet function in Excel VBA
The function must be preceded by the word "WorkheetFunction" and then place "." (period).
Download the workbook used in the examples shown below.
The data are shown in the image below:
Example 1 : Sum numbers in a range
Suppose you have data in range A2:A8. You want to sum numbers entered in the range A2:A8.
Sub SumRange()
Dim result As Double
result = WorksheetFunction.Sum(Range("A2:A8"))
MsgBox result
End Sub
Example 2 : Sum numbers in two columns
Suppose you have data in column A and B. You want to sum the columns of numbers.
Sub SumColumn()
Dim cresult As Double
cresult = WorksheetFunction.Sum(Columns(1), Columns(2))
MsgBox cresult
End Sub
Example 3 : Count the number of used cells in two columns
Suppose you have data in column A and B. You want to count the number of used cells in the columns.
Sub countcolumn()
Dim cresult As Double
cresult = WorksheetFunction.CountA(Columns(1), Columns(2))
MsgBox cresult
End Sub
Example 4 : Conditional count of a range of numbers
Suppose you have data in range A2:A8. You want to count the number of cells that have value greater than 20.
Sub condcount()Example 5 : VLOOKUP Function
Dim cresult As Double
cresult = WorksheetFunction.CountIf(Range("A2:A8"), ">20")
MsgBox cresult
End Sub
The data used in this example is shown in the image below :
Sub look()Download the workbook
Dim cresult As Double
cresult = WorksheetFunction.VLookup("Deepanshu", Range("D2:E5"), 2, 0)
MsgBox cresult
End Sub
USEFUL INFORMATION WITH REAL TIME SCENARIOS ..
ReplyDelete