SlideShare a Scribd company logo
Useful Macros and functions for Excel




        This knowledge asset has information pertaining to Excel Macros and useful Functions.




         This document will let you know about some useful macros and functions, which are frequently
used by the users.



Topics Covered:


1.   Macro for Deleting Blank columns from a worksheet................................................................................ 1
2.   Macro uploading rows more than 65536. ................................................................................................... 2
3.   Macro for removing un-necessary columns from a worksheet................................................................... 3
4.   Formula for finding matching values present in column A and Column B................................................ 5
5.   Formula for showing distinct values in a column. ...................................................................................... 6




                                                                                                        Developer: Nihar R Paital
1. Macro for Deleting Blank columns from a worksheet.
We need to delete the column B, E as they are blank.


The below is the Procedure.

   1 Click "Tools," select "Macro" and choose "Macros."
   2 Type a name for your macro in the "Name" field, such as "DeleteBlankColumns" and click "Create."
   The Visual Basic Editor will open automatically.
   3 Double-click "(Name) Module" in the "Properties" window and type a name say "DeleteBlankCol"
   4 Click the "+" icon next to "Microsoft Excel Objects."
   5 Double-click "DeleteBlankCol" to open the "Code" window.
   6 Copy and paste the following into the "Code".

           Dim Col As Long, ColCnt As Long, Rng As Range
             Application.ScreenUpdating = False
             Application.Calculation = xlCalculationManual
           On Error GoTo Exits:
             If Selection.Columns.Count > 1 Then
                Set Rng = Selection
             Else
                Set Rng = Range(Columns(1), Columns(ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column()))
             End If
             ColCnt = 0
             For Col = Rng.Columns.Count To 1 Step -1
                If Application.WorksheetFunction.CountA(Rng.Columns(Col).EntireColumn) = 0 Then
                   Rng.Columns(Col).EntireColumn.Delete
                   ColCnt = ColCnt + 1
                End If
             Next Col
           Exits:
             Application.ScreenUpdating = True
             Application.Calculation = xlCalculationAutomatic

   7 Save the Macro.
   8 Click "Tools," select "Macro" and choose "Macros."
   9 Select the "DeleteBlankColumns" macro from the "Macros" dialog box and click "Run."
   This will delete all the blank columns. Like B and E.




                                                                                   Developer: Nihar R Paital
2. Macro uploading rows more than 65536.
 1 Create a text file say myhugedocument.txt having number of lines more than 65536.
 2 Click "Tools," select "Macro" and choose "Macros."
 3 Type a name for your macro in the "Name" field, such as "ImportFile" and click "Create." The Visual
 Basic Editor will open automatically.
 4 Double-click "(Name) Module" in the "Properties" window and type a name say "FileImport"
 5 Click the "+" icon next to "Microsoft Excel Objects."
 6 Double-click "FileImport" to open the "Code" window.
 7 Copy and paste the following into the "Code".

        'Dimension Variables
        Dim ResultStr As String
        Dim FileName As String
        Dim FileNum As Integer
        Dim Counter As Double
        'Ask User for File's Name
        FileName = InputBox("Please enter the Text File's name, e.g. test.txt")
        'Check for no entry
        If FileName = "" Then End
        'Get Next Available File Handle Number
        FileNum = FreeFile()
        'Open Text File For Input
        Open FileName For Input As #FileNum
        'Turn Screen Updating Off
        Application.ScreenUpdating = False
        'Create A New WorkBook With One Worksheet In It
        Workbooks.Add template:=xlWorksheet
        'Set The Counter to 1
        Counter = 1
        'Loop Until the End Of File Is Reached
        Do While Seek(FileNum) <= LOF(FileNum)
        'Display Importing Row Number On Status Bar
        Application.StatusBar = "Importing Row " & _
        Counter & " of text file " & FileName
        'Store One Line Of Text From File To Variable
        Line Input #FileNum, ResultStr
        'Store Variable Data Into Active Cell
        If Left(ResultStr, 1) = "=" Then
        ActiveCell.Value = "'" & ResultStr
        Else
        ActiveCell.Value = ResultStr
        End If
        'For Excel versions before Excel 97, change 65536 to 16384
        If ActiveCell.Row = 65536 Then
        'If On The Last Row Then Add A New Sheet
        ActiveWorkbook.Sheets.Add
        Else
        'If Not The Last Row Then Go One Cell Down
        ActiveCell.Offset(1, 0).Select
        End If
        'Increment the Counter By 1
        Counter = Counter + 1
        'Start Again At Top Of 'Do While' Statement
        Loop
        'Close The Open Text File

                                                                                  Developer: Nihar R Paital
