2008 November | Automate Excel

Automate Excel

Nov 5

The following routine will sort the sheets in a workbook alphabetically. The flag “Sort_Mode_Descending” can be switched between descending and ascending as required. The routine is case INSENSITIVE.

Sub Sort_Sheets()
Dim Sort_Mode_Descending As Boolean
Dim No_of_Sheets As Integer
Dim Outer_Loop As Integer
Dim Inner_Loop As Integer
No_of_Sheets = Sheets.Count
'Change Flag As appropriate
Sort_Mode_Descending = False
For Outer_Loop = 1 To No_of_Sheets
        For Inner_Loop = 1 To Outer_Loop
               If Sort_Mode_Descending = True Then
                If UCase(Sheets(Outer_Loop).Name) > UCase(Sheets(Inner_Loop).Name) Then
                                Sheets(Outer_Loop).Move Before:=Sheets(Inner_Loop)
                End If
           End If
        If Sort_Mode_Descending = False Then
            If UCase(Sheets(Outer_Loop).Name) < UCase(Sheets(Inner_Loop).Name) Then
                        Sheets(Outer_Loop).Move Before:=Sheets(Inner_Loop)
            End If
          End If

        Next Inner_Loop
            Next Outer_Loop

To download the .XLSM file from this article, click here.

Nov 4

The standard Excel “Column” Function returns the number rather than the letter of the column e.g:

Column(E4) – returns the number 5 rather than the letter E
Column(AD12) returns the number 30 rather than AD.

The following function returns the letter rather than the number of the column. So in the above two examples we have the letters E and AD respectively . The routine will return blank if more than a single cell is referenced:

Function Alpha_Column(Cell_Add As Range) As String
Dim No_of_Rows As Integer
Dim No_of_Cols As Integer
Dim Num_Column As Integer
No_of_Rows = Cell_Add.Rows.Count
No_of_Cols = Cell_Add.Columns.Count
If ((No_of_Rows <> 1) Or (No_of_Cols <> 1)) Then
    Alpha_Column = ""
    Exit Function
End If
 Num_Column = Cell_Add.Column
If Num_Column < 26 Then
    Alpha_Column = Chr(64 + Num_Column)
Else

    Alpha_Column = Chr(Int(Num_Column / 26) + 64) & Chr((Num_Column Mod 26) + 64)
End If
End Function

To download the .XLSM file from this article, click here.

Nov 3

The following function will extract the numerical portion from a string:

Function Extract_Number_from_Text(Phrase As String) As Double
Dim Length_of_String As Integer
Dim Current_Pos As Integer
Dim Temp As String
Length_of_String = Len(Phrase)
Temp = ""
For Current_Pos = 1 To Length_of_String
If (Mid(Phrase, Current_Pos, 1) = "-") Then
  Temp = Temp & Mid(Phrase, Current_Pos, 1)
End If
If (Mid(Phrase, Current_Pos, 1) = ".") Then
 Temp = Temp & Mid(Phrase, Current_Pos, 1)
End If
If (IsNumeric(Mid(Phrase, Current_Pos, 1))) = True Then
    Temp = Temp & Mid(Phrase, Current_Pos, 1)
 End If
Next Current_Pos
If Len(Temp) = 0 Then
    Extract_Number_from_Text = 0
Else
    Extract_Number_from_Text = CDbl(Temp)
End If
End Function

So for example:

Extract_Number_from_Text(“uuigguo 995”) will return 995
Extract_Number_from_Text(“uuigguo 0.12995”) will return 0.12995
Extract_Number_from_Text(“yu00.000456”) will return 0.000456
=Extract_Number_from_Text(”juii-0009.9987iihiii”) will return -0.9987
If there is no number in the phrase then a zero is returned.

To download the .XLSM file from this article, click here.