How to Get a Full File Path in Excel?

Ever needed to find out the full path of your current file? What I mean by full path here is the full location of the active file including the directory path and full file name.

If you did, then what you need is this following code:

Application.ActiveWorkbook.FullName

The one line code will return the full directory path + filename of the active file, ex:
D:\My Directory\filename.xls

To simply use it from our formula bar, we can wrap the code into excel vba function like this:

Function FULLFILENAME()

     FULLFILENAME = Application.ActiveWorkbook.FullName

End Function

Every time we needed the information, just call the FULLFILENAME function from the formula bar.


Remove Non-AlphaNumeric Characters from String

The function below will allow us to remove a non alphanumeric character from an input string.

Be careful though, lengther the string, more time needed to evaluate each string on the sentence.

To bad that I don’t have any idea on how to use Regular Expression in Excel VBA, if we can use regex to replace all the unwanted character directly, it would be great :)

Function GETALPHANUMERIC(text)

str_all = "abcdefghijklmnopqrstuvwxyz1234567890"
For lenstr = 1 To Len(text)
    If InStr(str_all, LCase(Mid(text, lenstr, 1))) Then
        GETALPHANUMERIC = GETALPHANUMERIC & Mid(text, lenstr, 1)
    End If
Next

End Function

If somehow what we need is only the apha part without the numeric part or vice versa, simply adjusting the value of str_all variable will solve it.


Sum Unique/Distinct Values in Excel

Usually in the sequential database system, we can SUM only unique values in table column by adding all the values from SELECT DISTINCT query result only.

Based on the same principle, we can also create a simple VBA function in Microsoft Excel by adding the values only available in a collection of unique values we have created before hand.

Public Function DISTINCTSUM(Rg As range)
    Dim rCell As range
    Dim cCells As New Collection
    Dim vValue As Variant

    ' create a unique no duplicate value collection
    For Each rCell In Rg
        On Error Resume Next
        cCells.Add rCell.Value, CStr(rCell.Value)
    Next rCell

    ' sum all the data in previous collection
    For Each vValue In cCells
        DISTINCTSUM = vValue + DISTINCTSUM
    Next vValue

    Set cCells = Nothing
End Function

In here, first we create a Collection object that contains unique values only. Only then will we loop throught the collection and add up all the values stored there to get the DISTINCT SUM value.

Now we can call the above DISCTINCTSUM formula directly from Excel formula bar to sum only the unique or disctinct values in some range of cells, with the cell range as parameter.

=distinctsum(D4:D8)

Will sum all the unique values in range of D4 until D8. This is really helpful if we have a lot of duplicate data that we don’t want to count.

The other way around to get the sum of unique/distinct values in Excel is using Pivot Table.

Create a Pivot Table, then drop the column containing duplicate values in the Row fields area. Excel will then automatically filtered all the values and remove any duplicate values in that column. Later on, simply SUM all the data in the row fields to get the distinct sum.

The final result will be the same, but we don’t want to create a Pivot Table for every column we wanna sum, right?

Another way to get a list of unique values is to use the Advanced Filter feature. In advanced filtering options, check the option Copy to another location, and Unique records only option. Then select the cell where we want to put the unique values.

Easier to do compare to Pivot Table, but we still don’t have the flexibility of the previous DISTINCTSUM macro.


Move Cursor To One Cell Below Last Row With Data

Ever want to paste some data into new empty cell after the last cell with data in certain column? Then take a look at this simple macro.

    'first, select cell in the first row of that column, like A1, K1, etc
    Range("A1").Select

    'move to the last cell with data
    Selection.End(xlDown).Select

    'move to one row below it
    ActiveCell.Offset(1, 0).Select

    'paste the copied data in there
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

Simple isn’t it? Simple code, but will help us a lot each time we wanna move the active cell into one cell below the last cell contained any kind of data, in the same column.

One requirement needed to use this simple excel vba macro, first we need to select the first cell in the same column where we wanna do the selection.


Finding Cell with Minimum/Maximum Value in Active Worksheet

Let say we want to find position of cell containing the minimum/maximum value in current/active Excel worksheet, and then after we found the cell, we will change the cell format to make it stand out before other cells.

The logic is simple, we just need to use Excel MIN function to find the minimum/maximum value on the worksheet, and then using Excel FIND function we will find which cell contain that minimum/ maximum value.

Excel VBA macro implementation of the algorithm above will look like below, change code Application.Min(oRg) into Application.Max(oRg) to find the maximum value instead of minimum value.

Sub FindMinValue()

    Dim oRg As Range, iMin As Variant

    Set oRg = Cells
    'Finding the minimum value
    'change Application.Min(oRg) into Application.Max(oRg) to find the maximum value
    iMin = Application.Min(oRg)

    'Select cell containing the min value
    oRg.Find(What:=iMin, _
        After:=oRg.Range("A1"), _
        LookIn:=xlValues, _
        LookAt:=xlPart, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False _
        ).Select

    'Change selected cell format
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent3
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

    'Displaying min value info
    With Selection
        MsgBox "Min value : " & iMin & vbCrLf & _
        "Cell position " & vbCrLf & _
        "Row : " & .Row & vbCrLf & _
        "Column : " & .Column
    End With

End Sub

As an additional info, the code above will display the cell info, row and column position of the cell containing the minimum/maximum value.


How To Open Excel File Using Macro

Imagine this condition; We are in the middle of doing something using Excel VBA macro, then we want the macro to automatically show the File Open dialog box to make us (user) able to choose another Excel file to be opened by Excel.

Got the picture? Below is vba procedure that do exactly like that, show the File Open dialog box, and open the Excel file selected by user.

Sub OpenExcelFile()

    Dim vFile As Variant

    'Showing Excel Open Dialog Form
    vFile = Application.GetOpenFilename("Excel Files (*.xl*)," & _
    "*.xl*", 1, "Select Excel File", "Open", False)

    'If Cancel then exit
    If TypeName(vFile) = "Boolean" Then
        Exit Sub
    End If

    'Open selected file
    Workbooks.Open vFile

End Sub

Changing Microsoft Excel Status Bar

Maybe for some reason we want to change our Microsoft Excel Status Bar, then this simple vba function will do the work for us.

The end result will look like this:

changing microsoft excel status bar

Ok, to do it, put this following code in ThisWorkbook code

Private Sub Workbook_Open()
    'when workbook opened, change the status bar
    Application.StatusBar = "Changing Excel Status Bar | excelvbamacro.com"
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    'remove status bar info before workbook closed
    Application.StatusBar = False
End Sub

Now we can brag to our friends about our Excel status bar when we opened our Excel Workbook ;) )