Useful Macros and functions for Excel        This knowledge asset has information pertaining to Excel Macros and useful Fu...
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 ...
2. Macro uploading rows more than 65536. 1 Create a text file say myhugedocument.txt having number of lines more than 6553...
Close              Remove Message From Status Bar              Application.StatusBar = False       8 Click "File" and sele...
DELETE_FLAG = False              Case "Emp DOB"                DELETE_FLAG = False              Case "EMP DOJ"            ...
4. Formula for finding matching values present in column A and       Column B.Follow the table below.Here first two column...
5. Formula for showing distinct values in a column.Follow the table below.Here first column A contains data. My requiremen...
Upcoming SlideShare
Loading in …5
×

Useful macros and functions for excel

14,198 views

Published on

This will help us to

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
14,198
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
179
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Useful macros and functions for excel

  1. 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 frequentlyused by the users.Topics Covered:1. Macro for Deleting Blank columns from a worksheet................................................................................ 12. Macro uploading rows more than 65536. ................................................................................................... 23. Macro for removing un-necessary columns from a worksheet................................................................... 34. Formula for finding matching values present in column A and Column B................................................ 55. Formula for showing distinct values in a column. ...................................................................................... 6 Developer: Nihar R Paital
  2. 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. 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 Files Name FileName = InputBox("Please enter the Text Files 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. 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 Excels 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 allother 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. 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 J7 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. 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 ColumnB.” 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. Anddrag 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-matchingvalues. 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. 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 toB18.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

×