Close
              'Remove Message From Status Bar
              Application.StatusBar = False


       8 Click "File" and select "Close" to close the Visual Basic Editor.
       9 Click "Tools," select "Macro" and choose "Macros."
       10 Select the "ImportFile" macro from the "Macros" dialog box and click "Run."
       11 Enter the name of your file (myhugedocument.txt, for example) in the dialog box that appears.
       Excel will import the data, splitting it into multiple worksheets in order to circumvent Excel's line limit.




    3. Macro for removing un-necessary columns from a worksheet.
The below is the current contents of my worksheet. I want some of the columns out of the below columns.
Let say I want only Emp No, Emp Name, Emp DOB, EMP DOJ, Permanent Address. Except these all
other columns needs to be deleted.




   The below is the Procedure.

       1 Click "Tools," select "Macro" and choose "Macros."
       2 Type a name for your macro in the "Name" field, such as "DeleteUnWantedColumns" and click
       "Create." The Visual Basic Editor will open automatically.
       3 Double-click "(Name) Module" in the "Properties" window and type a name say "DeleteUnWanted"
       4 Click the "+" icon next to "Microsoft Excel Objects."
       5 Double-click "DeleteUnWanted" to open the "Code" window.
       6 Copy and paste the following into the "Code".

              Dim LASTCOL As Integer
              Dim J As Integer
              Dim DELETE_FLAG As Boolean
                LASTCOL = Cells(1, Columns.Count).End(xlToLeft).Column
                For J = LASTCOL To 1 Step -1
                  DELETE_FLAG = True
                  Select Case Cells(1, J)
                     Case "Emp No"
                        DELETE_FLAG = False
                     Case "Emp Name"
                                                                                    Developer: Nihar R Paital
DELETE_FLAG = False
              Case "Emp DOB"
                DELETE_FLAG = False
              Case "EMP DOJ"
                DELETE_FLAG = False
              Case "Permanent Address"
                DELETE_FLAG = False
              End Select
           If (DELETE_FLAG) Then Columns(J).Delete
         Next J

7 Save the Macro.
8 Click "Tools," select "Macro" and choose "Macros."
9 Select the "DeleteUnWantedColumns" macro from the "Macros" dialog box and click "Run."
This will delete all the unwanted columns. And the Output will as below.




                                                                     Developer: Nihar R Paital
4. Formula for finding matching values present in column A and
       Column B.
Follow the table below.
Here first two columns contain data. My requirement is “Which value of column A is present at Column
B.” and “which values of column A are not present at Column B.”




Write the function at C1 as =IF(COUNTIF($B$1:$B$9,A1:A9)=1,A1,"") for finding matching values. And
drag the formula up to C9.

Write the function at D1 as =IF(COUNTIF($B$1:$B$9,A1:A9)=0,A1,"") for finding the non-matching
values. And drag the formula up to D9.

The required Output table is as follows.


         1         7          1
         2         6                       2
         3         5                       3
         4         9          4
         5         1          5
         6        10          6
         7        12          7
         8        15                       8
         9         4          9




                                                                          Developer: Nihar R Paital
5. Formula for showing distinct values in a column.
Follow the table below.
Here first column A contains data. My requirement is “Finding out the distinct values within column A”




Write the function at B2 as =IF(MAX(COUNTIF(A2:A17,A2:A17))>1,"",A2) and drag it through B2 to
B18.
The required Output table is as follows.
Value        Distinct Value
         1
         2
         3
         4
         5            5
         3
        23           23
         3
         2
         1
         3
         4
         3
         1            1
         2            2
         3            3
         4            4
                                                 THANK YOU
                      (PS - Pls leave your ratings/feedback/comments on the main page)


                                                                              Developer: Nihar R Paital

More Related Content

