Hello All,
I have come up with the folowing bit of code and I am looking for ways to make it go faster.
Can anyoone see any ways to make it run faster. What it is doing is looking at the value in column a and putting a value into column E based on that value. For sheets with a large number of rows, it really starts to slow down. If I have written the code completely wrong, and this is just a slow way of doing it, I don't mind redoing the whole thing, if someone could steer me in the right direction.
Thanks
I have come up with the folowing bit of code and I am looking for ways to make it go faster.
Rich (BB code):
Sub Marks_Insert_timezone()
'
' Insert_timezone Macro
Timezone = InputBox("What timezone would you like:" & Chr(13) & " (P) PacElseific or" & Chr(13) & " (M) Mountatin or" & Chr(13) & " (C) Central or" & Chr(13) & " (E) Eastern?")
Count = 1
If Timezone = "P" Or Timezone = "p" Then
' Use the following lines for PST calling
timeHST = "-120"
timeALA = "-060"
timePST = "0000"
timeMST = "0060"
timeCST = "0120"
timeEST = "0180"
timeATL = "0240"
timeNFT = "0270"
ElseIf Timezone = "M" Or Timezone = "m" Then
' Use the following lines for MST calling
timeHST = "-180"
timeALA = "-120"
timePST = "-060"
timeMST = "0000"
timeCST = "0060"
timeEST = "0120"
timeATL = "0180"
timeNFT = "0210"
ElseIf Timezone = "C" Or Timezone = "c" Then
' Use the following lines for CST calling
timeHST = "-240"
timeALA = "-180"
timePST = "-120"
timeMST = "-060"
timeCST = "0000"
timeEST = "0060"
timeATL = "0120"
timeNFT = "0150"
ElseIf Timezone = "E" Or Timezone = "e" Then
' Uncomment the following lines for EST calling
timeHST2 = "-360"
timeHST = "-300"
timeALA = "-240"
timePST = "-180"
timeMST = "-120"
timeCST = "-060"
timeEST = "0000"
timeATL = "0060"
timeNFT = "0090"
End If
'TURN OFF SCREENUPDATING TO ELIMINATE UNWANTED FLASHING WHILE MACRO IS RUNNING
Application.ScreenUpdating = False
Dim myRng As Range
'Dim cell As Range
Set myRng = Range("A2:A65536")
For Each cell In myRng
If cell.Value = "" Then GoTo EndMacro
' Insert the timezone based on the area code
'BC Area Codes
If cell.Value = "604" Or "778" Then
cell.Offset(0, 4).Value = timePST
ElseIf cell.Value = "250" Then
' For North East BC and Creston change in the summer to PST
If cell.Offset(0, 1).Value = "219" Or "719" Or "782" Or "784" Or "242" Or "843" Or _
"261" Or "262" Or "263" Or "264" Or "271" Or "780" Or "785" Or "786" Or "787" Or "793" Or _
"401" Or "601" Or "788" Or "630" Or "759" Or "772" Or "783" Or "789" Or "827" Or _
"254" Or "402" Or "428" Or "431" Or "435" Then
cell.Offset(0, 4).Value = timePST
' For the Kootenays except Creston
ElseIf cell.Offset(0, 1).Value = "223" Or "225" Or "227" Or "270" Or "272" Or "278" Or "290" Or "340" Or "341" Or _
"342" Or "343" Or "344" Or "345" Or "346" Or "347" Or "348" Or "349" Or "417" Or _
"420" Or "421" Or "422" Or "423" Or "424" Or "425" Or "426" Or "427" Or "429" Or _
"430" Or "432" Or "433" Or "439" Or "489" Or "529" Or "531" Or "581" Or "688" Or _
"829" Or "865" Or "866" Or "887" Or "919" Then
cell.Offset(0, 4).Value = timeMST
Else 'all others are Pacfic time
cell.Offset(0, 4).Value = timePST
End If
'Alberta Area Codes (403 or 780)
ElseIf cell.Value = "403" Or "780" Then
cell.Offset(0, 4).Value = timeMST
' SK-Saskatchewan (306)
' (Need to change to CST during Standard Time and MST during Daylight Time)
ElseIf cell.Value = "306" Then
cell.Offset(0, 4).Value = timeCST
' MB-Manitoba (204)
ElseIf cell.Value = "204" Then
cell.Offset(0, 4).Value = timeCST
' ON-Ontario (289 or 416 or 519 or 613 or 647 or 705 or 807 or 905)
ElseIf cell.Value = "289" Or "416" Or "519" Or "613" Or "647" Or "705" Or "905" Then
cell.Offset(0, 4).Value = timeEST
ElseIf cell.Value = "807" Then
'Check for Western Ontario Central time exchanges
ElseIf cell.Offset(0, 1).Value = "221" Or "222" Or "223" Or "224" Or "225" Or "226" Or "227" Or "274" Or "275" Or "347" Or "363" Then
cell.Offset(0, 4).Value = timeCST
ElseIf cell.Offset(0, 1).Value = "466" Or "467" Or "468" Or "469" Or "471" Or "481" Or "482" Or "483" Or "484" Or "485" Or "486" Or "487" Or "488" Then
cell.Offset(0, 4).Value = timeCST
ElseIf cell.Offset(0, 1).Value = "529" Or "543" Or "547" Or "548" Or "582" Or "584" Or "595" Or "597" Then
cell.Offset(0, 4).Value = timeCST
ElseIf cell.Offset(0, 1).Value = "599" Or "662" Or "727" Or "733" Or "735" Or "737" Or "749" Or "755" Or "771" Or "772" Or "773" Or "774" Or "775" Or "776" Then
cell.Offset(0, 4).Value = timeCST
ElseIf cell.Offset(0, 1).Value = "852" Or "925" Or "926" Or "927" Or "928" Or "929" Or "934" Or "937" Or "938" Or "947" Or "967" Then
cell.Offset(0, 4).Value = timeCST
'Else
'cell.Offset(0, 4).Value = timeEST
' QC-Quebec (418 or 450 or 514 or 819)
ElseIf cell.Offset(0, 1).Value = "418" Or "450" Or "514" Or "819" Then
cell.Offset(0, 4).Value = timeEST
' PE-PEI and NS-Nova Scotia (902)
ElseIf cell.Offset(0, 1).Value = "902" Then
cell.Offset(0, 4).Value = timeATL
' NB-New Brunswick (506)
ElseIf cell.Offset(0, 1).Value = "506" Then
cell.Offset(0, 4).Value = timeATL
' NF-Newfoundland (709)
ElseIf cell.Offset(0, 1).Value = "709" Then
cell.Offset(0, 4).Value = timeNFT
' YT-Yukon and NT-Northwest Territories and NU-Nunavut (867)
ElseIf cell.Offset(0, 1).Value = "867" Then
ElseIf cell.Offset(0, 1).Value = "536" Or "668" Or "667" Or "841" Or "993" Then 'Yukon Prefixes
cell.Offset(0, 4).Value = timePST
ElseIf cell.Offset(0, 1).Value = "669" Or "777" Or "873" Or "920" Or "983" Then ' Northwest Territories prefixes
cell.Offset(0, 4).Value = timeCST
'Else
'cell.Offset(0, 4).Value = timeCST
' AK-Alaska (907)
ElseIf cell.Offset(0, 1).Value = "907" Then
cell.Offset(0, 4).Value = timeALA
' AL-Alabama (205 or 251 or 256 or 334)
ElseIf cell.Offset(0, 1).Value = "205" Or "251" Or "256" Or "334" Then
cell.Offset(0, 4).Value = timeCST
' AR-Arkansas (479 or 501 or 870)
ElseIf cell.Offset(0, 1).Value = "479" Or "501" Or "870" Then
cell.Offset(0, 4).Value = timeCST
' AZ-Arizona (480 or 520 or 602 or 623 or 928)
' (Need to change to MST during Standard Time and PST during Daylight Time)
ElseIf cell.Offset(0, 1).Value = "480" Or "520" Or "602" Or "623" Or "928" Then
cell.Offset(0, 4).Value = timeMST
' BA-Bahamas (242)
ElseIf cell.Offset(0, 1).Value = "242" Then
cell.Offset(0, 4).Value = timeEST
' CA-CalElseifornia (209 or 213 or 310 or 323 or 341 or 369 or 408 or 415 or 424 or 442 or
' 510 or 530 or 559 or 562 or 619 or 626 or 627 or 628 or 650 or 657 or
' 661 or 669 or 707 or 714 or 747 or 752 or 760 or 764 or 805 or 818 or
' 831 or 858 or 909 or 916 or 925 or 935 or 949 or 951)
ElseIf cell.Offset(0, 1).Value = "209" Or "213" Or "310" Or "323" Or "341" Or "369" Or "408" Or "415" Or "424" Or "442" Then
cell.Offset(0, 4).Value = timePST
ElseIf cell.Offset(0, 1).Value = "510" Or "530" Or "559" Or "562" Or "619" Or "626" Or "627" Or "628" Or "650" Or "657" Then
cell.Offset(0, 4).Value = timePST
ElseIf cell.Offset(0, 1).Value = "661" Or "669" Or "707" Or "714" Or "747" Or "752" Or "760" Or "764" Or "805" Or "818" Then
cell.Offset(0, 4).Value = timePST
ElseIf cell.Offset(0, 1).Value = "831" Or "858" Or "909" Or "916" Or "925" Or "935" Or "949" Or "951" Then
cell.Offset(0, 4).Value = timePST
' CO-Colorado (303 or 719 or 720 or 970)
ElseIf cell.Offset(0, 1).Value = "303" Or "719" Or "720" Or "970" Then
cell.Offset(0, 4).Value = timeMST
' CT-Connecticut (203 or 475 or 860 or 959)
ElseIf cell.Offset(0, 1).Value = "203" Or "475" Or "860" Or "959" Then
cell.Offset(0, 4).Value = timeEST
' DC-District of Columbia (202)
ElseIf cell.Offset(0, 1).Value = "202" Then
cell.Offset(0, 4).Value = timeEST
' DE-Delaware (302)
ElseIf cell.Offset(0, 1).Value = "302" Then
cell.Offset(0, 4).Value = timeEST
' FL-Florida (239 or 305 or 321 or 352 or 386 or 407 or 561 or 727 or 754 or 772 or 786 or
' 813 or 850 or 863 or 904 or 941 or 954)
ElseIf cell.Offset(0, 1).Value = "239" Or "305" Or "321" Or "352" Or "386" Or "407" Or "561" Or "727" Or "754" Or "772" Or "786" Then
cell.Offset(0, 4).Value = timeEST
ElseIf cell.Offset(0, 1).Value = "813" Or "850" Or "863" Or "904" Or "941" Or "954" Then
cell.Offset(0, 4).Value = timeEST
' GA-Georgia (229 or 404 or 470 or 478 or 678 or 706 or 770 or 912)
ElseIf cell.Offset(0, 1).Value = "229" Or "404" Or "470" Or "478" Or "678" Or "706" Or "770" Or "912" Then
cell.Offset(0, 4).Value = timeEST
' HI-Hawaii (808)
' (Need to change to HST during Standard Time and HST2 during Daylight Time)
ElseIf cell.Offset(0, 1).Value = "808" Then
cell.Offset(0, 4).Value = timeHST
' IA-Iowa (319 or 515 or 563 or 641 or 712)
ElseIf cell.Offset(0, 1).Value = "319" Or "515" Or "563" Or "641" Or "712" Then
cell.Offset(0, 4).Value = timeCST
' ID-Idaho (208)
ElseIf cell.Offset(0, 1).Value = "208" Then
cell.Offset(0, 4).Value = timeMST
' IL-Illinois (217 or 224 or 309 or 312 or 331 or 464 or 618 or 630 or 708 or 773 or
' 815 or 847 or 872)
ElseIf cell.Offset(0, 1).Value = "217" Or "224" Or "309" Or "312" Or "331" Or "464" Or "618" Or "630" Or "708" Or "773" Then
cell.Offset(0, 4).Value = timeCST
ElseIf cell.Offset(0, 1).Value = "815" Or "847" Or "872" Then
cell.Offset(0, 4).Value = timeCST
' IN-Indiana (219 or 260 or 317 or 574 or 765 or 812)
' (Need to change to EST during Standard Time and CST during Daylight Time)
ElseIf cell.Offset(0, 1).Value = "219" Or "260" Or "317" Or "574" Or "765" Or "812" Then
cell.Offset(0, 4).Value = timeEST
' KS-Kansas (316 or 620 or 785 or 913)
ElseIf cell.Offset(0, 1).Value = "316" Or "620" Or "785" Or "913" Then
cell.Offset(0, 4).Value = timeCST
' KY-Kentucky (270 or 502 or 606 or 859)
ElseIf cell.Offset(0, 1).Value = "270" Or "502" Or "859" Then
cell.Offset(0, 4).Value = timeCST
ElseIf cell.Offset(0, 1).Value = "606" Then
cell.Offset(0, 4).Value = timeEST
' LA-Louisiana (225 or 318 or 337 or 504 or 985)
ElseIf cell.Offset(0, 1).Value = "225" Or "318" Or "337" Or "504" Or "985" Then
cell.Offset(0, 4).Value = timeCST
' MA-Massachusetts (339 or 351 or 413 or 508 or 617 or 774 or 781 or 857 or 978)
ElseIf cell.Offset(0, 1).Value = "339" Or "351" Or "413" Or "508" Or "617" Or "774" Or "781" Or "857" Or "978" Then
cell.Offset(0, 4).Value = timeEST
' MD-Maryland (240 or 301 or 410 or 443)
ElseIf cell.Offset(0, 1).Value = "240" Or "301" Or "410" Or "443" Then
cell.Offset(0, 4).Value = timeEST
' ME-Maine (207)
ElseIf cell.Offset(0, 1).Value = "207" Then
cell.Offset(0, 4).Value = timeEST
' MI-Michigan (231 or 248 or 269 or 278 or 313 or 517 or 586 or 616 or 734 or 810 or 906 or 989)
ElseIf cell.Offset(0, 1).Value = "231" Or "248" Or "269" Or "278" Or "313" Or "517" Or "586" Or "616" Or "734" Or "810" Or "906" Or "989" Then
cell.Offset(0, 4).Value = timeEST
' MN-Minnesota (218 or 320 or 507 or 612 or 651 or 763 or 952)
ElseIf cell.Offset(0, 1).Value = "218" Or "320" Or "507" Or "612" Or "651" Or "763" Or "952" Then
cell.Offset(0, 4).Value = timeCST
' MS-Mississippi (228 or 601 or 662)
ElseIf cell.Offset(0, 1).Value = "228" Or "601" Or "662" Then
cell.Offset(0, 4).Value = timeCST
' MO-Missouri (557 or 573 or 636 or 660 or 975 or 314 or 816 or 417)
ElseIf cell.Offset(0, 1).Value = "557" Or "573" Or "636" Or "660" Or "975" Or "314" Or "816" Or "417" Then
cell.Offset(0, 4).Value = timeCST
' MT-Montana (406)
ElseIf cell.Offset(0, 1).Value = "406" Then
cell.Offset(0, 4).Value = timeMST
' NC-North Carolina (252 or 336 or 828 or 910 or 980 or 919 or 704)
ElseIf cell.Offset(0, 1).Value = "252" Or "336" Or "828" Or "910" Or "980" Or "919" Or "704" Then
cell.Offset(0, 4).Value = timeEST
' ND-North Dakota (701)
ElseIf cell.Offset(0, 1).Value = "701" Then
cell.Offset(0, 4).Value = timeCST
' NE-Nebraska (402-Eastern or 308-Western)
ElseIf cell.Offset(0, 1).Value = "402" Then
cell.Offset(0, 4).Value = timeCST
ElseIf cell.Offset(0, 1).Value = "308" Then
cell.Offset(0, 4).Value = timeMST
' NH-Hew Hampshire (603)
ElseIf cell.Offset(0, 1).Value = "603" Then
cell.Offset(0, 4).Value = timeEST
' NJ-New Jersey (201 or 609 or 732 or 856 or 908 or 973)
ElseIf cell.Offset(0, 1).Value = "201" Or "609" Or "732" Or "856" Or "908" Or "973" Then
cell.Offset(0, 4).Value = timeEST
' NM-New Mexico (505)
ElseIf cell.Offset(0, 1).Value = "505" Then
cell.Offset(0, 4).Value = timeMST
' NV-Nevada (775 or 702)
ElseIf cell.Offset(0, 1).Value = "775" Or "702" Then
cell.Offset(0, 4).Value = timePST
' NY-New York (212 or 315 or 347 or 516 or 518 or 585 or 607 or 631 or 646 or 716 or 718 or 845 or 914 or 917)
ElseIf cell.Offset(0, 1).Value = "212" Or "315" Or "347" Or "516" Or "518" Or "585" Or "607" Or "631" Or "646" Or "716" Or "718" Or "845" Or "914" Or "917" Then
cell.Offset(0, 4).Value = timeEST
' OH-Ohio (216 or 234 or 330 or 419 or 440 or 513 or 614 or 740 or 937)
ElseIf cell.Offset(0, 1).Value = "216" Or "234" Or "330" Or "419" Or "440" Or "513" Or "614" Or "740" Or "937" Then
cell.Offset(0, 4).Value = timeEST
' OK-Oklahoma (580 or 918 or 405)
ElseIf cell.Offset(0, 1).Value = "580" Or "918" Or "405" Then
cell.Offset(0, 4).Value = timeCST
' OR-Oregon (503 or 541 or 971)
ElseIf cell.Offset(0, 1).Value = "503" Or "541" Or "971" Then
cell.Offset(0, 4).Value = timePST
' PA-Pennsylvania (215 or 267 or 412 or 484 or 570 or 610 or 717 or 724 or 814)
ElseIf cell.Offset(0, 1).Value = "215" Or "267" Or "412" Or "484" Or "570" Or "610" Or "717" Or "724" Or "814" Then
cell.Offset(0, 4).Value = timeEST
' PR-Puerto Rico (787 or 939)
ElseIf cell.Offset(0, 1).Value = "787" Or "939" Then
cell.Offset(0, 4).Value = timeEST
' RI-Rhode Island (401)
ElseIf cell.Offset(0, 1).Value = "401" Then
cell.Offset(0, 4).Value = timeEST
' SC-South Carolina (803 or 843 or 864)
ElseIf cell.Offset(0, 1).Value = "803" Or "843" Or "864" Then
cell.Offset(0, 4).Value = timeEST
' SD-South Dakota (605)
ElseIf cell.Offset(0, 1).Value = "605" Then
cell.Offset(0, 4).Value = timeCST
' TN-Tennessee (423 or 615 or 731 or 865 or 901 or 931)
ElseIf cell.Offset(0, 1).Value = "615" Or "731" Or "865" Or "901" Or "931" Then
cell.Offset(0, 4).Value = timeCST
ElseIf cell.Offset(0, 1).Value = "423" Then
cell.Offset(0, 4).Value = timeEST
' TX-Texas (210 or 214 or 254 or 281 or 361 or 409 or 469 or 512 or 682 or 713 or 806 or 817 or
' 830 or 832 or 903 or 915 or 936 or 940 or 956 or 972 or 979)
ElseIf cell.Offset(0, 1).Value = "210" Or "214" Or "254" Or "281" Or "361" Or "409" Or "469" Or "512" Or "682" Or "713" Or "806" Or "817" Then
cell.Offset(0, 4).Value = timeCST
ElseIf cell.Offset(0, 1).Value = "830" Or "832" Or "903" Or "936" Or "940" Or "956" Or "972" Or "979" Then
cell.Offset(0, 4).Value = timeCST
ElseIf cell.Offset(0, 1).Value = "915" Then
' need to split out time zone information
' see http://www.lib.utexas.edu/maps/united_states/texas_90.jpg
' and http://www.areacodeonlinelookup.com/area_codes_Texas_TX.html
' and http://www.united-states-online.de/maptime.htm
cell.Offset(0, 4).Value = timeCST
' UT-Utah (385 or 435 or 801)
ElseIf cell.Offset(0, 1).Value = "385" Or "435" Or "801" Then
cell.Offset(0, 4).Value = timeMST
' VA-Virginia (276 or 434 or 540 or 571 or 757 or 703 or 804)
ElseIf cell.Offset(0, 1).Value = "276" Or "434" Or "540" Or "571" Or "757" Or "703" Or "804" Then
cell.Offset(0, 4).Value = timeEST
' VI-Virgin Islands (340)
ElseIf cell.Offset(0, 1).Value = "340" Then
cell.Offset(0, 4).Value = timeEST
' VT-Vermont (802)
ElseIf cell.Offset(0, 1).Value = "802" Then
cell.Offset(0, 4).Value = timeEST
' WA-Washington (206 or 253 or 360 or 425 or 509)
ElseIf cell.Offset(0, 1).Value = "206" Or "253" Or "360" Or "425" Or "509" Then
cell.Offset(0, 4).Value = timePST
' WI-Wisconsin (262 or 414 or 608 or 715 or 920)
ElseIf cell.Offset(0, 1).Value = "262" Or "414" Or "608" Or "715" Or "920" Then
cell.Offset(0, 4).Value = timeCST
' WV-West Virginia (304)
ElseIf cell.Offset(0, 1).Value = "304" Then
cell.Offset(0, 4).Value = timeEST
' WY-Wyoming (307)
ElseIf cell.Offset(0, 1).Value = "307" Then
cell.Offset(0, 4).Value = timeMST
End If
Next cell
Application.ScreenUpdating = True
EndMacro:
Application.ScreenUpdating = True
End Sub
Can anyoone see any ways to make it run faster. What it is doing is looking at the value in column a and putting a value into column E based on that value. For sheets with a large number of rows, it really starts to slow down. If I have written the code completely wrong, and this is just a slow way of doing it, I don't mind redoing the whole thing, if someone could steer me in the right direction.
Thanks