How to create a user-defined function in Excel 2007 or Excel 2010
Function IsLeap(iYear)
If (iYear Mod 400) = 0 Then
IsLeap = True
ElseIf (iYear Mod 100) = 0 Then IsLeap = False
ElseIf (iYear Mod 4) = 0 Then IsLeap = True
Else: IsLeap = False
End If
End Function
=============================================
Function summ(a As Double, b As Double)
summ = a * b
End Function
If you are new to the VBA macros in MS Excel, follow these instructions:
Sub SentenceCase()
For Each cell In Selection.Cells
s = cell.Value
Start = True
For i = 1 To Len(s)
ch = Mid(s, i, 1)
Select Case ch
Case "."
Start = True
Case "?"
Start = True
Case "a" To "z"
If Start Then ch = UCase(ch): Start = False
Case "A" To "Z"
If Start Then Start = False Else ch = LCase(ch)
End Select
Mid(s, i, 1) = ch
Next
cell.Value = s
Next
End Sub
If you know Visual Basic programming then you can create your own user-defined functions in Excel 2007 or Excel 2010. Follow the steps below:
1. Click on Excel Developer tab. If you can’t see the developer go to office button >popular >enable developer tool
3. You will be switched to Visual Basic Editor Menu.
4. On the Insert menu (Visual Basic Editor) select Module.
5. This will open the code window.
6. Type the Visual Basic code for your function.
7. On the File menu click Close and return to Microsoft Excel.
8. Your new function should now be ready for use like any other Excel function.
Function to decide whether a year is a leap year or not
Function IsLeap(iYear)
If (iYear Mod 400) = 0 Then
IsLeap = True
ElseIf (iYear Mod 100) = 0 Then IsLeap = False
ElseIf (iYear Mod 4) = 0 Then IsLeap = True
Else: IsLeap = False
End If
End Function
=============================================
Function summ(a As Double, b As Double)
summ = a * b
End Function
==============================================
If you are new to the VBA macros in MS Excel, follow these instructions:
- Create a new or blank Excel workbook.
- Press ALT+F11 to open the Visual Basic Editor in MS Excel.
- Go to 'Insert' in the Menu --> Select 'Module'.
- Copy and Paste any or all of the following macro in the new module sheet.
TO UPPER CASE
1
2
3
4
5
6
7
8
9
10
| Sub Change_to_Upper_Case() ' This module will change case of selected cells ' to UPPER CASE or CAPITAL letters. On Error Resume Next Dim MyCell As Range For Each MyCell In Selection.Cells MyCell.Value = UCase(MyCell.Value) Next On Error GoTo 0 End Sub |
to lower case
1
2
3
4
5
6
7
8
9
| Sub ChangeLCase() ' This module will change case of selected cells to lower case. On Error Resume Next Dim MyCell As Range For Each MyCell In Selection.Cells MyCell.Value = LCase(MyCell.Value) Next On Error GoTo 0 End Sub |
To Proper Case (First letter of each word is capital in the sentence)
1
2
3
4
5
6
7
8
9
| Sub ChangePCase() ' This module will change case of selected cells to Proper Case. On Error Resume Next Dim MyCell As Range For Each MyCell In Selection.Cells MyCell.Value = WorksheetFunction.Proper(MyCell.Value) Next On Error GoTo 0 End Sub |
To sentence case (Only first letter of first word is capital in the sentence.)
Sub SentenceCase()
For Each cell In Selection.Cells
s = cell.Value
Start = True
For i = 1 To Len(s)
ch = Mid(s, i, 1)
Select Case ch
Case "."
Start = True
Case "?"
Start = True
Case "a" To "z"
If Start Then ch = UCase(ch): Start = False
Case "A" To "Z"
If Start Then Start = False Else ch = LCase(ch)
End Select
Mid(s, i, 1) = ch
Next
cell.Value = s
Next
End Sub
To ToGgGlE CaSe (Alternate letters are Capital and small.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| Sub ChangeTCase() ' This module will change case of selected cells to ToGgLe CaSe. Dim MyCell As Range Dim i As Integer On Error Resume Next For Each MyCell In Selection.Cells If Len(MyCell.Value) >= 2 And IsNumeric(MyCell.Value) = False And _ IsEmpty(MyCell.Value) = False And IsNull(MyCell.Value) = False Then For i = 1 To Len(MyCell.Value) Step 2 MyCell.Characters(i, 1).Text = UCase(MyCell.Characters(i, 1).Text) Next For i = 2 To Len(MyCell.Value) Step 2 MyCell.Characters(i, 1).Text = LCase(MyCell.Characters(i, 1).Text) Next End If Next On Error GoTo 0 End Sub |
How to use?
To run these macros, simply press F5 while placing the cursor inside the macro code.
OR From MS Excel window, press 'Alt+F8' to select the macro and then hit the 'Run' button.
No comments:
Post a Comment