PPTX
Excel macro
PPTX
Learn Excel Macro
PPT
MACROS excel
PPTX
A Quick Simple MS Excel Macro
PPTX
How to apply a formula and macro in excel......by irfan afzal
PDF
Getting started with Microsoft Excel Macros
PDF
Excel macros tutorial
PPTX
Intro macros in Excel 2007
Excel macro
Learn Excel Macro
MACROS excel
A Quick Simple MS Excel Macro
How to apply a formula and macro in excel......by irfan afzal
Getting started with Microsoft Excel Macros
Excel macros tutorial
Intro macros in Excel 2007

What's hot (20)

PPTX
Microsoft Excel - Macros
PPTX
Creating a Microsoft Excel Macro
PPTX
Introduction to Excel VBA/Macros
PPT
Using macros in microsoft excel part 1
PPT
Microsoft Office 2003 Creating Macros
DOCX
Advance MS Excel
PPTX
E learning excel vba programming lesson 2
PPT
E learning excel vba programming lesson 1
PPTX
Vba introduction
PPTX
Vba Class Level 1
PDF
Excel VBA programming basics
PPTX
PDF
Online Advance Excel & VBA Training in India
PPTX
Vba part 1
PPTX
E learning excel vba programming lesson 3
DOCX
Excel vba
PPTX
VBA
PPTX
VBA Classes from Chandoo.org - Course Brochure
PDF
Vba part 1
PPTX
Intro to Excel VBA Programming
Microsoft Excel - Macros
Creating a Microsoft Excel Macro
Introduction to Excel VBA/Macros
Using macros in microsoft excel part 1
Microsoft Office 2003 Creating Macros
Advance MS Excel
E learning excel vba programming lesson 2
E learning excel vba programming lesson 1
Vba introduction
Vba Class Level 1
Excel VBA programming basics
Online Advance Excel & VBA Training in India
Vba part 1
E learning excel vba programming lesson 3
Excel vba
VBA
VBA Classes from Chandoo.org - Course Brochure
Vba part 1
Intro to Excel VBA Programming
Ad

Similar to Useful macros and functions for excel (20)

PDF
Workbench tutorial airfoil
DOC
Excel Vba Basic Tutorial 1
DOCX
Tutorials on Macro
PPT
Tolerance Stackups Using Oracle Crystal Ball
PDF
Cc code cards
DOCX
OBIEE 11g : Repository Creation Steps
DOCX
PT1420 File Access and Visual Basic .docx
DOC
( 13 ) Office 2007 Coding With Excel And Excel Services
PPT
Spreadsheet Analytical Tools
PPTX
Learn VBA Training & Advance Excel Courses in Delhi
PPT
Programming For As Comp
PPT
Programming For As Comp
DOCX
Visual basic bt0082
DOCX
Hw8Excel - Exercise 8 Mail Merge-2.docINFS 3250In Class Pro.docx
PDF
c++ lab manual
DOCX
The program reads data from two files, itemsList-0x.txt and .docx
PPTX
Elementary Data Analysis with MS Excel_Day-4
DOCX
Ecs 10 programming assignment 4 loopapalooza
PDF
Curve fitting
Workbench tutorial airfoil
Excel Vba Basic Tutorial 1
Tutorials on Macro
Tolerance Stackups Using Oracle Crystal Ball
Cc code cards
OBIEE 11g : Repository Creation Steps
PT1420 File Access and Visual Basic .docx
( 13 ) Office 2007 Coding With Excel And Excel Services
Spreadsheet Analytical Tools
Learn VBA Training & Advance Excel Courses in Delhi
Programming For As Comp
Programming For As Comp
Visual basic bt0082
Hw8Excel - Exercise 8 Mail Merge-2.docINFS 3250In Class Pro.docx
c++ lab manual
The program reads data from two files, itemsList-0x.txt and .docx
Elementary Data Analysis with MS Excel_Day-4
Ecs 10 programming assignment 4 loopapalooza
Curve fitting
Ad

More from Nihar Ranjan Paital (11)

