Basic Error Handling

tinydancer

New Member
Joined
Jun 15, 2016
Messages
44
I have a macro that basically takes a data dump from QMOS into excel and makes it look decent and puts the information in printable order. I am looking to do some basic error handling that simply displays a message box if something goes wrong in the code. This doesn't need to be advanced at all, just the basics. Every time I've tried something it gives me an error and the error handling code itself is messing everything up. Here is a copy of my code:
Code:
Sub RunAll()                                    'VBA code to run every macro together at once, in descending order
    Call FillWorkCenter
    Call FillDates
    Call sbRemoveDuplicatesSpecificWithHeaders
    Call DeleteYellow
    Call FreezeRow
    Call DeleteRows
    Call Sort
    Call HideColumnG
    Call Borders
End Sub


Sub FillWorkCenter()                        'VBA code to fills in blank spots with given Main Work Center #


Range("D1").Activate                        'specifies column
LastRow = Range("D65000").End(xlUp).Row     'finds last row of data in given column given it's within range




a = ActiveCell.Value                        'establishes "a" as the variable representing the active cell value




Do Until ActiveCell.Row > LastRow           'performs loop until last row of data
    If ActiveCell.Value = "" Then           'determines if active cell is blank
        ActiveCell.Value = a                'cell repopulates as the cell above it
    Else
        a = ActiveCell.Value                'cell repopulates as the cell above it
    End If
        ActiveCell.Offset(1, 0).Activate    'ends loop if data ends
Loop                                        'establishes loop




End Sub






Sub FillDates()                             'VBA code to fill in blank spots with given Main Work Center #




Range("N1").Activate                        'specifies column
LastRow = Range("N65000").End(xlUp).Row     'finds last row of data in given column given it's within range




a = ActiveCell.Value                        'establishes "a" as the variable representing the active cell value




Do Until ActiveCell.Row > LastRow           'performs loop until last row of data
    If ActiveCell.Value = "" Then           'determines if active cell is blank
        ActiveCell.Value = a                'cell repopulates as the cell above it
    Else
        a = ActiveCell.Value                'cell repopulates as the cell above it
    End If
        ActiveCell.Offset(1, 0).Activate    'ends loop if data ends
Loop                                        'establishes loop




End Sub


Sub sbRemoveDuplicatesSpecificWithHeaders()                           'VBA code to remove duplicates from data with headers


Range("A1:T506").RemoveDuplicates Columns:=Array(5), Header:=xlYes    'removes duplicate rows of data based off information in Column E


End Sub


Sub DeleteYellow()                  'VBA code to delete last row from given table


    Range("A507:P507").Select       'establishes range of given row
    Selection.EntireRow.Delete      'deletes given selection
    
End Sub


Sub FreezeRow()              'VBA code to freeze top row of sheet (Headers)


Dim r As Range               'declines variable "r" as a range
Set r = ActiveCell           'sets variable "r" as ActiveCell
Range("A2").Select           'selects given range to be frozen
With ActiveWindow            'represents current window being viewed
    .FreezePanes = False     'rejects freezing of top panes in view
    .ScrollRow = 1           'locates row 1
    .ScrollColumn = 1        'locates column A
    .FreezePanes = True      'freezes row where row 1 and column A intersect
    .ScrollRow = r.Row       'refers to upper left cell
End With
r.Select                     'selects given cells to be frozen


End Sub


Sub DeleteRows()                                                        'VBA code to delete rows that contain "RHOLDMS1" data
    For x = Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1           'counts rows down column A that contain data
        If Cells(x, 1) = "RHOLDMS1" Then Cells(x, 1).EntireRow.Delete   'deletes those cells if they contain phrase "RHOLDMS1"
    Next x                                                              'moves on to next cell
End Sub


Sub Sort()                                                                              'VBA code to sort data based off of three sepertate perameters with layered bias
    ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range _
        ("D2:D507"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal                                                                    'sorts data first alphabetically by column D in ascending order
    ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range _
        ("P2:P507"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal                                                                    'sorts data second alphabetically by column P in ascending order
    ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range _
        ("M2:M507"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal                                                                    'sorts data third alphabetically by column M in ascending order
    With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
        .Header = xlYes                                                                 'establishes the presence of headers
        .MatchCase = False                                                              'establishes that code is case sensitive
        .Orientation = xlTopToBottom                                                    'indicates that columns instead of rows are being sorted
        .SortMethod = xlPinYin                                                          'helps sort non-english characters
        .Apply
    End With
End Sub


Sub HideColumnG()                           'VBA code to hide column G


    Columns("G:G").Select                   'Selects and highlights column to be hidden
    Selection.EntireColumn.Hidden = True    'Hides entire column from site
    
End Sub


Sub Borders()                                                       'VBA code to establish borders around occupied cells


With Cells.SpecialCells(xlCellTypeConstants, 23)                    'identifies cells which are occupied
       .BorderAround xlContinuous, xlThin, xlColorIndexAutomatic    'establishes outer border with specifications
        On Error Resume Next                                        'used in case there are no inside borders
        With .Borders(xlInsideHorizontal)                           'establishes horizontal borders to be constructed
            .LineStyle = xlContinuous                               'establishes line style
            .Weight = xlThin                                        'establishes line thickness
            .ColorIndex = xlAutomatic                               'establishes line color
        End With
        With .Borders(xlInsideVertical)                             'establishes vertical borders to be constructed
            .LineStyle = xlContinuous                               'establishes line style
            .Weight = xlThin                                        'establishes line thickness
            .ColorIndex = xlAutomatic                               'establishes line color
        End With
    End With
    
End Sub

If anybody could give me some tips or just a basic few lines I could use in each macro I would be greatly appreciative, thank you.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Here's a basic scenario:

Rich (BB code):
Sub Macronamehere()

On Error Goto err_handler

' all your code goes here

' this stops the error handler being called when no error has occurred.
Exit sub

' this is a label matching the name used in the On Error Goto statement - note the colon on the end
err_handler:

   Msgbox Err.Description
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top