Blogs
XLA routines: EE_ExportSheetToXLS
Submitted by Nick on 31 January, 2015 - 08:18EE_ExportSheetToXLS exports a worksheet to an XLS
Function EE_ExportSheetToXLS(strSheetName As String, strFilePath As String) As Boolean '- takes a sheet name '- takes a FullFilePath '- creates new wb '- delete existing file '- sheet.copy (new wb) '- saveas (FilePath) '- close '- returns True if success Dim wbkNew As Workbook Dim strNewFullFilePath As String 'http://excelexperts.com/xla-routines-eeExportSheetToXLS for updates on this function ThisWorkbook.Worksheets(strSheetName).Copy Set wbkNew = ActiveWorkbook strNewFu
»
- Nick's blog
- Login or register to post comments
- Read more
- 2735 reads
XLA routines: EE_DateFileName
Submitted by Nick on 31 January, 2015 - 08:18EE_DateFileName if a function that creates a date / time stamped file name in yyyymmdd format so that it can be sorted in Explorer
Function EE_DateFileName(strFileName As String, Optional varFileDate As Variant, Optional blnAddTime As Boolean) As String '- takes a file name ' 'Optional: ' FileDate As Variant ' AddTime As Boolean ' '- if date is missing, it appends today's date formatted as: '_yyyymmdd' '- if AddTime is true, add 'hhmmss' of Now() ' '- returns file name with appended date stamping Dim strNewFileName As String 'http://excelexperts.com/xla-routines-eeDa
»
- Nick's blog
- Login or register to post comments
- Read more
- 3615 reads
XLA routines: EE_ExportRangeToCSV
Submitted by Nick on 31 January, 2015 - 08:17EE_ExportRangeToCSV creates a csv file from a range of data, and replaces errors.
Function EE_ExportRangeToCSV(strCSVfileName As String, rngExport As range, Optional blnDispMsg As Boolean = False) As Boolean '-Takes a csv file name '-deletes existing '- converts all dates or times to longs - not implemented '- exports CSV ' - if the cell contains an error (eg: #Value), it writes 'ERROR' '- restores date time formatting - not implemented '- returns True if success Dim wbkCSV As Workbook Dim strCSVfullFilePath As String 'http://excelexperts.com/xl
»
- Nick's blog
- Login or register to post comments
- Read more
- 3232 reads
XLA routines: EE_CheckPaths
Submitted by Nick on 31 January, 2015 - 08:16EE_CheckPaths takes a range containing file paths, and checks to see if they all exist.
- very useful at the start of a routine that depends on files existing.
- check the paths and exit if they don't exist
Function EE_CheckPaths(rngPaths As range, Optional blnDispMissingMsg As Boolean = True) As Boolean '- takes a range of cells containing file paths / folders. '- checks if they exist '- returns false if any 1 doesn't exist '- displays messagebox with list of missing paths Dim strMissingPaths As String Dim rngEachPath As range 'http://excelexperts.com/xla-routines-
»
- Nick's blog
- Login or register to post comments
- Read more
- 2807 reads
XLA routines: EE_DeleteFile
Submitted by Nick on 31 January, 2015 - 08:15EE_DeleteFile deletes a file if it exists.
Sub EE_DeleteFile(strFilePath As String, Optional blnShowMsg As Boolean = False) '- takes file path '- deletes file '- does not error if file does not exist '- does error if file is locked 'http://excelexperts.com/xla-routines-eeDeleteFile for updates on this sub routine With CreateObject("Scripting.FileSystemObject") If .FileExists(strFilePath) Then On Error Resume Next .deletefile strFilePath If Err.Number <> 0 And blnShowMsg = True Then MsgBox Err.D
»
- Nick's blog
- Login or register to post comments
- Read more
- 2739 reads
XLA routines: EE_Copy
Submitted by Nick on 31 January, 2015 - 08:14EE_Copy is one of the most used EE sub routines. Specify the source, and the top left of the target, and EE_Copy will copy the data over.
»
- Nick's blog
- Login or register to post comments
- Read more
- 3057 reads
XLA routines: EE_TableRemoveHeadings
Submitted by Nick on 31 January, 2015 - 08:13EE_TableRemoveHeadings is a function that takes a table of data + headings and returns only the data
Function EE_TableRemoveHeadings(rngTable As range) As range '- Takes an EE_table range '- removes the headings '- returns a range 'http://excelexperts.com/xla-routines-eeTableRemoveHeadings for updates on this function Set EE_TableRemoveHeadings = Intersect(rngTable, rngTable.Offset(1)) End Function
»
- Nick's blog
- Login or register to post comments
- 2699 reads
XLA routines: EE_TableFirstRowRange
Submitted by Nick on 31 January, 2015 - 08:13EE_TableFirstRowRange is a simple function to return the first row of a table of data (the Headings)
Function EE_TableFirstRowRange(rngTable As range) As range '- Takes an EE_Table Range '- returns a range around the 1st Row in the table 'http://excelexperts.com/xla-routines-eeTableFirstRowRange for updates on this function Set EE_TableFirstRowRange = rngTable.Rows(1) End Function
»
- Nick's blog
- Login or register to post comments
- 2518 reads
XLA routines: EE_Find
Submitted by Nick on 31 January, 2015 - 08:12The problem with Excel's "Find" routine is that it does not reset the Find criteria, so that when you use CTRL+F on the worksheet, you have to reset all the params.
EE_Find gets around this by resetting the criteria.
Returns a range object or nothing
Function EE_Find(strFind As String, rngRangeToFindIn As range) As range '- takes a string, RangeToLookIn '- returns a range of the first cell containing the string '- uses .Find method, and looks for exact match, in whole cell '- returns the 'exact match' checkbox back to unchecked 'http://excelexperts.com/xla-routines-eeFind
»
- Nick's blog
- Login or register to post comments
- Read more
- 3155 reads
XLA routines: EE_TableHeadingCol
Submitted by Nick on 31 January, 2015 - 08:10EE_TableHeadingCol is a function that returns the column of a heading...
»
- Nick's blog
- Login or register to post comments
- Read more
- 2523 reads
XLA routines: EE_FilterAndRemove
Submitted by Nick on 31 January, 2015 - 08:10EE_FilterAndRemove is a hugely useful VBA routine that uses sorting to remove large amounts of data from a table.
Specify your table of data, the heading name, and the criteria.
- designed to work well with vast amounts of data
Sub EE_FilterAndRemove(rngTable As range, strHeading As String, strCriteria As String) Dim rngTblData As range Dim rngSortData As range Dim intHeadCol As Integer 'http://excelexperts.com/xla-routines-eeFilterAndRemove for updates on this sub routine Set rngTblData = Intersect(rngTable, rngTable.Offset(1)) intHe
»
- Nick's blog
- Login or register to post comments
- Read more
- 3122 reads
XLA routines: EE_HideSheets
Submitted by Nick on 31 January, 2015 - 08:09EE_HideSheets is a sub routine that hides any sheets that are not in the range of sheet names specified, or hides everything in the range if blnReverseSelection is true
Sub EE_HideSheets(ArrayOrRange, Optional blnReverseSelection As Boolean = False) '> - takes an array or range '> - Hide any sheets that are/not contained in the array\range Dim arr Dim intWksCount As Integer Dim blnDelete As Boolean Dim blnDelete2 As Boolean Dim wbk As Workbook Dim intShtAdded As Integer Dim intShtAdded2 As I
»
- Nick's blog
- Login or register to post comments
- Read more
- 3037 reads
XLA routines: EE_ColorSheetTabs
Submitted by Nick on 31 January, 2015 - 08:08EE_ColorSheetTabs is an easy sub that takes a range of coloured cells with sheet tab names in it.
- colours the sheet tabs in accordance with the cell colour.
Sub EE_ColorSheetTabs(rngColor As range) Dim rngEach As range Dim wbk As Workbook 'http://excelexperts.com/xla-routines-eeColorSheetTabs for updates on this sub routine Set wbk = ThisWorkbook For Each rngEach In rngColor On Error Resume Next wbk.Worksheets(rngEach.value).Tab.Color = rngEach.Interior.Color Err.Clear: On Error GoTo 0: On Error GoTo -1
»
- Nick's blog
- Login or register to post comments
- Read more
- 3450 reads
XLA routines: EE_SortTable
Submitted by Nick on 31 January, 2015 - 08:07Sub routine that sorts a table using Excel 2007+ code.
Needs to be extended to work with heading 2 and 3
Sub EE_SortTable(blnAscending As Boolean, wks As Worksheet, strFieldHeading As String) '> - takes string of sheet name or wosksheet object, Heading1 string, '> Ascending1 boolean...
»
- Nick's blog
- Login or register to post comments
- Read more
- 2864 reads
XLA routines: EE_AddCalculatedColumn
Submitted by Nick on 25 January, 2015 - 18:13EE_AddCalculatedColumn is a hugely useful sub routine that adds a calculated column to your data set, and names it.
- this saves writing the VBA to add a calculated column from your data set.
Uses EE_GetLastPopulatedCell to find the last cell of the data
Sub EE_AddCalculatedColumn(rngColumn As range, strFormula As String, strNewHeading As String, Optional InChunksOf As Long) Dim rng As range With rngColumn Set rng = range(.Cells(2), .Cells(EE_GetLastPopulatedCell(rngColumn.Parent).
»
- Nick's blog
- Login or register to post comments
- Read more
- 2996 reads
XLA routines: EE_CloseOtherWorkbooks
Submitted by Nick on 25 January, 2015 - 18:07Routine to close other workbooks
- this saves setting variables and tracking open workbooks
Sub EE_CloseOtherWorkbooks(wbKeepOpen As Workbook) 'http://excelexperts.com/xla-routines-eeCloseOtherWorkbooks for updates on this sub routine ' closes workbooks other than the workbook containing the code and another one specified Dim wbk As Workbook On Error Resume Next For Each wbk In Application.Workbooks If wbk.Name <> ThisWorkbook.Name Then If Not wbKeepOpen Is Nothing Then If wbk.Name <> wbKeepOpen.Name Then
»
- Nick's blog
- Login or register to post comments
- Read more
- 2926 reads
XLA routines: EE_FileNameFromFilePath
Submitted by Nick on 25 January, 2015 - 18:04Returns the file name from the file path
Function EE_FileNameFromFilePath(strFilePath As String) As String 'http://excelexperts.com/xla-routines-eeFileNameFromFilePath EE_FileNameFromFilePath = Mid(strFilePath, InStrRev(strFilePath, Application.PathSeparator) + 1) End Function
»
- Nick's blog
- Login or register to post comments
- 2744 reads
XLA routines: EE_HeadersCorrect
Submitted by Nick on 25 January, 2015 - 18:01Often others deliver files to an application that need to have fixed col headers. Unfortunately, they often change them without telling you.
»
- Nick's blog
- Login or register to post comments
- Read more
- 2761 reads
XLA routines: EE_ApplyPivotDataFormatting
Submitted by Nick on 25 January, 2015 - 17:57Takes a pivot table, and formats the pivot field based on a 2 col range
Sub EE_ApplyPivotDataFormatting(pt As PivotTable, FormattingRange As range) '- takes a 2 col range, and applies the formatting specified in col 2 to the Data fields. Dim rngCell As range 'http://excelexperts.com/xla-routines-eeapplypivotdataformatting If FormattingRange.Rows.Count <> 2 Then Exit Sub For Each rngCell In FormattingRange.Rows pt.PivotFields(rngCell.Cells(, 1).value).NumberFormat = rngCell.Cells(, 2).value Next rngCell Set rngCell = Nothing End Sub
»
- Nick's blog
- 2 comments
- Read more
- 2987 reads
XLA routines: EE_SortTable2003Comp
Submitted by Nick on 25 January, 2015 - 17:55Sort data using VBA in Excel.
»
- Nick's blog
- Login or register to post comments
- Read more
- 3058 reads
XLA routines: EE_GetLastPopulatedCell
Submitted by Nick on 25 January, 2015 - 11:17Finds the last populated cell on a worksheet, or an empty cell representing the max row and max col.
Function EE_GetLastPopulatedCell(Optional wks As Worksheet) As Range '- Works how specialcells (lastCell) SHOULD work '- Returns single cell range Dim lngCol As Long Dim lngMaxRow As Long Dim lngRow As Long Dim lngMaxCol As Long 'http://excelexperts.com/xla-routines-eeGetLastPopulatedCell for updates on this function If wks Is Nothing Then Set wks = ActiveSheet End If If wks.UsedRange.Rows
»
- Nick's blog
- Login or register to post comments
- Read more
- 3983 reads
XLA routines: EE_Table
Submitted by Nick on 25 January, 2015 - 11:15If you use Excel as a database, EE_Table will return the range object of your data.
»
- Nick's blog
- Login or register to post comments
- Read more
- 3052 reads
XLA routines: EE_DeleteTempSheets
Submitted by Nick on 25 January, 2015 - 11:11EE_DeleteTempSheets is used when your routine creates temp sheets that need to be deleted at the end of the routine.
»
- Nick's blog
- Login or register to post comments
- Read more
- 2688 reads
XLA routines: EE_RearrangeSheetsAlphabetic
Submitted by Nick on 25 January, 2015 - 11:08EE_RearrangeSheetsAlphabetic is useful to use at the end of your procedure when you have created sheets all over the place and want them rearraged alphabetically.
»
- Nick's blog
- Login or register to post comments
- Read more
- 2888 reads
XLA Routines: EE_ReplaceSheet
Submitted by Nick on 25 January, 2015 - 11:02EE_ReplaceSheet is a useful routine that replaces a sheet, and does not error if the sheet does not exist meaning you can use the routine to create a new sheet.
Function EE_ReplaceSheet(strSheet As String) As Worksheet Dim wksNew As Worksheet Dim wbk As Workbook Dim blnDisplayAlerts As Boolean Set wbk = ActiveWorkbook On Error Resume Next Set wksNew = wbk.Worksheets.Add(after:=wbk.Worksheets(strSheet)) Err.Clear: On Error GoTo 0: On Error GoTo -1 If wksNew Is Nothing Then Set wksNe
»
- Nick's blog
- Login or register to post comments
- Read more
- 2650 reads
XLA Routines: EE_DeleteSheets
Submitted by Nick on 25 January, 2015 - 10:57Following on from EE_DeleteSheet, here's a routine that deletes multiple sheets
Sub EE_DeleteSheets(ArrayOrRange) Dim blnDisplayAlerts As Boolean Dim arr Dim wbk As Workbook 'http://excelexperts.com/xla-routines-eedeletesheets Set wbk = ActiveWorkbook Select Case TypeName(ArrayOrRange) Case "Variant()", "String" arr = ArrayOrRange Case "Range" arr = Application.Transpose(ArrayOrRange.SpecialCells(xlCellTypeConstants))
»
- Nick's blog
- Login or register to post comments
- Read more
- 2737 reads
XLA routines: EE_DeleteSheet
Submitted by Nick on 25 January, 2015 - 10:54Deleting a sheet using VBA is more work than it should be.
»
- Nick's blog
- Login or register to post comments
- Read more
- 2780 reads
XLA routines: EE_End
Submitted by Nick on 25 January, 2015 - 10:49At the end of your code, it's good practise to turn events back on, enable alerts, and put calculation back to where it was.
Here's the code to turn them on:
See here to turn them off at the start of your code:
EE_Start
Public lngCalc As Long Sub EE_End() 'http://excelexperts.com/xla-routines-eeend Application.EnableEvents = True Application.DisplayAlerts = True Application.Calculation = lngCalc End Sub
»
- Nick's blog
- Login or register to post comments
- 3186 reads
XLA routines: EE_Start
Submitted by Nick on 25 January, 2015 - 10:41At the start of your code, it's good practise to turn off events, disable alerts, and put calculation on manual.
At the end of your routine, turn them back on.
Here's the code to turn them off:
See here to turn them back on:
EE_End
Public lngCalc As Long Sub EE_Start() 'http://excelexperts.com/xla-routines-eestart Application.EnableEvents = False Application.DisplayAlerts = False lngCalc = Application.Calculation Application.Calculation = xlCalculationManual End Sub
»
- Nick's blog
- Login or register to post comments
- 4068 reads
Create Database in excel and ms access
Submitted by venkatr8529 on 13 December, 2014 - 17:15Hi Team,
Please help me. I am working in vba last two years and now i got some development tool for my friend. He asked me to create employee database application in excel. so i decide to create in excel user form and database with ms access.
can store huge data in msaccess and excel form will be reliable. help me.
Regards,
Venkat R
»
- venkatr8529's blog
- Login or register to post comments
- 3123 reads
Recent comments
5 years 36 weeks ago
6 years 22 weeks ago
6 years 34 weeks ago
6 years 37 weeks ago
6 years 38 weeks ago
6 years 43 weeks ago
6 years 52 weeks ago
7 years 2 days ago
7 years 3 days ago
7 years 3 days ago