Hi all,
The code below is what I am working with.
I need it to do the following:
Proper Case in Column A,B,C
Upper Case in Column D
Postal Abbreviations Convert
Keeps 20 Rows at end of table
I plan on adding more to it later but this is where I am at right now
So far Macro1,2,3 work but when I add in Macro4 it gives an error every time I press enter at the end of the table in column A.
Can someone tell me what I did wrong? Cause I am not sure what is happening or how to fix it.
Thank you.
The code below is what I am working with.
I need it to do the following:
Proper Case in Column A,B,C
Upper Case in Column D
Postal Abbreviations Convert
Keeps 20 Rows at end of table
I plan on adding more to it later but this is where I am at right now
So far Macro1,2,3 work but when I add in Macro4 it gives an error every time I press enter at the end of the table in column A.
Can someone tell me what I did wrong? Cause I am not sure what is happening or how to fix it.
Thank you.
Code:
Option ExplicitOption Compare Text
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Call Macro1(Target) ''Proper Case in Column A,B,C
Call Macro2(Target) ''Upper Case in Column D
Call Macro3(Target) ''Postal Abbreviations Convert
Call Macro4(Target) ''Keeps 20 Rows at end of table.
End Sub
Sub Macro1(ByVal Target As Excel.Range)
On Error GoTo Error
Dim rngCell As Range
If Intersect(Target, Range("A2:C5000")) Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each rngCell In Target.Cells
Target.Value = Application.WorksheetFunction.Proper(Target.Value)
Next
Letscontinue:
Application.EnableEvents = True
Exit Sub
Error: ''Error Handling
MsgBox "Press OK to Continue."
Resume Letscontinue
End Sub
Sub Macro2(ByVal Target As Excel.Range)
On Error GoTo Error1
Dim rngCell As Range
If Intersect(Target, Range("D2:D5000")) Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each rngCell In Target.Cells
Target = UCase(Target)
Next
Letscontinue1:
Application.EnableEvents = True
Exit Sub
Error1: ''Error Handling
MsgBox "Press OK to Continue."
Resume Letscontinue1
End Sub
Sub Macro3(ByVal Target As Range)
Dim r As Range, cel As Range
Set r = Range("D2:D5000")
For Each cel In r
Select Case cel.Value
''U.S.A Postal Abbreviations
Case "Alabama"
cel.Offset(, 0).Value = "AL"
Case "Alaska"
cel.Offset(, 0).Value = "AK"
Case "Arizona"
cel.Offset(, 0).Value = "AZ"
Case "Arkansas"
cel.Offset(, 0).Value = "AR"
Case "California"
cel.Offset(, 0).Value = "CA"
Case "Colorado"
cel.Offset(, 0).Value = "CO"
Case "Connecticut"
cel.Offset(, 0).Value = "CT"
Case "Delaware"
cel.Offset(, 0).Value = "DE"
Case "Florida"
cel.Offset(, 0).Value = "FL"
Case "Georgia"
cel.Offset(, 0).Value = "GA"
Case "Hawaii"
cel.Offset(, 0).Value = "HI"
Case "Idaho"
cel.Offset(, 0).Value = "ID"
Case "Illinois"
cel.Offset(, 0).Value = "IL"
Case "Indiana"
cel.Offset(, 0).Value = "IN"
Case "Iowa"
cel.Offset(, 0).Value = "IA"
Case "Kansas"
cel.Offset(, 0).Value = "KS"
Case "Kentucky"
cel.Offset(, 0).Value = "KY"
Case "Louisiana"
cel.Offset(, 0).Value = "LA"
Case "Maine"
cel.Offset(, 0).Value = "ME"
Case "Maryland"
cel.Offset(, 0).Value = "MD"
Case "Massachusetts"
cel.Offset(, 0).Value = "MA"
Case "Michigan"
cel.Offset(, 0).Value = "MI"
Case "Minnesota"
cel.Offset(, 0).Value = "MN"
Case "Mississippi"
cel.Offset(, 0).Value = "MS"
Case "Missouri"
cel.Offset(, 0).Value = "MO"
Case "Montana"
cel.Offset(, 0).Value = "MT"
Case "Nebraska"
cel.Offset(, 0).Value = "NE"
Case "Nevada"
cel.Offset(, 0).Value = "NV"
Case "New Hampshire"
cel.Offset(, 0).Value = "NH"
Case "New Jersey"
cel.Offset(, 0).Value = "NJ"
Case "New Mexico"
cel.Offset(, 0).Value = "NM"
Case "New York"
cel.Offset(, 0).Value = "NY"
Case "North Carolina"
cel.Offset(, 0).Value = "NC"
Case "North Dakota"
cel.Offset(, 0).Value = "ND"
Case "Ohio"
cel.Offset(, 0).Value = "OH"
Case "Oklahoma"
cel.Offset(, 0).Value = "OK"
Case "Oregon"
cel.Offset(, 0).Value = "OR"
Case "Pennsylvania"
cel.Offset(, 0).Value = "PA"
Case "Rhode Island"
cel.Offset(, 0).Value = "RI"
Case "South Carolina"
cel.Offset(, 0).Value = "SC"
Case "South Dakota"
cel.Offset(, 0).Value = "SD"
Case "Tennessee"
cel.Offset(, 0).Value = "TN"
Case "Texas"
cel.Offset(, 0).Value = "TX"
Case "Utah"
cel.Offset(, 0).Value = "UT"
Case "Vermont"
cel.Offset(, 0).Value = "VT"
Case "Virginia"
cel.Offset(, 0).Value = "VA"
Case "Washington"
cel.Offset(, 0).Value = "WA"
Case "West Virginia"
cel.Offset(, 0).Value = "WV"
Case "Wisconsin"
cel.Offset(, 0).Value = "WI"
Case "Wyoming"
cel.Offset(, 0).Value = "WY"
''Canadian Postal Abbreviations
Case "Alberta"
cel.Offset(, 0).Value = "AB"
Case "British Columbia", "Colombie-Britannique"
cel.Offset(, 0).Value = "BC"
Case "New Brunswick", "Nouveau-Brunswick"
cel.Offset(, 0).Value = "NB"
Case "Newfoundland and Labrador", "Terre-Neuve-et-Labrador"
cel.Offset(, 0).Value = "NL"
Case "Manitoba", "Terre-Neuve-et-Labrador"
cel.Offset(, 0).Value = "MB"
Case "Nova Scotia", "Nouvelle-Écosse"
cel.Offset(, 0).Value = "NS"
Case "Northwest Territories", "Territoires du Nord-Ouest"
cel.Offset(, 0).Value = "NT"
Case "Nunavut"
cel.Offset(, 0).Value = "NU"
Case "Ontario"
cel.Offset(, 0).Value = "ON"
Case "Prince Edward Island", "Île-du-Prince-Édouard"
cel.Offset(, 0).Value = "PE"
Case "Quebec", "Québec"
cel.Offset(, 0).Value = "QC"
Case "Saskatchewan"
cel.Offset(, 0).Value = "SK"
Case "Yukon"
cel.Offset(, 0).Value = "YT"
'Copy the below 2 lines to add to list
' Case ""
' cel.Offset(, 0).Value = ""
End Select
Next cel
End Sub
Sub Macro4(ByVal Target As Range)
Dim rng As Range ''Expand Table size by 20 rows
Dim tbl As ListObject ''Expand Table size by 20 rows
''Expand Table size by 20 rows
Set tbl = ActiveSheet.ListObjects("Table1") ''Change Table #
Set rng = Range("Table1[#All]").Resize(tbl.Range.Rows.Range("A1").End(xlDown).Row + 20, tbl.Range.Columns.Count) ''Change Table #
tbl.Resize rng
End Sub