PPS
Oracle Select Query
PPS
Unix - Class7 - awk
PPS
UNIX - Class5 - Advance Shell Scripting-P2
PPS
UNIX - Class3 - Programming Constructs
PPS
UNIX - Class2 - vi Editor
PPS
UNIX - Class1 - Basic Shell
PPS
UNIX - Class6 - sed - Detail
PPS
UNIX - Class4 - Advance Shell Scripting-P1
PDF
PPT
Csql for telecom
PPT
Select Operations in CSQL
Oracle Select Query
Unix - Class7 - awk
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class3 - Programming Constructs
UNIX - Class2 - vi Editor
UNIX - Class1 - Basic Shell
UNIX - Class6 - sed - Detail
UNIX - Class4 - Advance Shell Scripting-P1
Csql for telecom
Select Operations in CSQL

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Tartificialntelligence_presentation.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPT
Teaching material agriculture food technology
PDF
Electronic commerce courselecture one. Pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
cuic standard and advanced reporting.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Tartificialntelligence_presentation.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Teaching material agriculture food technology
Electronic commerce courselecture one. Pdf
Digital-Transformation-Roadmap-for-Companies.pptx
MYSQL Presentation for SQL database connectivity
Encapsulation_ Review paper, used for researhc scholars
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”
cuic standard and advanced reporting.pdf
Group 1 Presentation -Planning and Decision Making .pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Unlocking AI with Model Context Protocol (MCP)
MIND Revenue Release Quarter 2 2025 Press Release
20250228 LYD VKU AI Blended-Learning.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Empathic Computing: Creating Shared Understanding
Agricultural_Statistics_at_a_Glance_2022_0.pdf

Useful macros and functions for excel

  • 1. Useful Macros and functions for Excel This knowledge asset has information pertaining to Excel Macros and useful Functions. This document will let you know about some useful macros and functions, which are frequently used by the users. Topics Covered: 1. Macro for Deleting Blank columns from a worksheet................................................................................ 1 2. Macro uploading rows more than 65536. ................................................................................................... 2 3. Macro for removing un-necessary columns from a worksheet................................................................... 3 4. Formula for finding matching values present in column A and Column B................................................ 5 5. Formula for showing distinct values in a column. ...................................................................................... 6 Developer: Nihar R Paital
  • 2. 1. Macro for Deleting Blank columns from a worksheet. We need to delete the column B, E as they are blank. The below is the Procedure. 1 Click "Tools," select "Macro" and choose "Macros." 2 Type a name for your macro in the "Name" field, such as "DeleteBlankColumns" and click "Create." The Visual Basic Editor will open automatically. 3 Double-click "(Name) Module" in the "Properties" window and type a name say "DeleteBlankCol" 4 Click the "+" icon next to "Microsoft Excel Objects." 5 Double-click "DeleteBlankCol" to open the "Code" window. 6 Copy and paste the following into the "Code". Dim Col As Long, ColCnt As Long, Rng As Range Application.ScreenUpdating = False Application.Calculation = xlCalculationManual On Error GoTo Exits: If Selection.Columns.Count > 1 Then Set Rng = Selection Else Set Rng = Range(Columns(1), Columns(ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column())) End If ColCnt = 0 For Col = Rng.Columns.Count To 1 Step -1 If Application.WorksheetFunction.CountA(Rng.Columns(Col).EntireColumn) = 0 Then Rng.Columns(Col).EntireColumn.Delete ColCnt = ColCnt + 1 End If Next Col Exits: Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic 7 Save the Macro. 8 Click "Tools," select "Macro" and choose "Macros." 9 Select the "DeleteBlankColumns" macro from the "Macros" dialog box and click "Run." This will delete all the blank columns. Like B and E. Developer: Nihar R Paital
  • 3. 2. Macro uploading rows more than 65536. 1 Create a text file say myhugedocument.txt having number of lines more than 65536. 2 Click "Tools," select "Macro" and choose "Macros." 3 Type a name for your macro in the "Name" field, such as "ImportFile" and click "Create." The Visual Basic Editor will open automatically. 4 Double-click "(Name) Module" in the "Properties" window and type a name say "FileImport" 5 Click the "+" icon next to "Microsoft Excel Objects." 6 Double-click "FileImport" to open the "Code" window. 7 Copy and paste the following into the "Code". 'Dimension Variables Dim ResultStr As String Dim FileName As String Dim FileNum As Integer Dim Counter As Double 'Ask User for File's Name FileName = InputBox("Please enter the Text File's name, e.g. test.txt") 'Check for no entry If FileName = "" Then End 'Get Next Available File Handle Number FileNum = FreeFile() 'Open Text File For Input Open FileName For Input As #FileNum 'Turn Screen Updating Off Application.ScreenUpdating = False 'Create A New WorkBook With One Worksheet In It Workbooks.Add template:=xlWorksheet 'Set The Counter to 1 Counter = 1 'Loop Until the End Of File Is Reached Do While Seek(FileNum) <= LOF(FileNum) 'Display Importing Row Number On Status Bar Application.StatusBar = "Importing Row " & _ Counter & " of text file " & FileName 'Store One Line Of Text From File To Variable Line Input #FileNum, ResultStr 'Store Variable Data Into Active Cell If Left(ResultStr, 1) = "=" Then ActiveCell.Value = "'" & ResultStr Else ActiveCell.Value = ResultStr End If 'For Excel versions before Excel 97, change 65536 to 16384 If ActiveCell.Row = 65536 Then 'If On The Last Row Then Add A New Sheet ActiveWorkbook.Sheets.Add Else 'If Not The Last Row Then Go One Cell Down ActiveCell.Offset(1, 0).Select End If 'Increment the Counter By 1 Counter = Counter + 1 'Start Again At Top Of 'Do While' Statement Loop 'Close The Open Text File Developer: Nihar R Paital
  • 4. Close 'Remove Message From Status Bar Application.StatusBar = False 8 Click "File" and select "Close" to close the Visual Basic Editor. 9 Click "Tools," select "Macro" and choose "Macros." 10 Select the "ImportFile" macro from the "Macros" dialog box and click "Run." 11 Enter the name of your file (myhugedocument.txt, for example) in the dialog box that appears. Excel will import the data, splitting it into multiple worksheets in order to circumvent Excel's line limit. 3. Macro for removing un-necessary columns from a worksheet. The below is the current contents of my worksheet. I want some of the columns out of the below columns. Let say I want only Emp No, Emp Name, Emp DOB, EMP DOJ, Permanent Address. Except these all other columns needs to be deleted. The below is the Procedure. 1 Click "Tools," select "Macro" and choose "Macros." 2 Type a name for your macro in the "Name" field, such as "DeleteUnWantedColumns" and click "Create." The Visual Basic Editor will open automatically. 3 Double-click "(Name) Module" in the "Properties" window and type a name say "DeleteUnWanted" 4 Click the "+" icon next to "Microsoft Excel Objects." 5 Double-click "DeleteUnWanted" to open the "Code" window. 6 Copy and paste the following into the "Code". Dim LASTCOL As Integer Dim J As Integer Dim DELETE_FLAG As Boolean LASTCOL = Cells(1, Columns.Count).End(xlToLeft).Column For J = LASTCOL To 1 Step -1 DELETE_FLAG = True Select Case Cells(1, J) Case "Emp No" DELETE_FLAG = False Case "Emp Name" Developer: Nihar R Paital
  • 5. DELETE_FLAG = False Case "Emp DOB" DELETE_FLAG = False Case "EMP DOJ" DELETE_FLAG = False Case "Permanent Address" DELETE_FLAG = False End Select If (DELETE_FLAG) Then Columns(J).Delete Next J 7 Save the Macro. 8 Click "Tools," select "Macro" and choose "Macros." 9 Select the "DeleteUnWantedColumns" macro from the "Macros" dialog box and click "Run." This will delete all the unwanted columns. And the Output will as below. Developer: Nihar R Paital
  • 6. 4. Formula for finding matching values present in column A and Column B. Follow the table below. Here first two columns contain data. My requirement is “Which value of column A is present at Column B.” and “which values of column A are not present at Column B.” Write the function at C1 as =IF(COUNTIF($B$1:$B$9,A1:A9)=1,A1,"") for finding matching values. And drag the formula up to C9. Write the function at D1 as =IF(COUNTIF($B$1:$B$9,A1:A9)=0,A1,"") for finding the non-matching values. And drag the formula up to D9. The required Output table is as follows. 1 7 1 2 6 2 3 5 3 4 9 4 5 1 5 6 10 6 7 12 7 8 15 8 9 4 9 Developer: Nihar R Paital
  • 7. 5. Formula for showing distinct values in a column. Follow the table below. Here first column A contains data. My requirement is “Finding out the distinct values within column A” Write the function at B2 as =IF(MAX(COUNTIF(A2:A17,A2:A17))>1,"",A2) and drag it through B2 to B18. The required Output table is as follows. Value Distinct Value 1 2 3 4 5 5 3 23 23 3 2 1 3 4 3 1 1 2 2 3 3 4 4 THANK YOU (PS - Pls leave your ratings/feedback/comments on the main page) Developer: Nihar R Paital