I want short code some how

TLS49

Board Regular
Joined
Nov 26, 2019
Messages
135
Office Version
  1. 365
Platform
  1. Windows
How I make code shorter.. It doing for all worksheets. I only want do for one worksheets only sheet name is calle remove cities

VBA Code:
'Removing cities name from the list
'Range D2:D273,G2:G273
Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
    With ws
        .Cells.Replace What:="ARIZONA ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
    End With
    With ws
  .Cells.Replace What:="ATLANTA ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="BALTIMORE ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
 
     .Cells.Replace What:="BUFFALO ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="CAROLINA ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="CHICAGO ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False


    .Cells.Replace What:="CINCINNATI ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="CLEVELAND ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="DALLAS ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="DENVER ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="DETROIT ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="GREEN BAY ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="HOUSTON ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="INDIANAPOLIS ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="JACKSONVILLE ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="KANSAS CITY ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="LOS ANGELES ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="MIAMI ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="MINNESOTA ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="NEW ENGLAND ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="NEW ORLEANS ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="NEW YORK ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="LAS VEGAS ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="PHILADELPHIA ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="PITTSBURGH ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="SAN FRANCISCO ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False


    .Cells.Replace What:="SEATTLE ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="TAMPA BAY ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="TENNESSEE ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    .Cells.Replace What:="WASHINGTON ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
    
     End With
    
    Next ws
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi
What about
VBA Code:
Sub test()
Dim ws As Worksheet
Dim a
Dim i
a = Array("ARIZONA", "ATLANTA", "BALTIMORE", ..............) ' fill all the cities
    For Each ws In ActiveWorkbook.Worksheets
    With ws
    For i = 0 To UBound(a)
    On Error Resume Next
        .Cells.Replace What:=a(i, 1), Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
      Next
     End With
     Next
End Sub
 
Upvote 0
Hi
What about
VBA Code:
Sub test()
Dim ws As Worksheet
Dim a
Dim i
a = Array("ARIZONA", "ATLANTA", "BALTIMORE", ..............) ' fill all the cities
    For Each ws In ActiveWorkbook.Worksheets
    With ws
    For i = 0 To UBound(a)
    On Error Resume Next
        .Cells.Replace What:=a(i, 1), Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
      Next
     End With
     Next
End Sub

The Variable is not defined. I have error post picture blue line have a problem. I need worksheet in code is called Remove Cities
 

Attachments

  • ERROR VBA CODE.jpg
    ERROR VBA CODE.jpg
    67.8 KB · Views: 16
Upvote 0
If I understand you correctly, you only want this applied to a single sheet called "remove cities"? If that is correct, try the following code. I note that your list includes both states as well as cities, and that each name is followed by a space.
VBA Code:
Option Explicit
Sub RemoveCities()
    Dim ws As Worksheet, arr, i As Long
    Set ws = Worksheets("remove cities")    '<~~ *** MAKE SURE THE SPELLING IS CORRECT ***
    
    arr = Array("ARIZONA ", "ATLANTA ", "BALTIMORE ", "BUFFALO ", "CAROLINA ", "CHICAGO ", "CINCINNATI ", _
                "CLEVELAND ", "DALLAS ", "DENVER ", "DETROIT ", "GREEN BAY ", "HOUSTON ", "INDIANAPOLIS ", _
                "JACKSONVILLE ", "KANSAS CITY ", "LOS ANGELES ", "MIAMI ", "MINNESOTA ", "NEW ENGLAND ", _
                "NEW ORLEANS ", "NEW YORK ", " LAS VEGAS ", "PHILADELPHIA ", "PITTSBURGH ", "SAN FRANCISCO ", _
                "SEATTLE ", "TAMPA BAY ", "TENNESSEE ", "WASHINGTON ")
    With ws.Cells
        For i = LBound(arr) To UBound(arr)
            .Replace arr(i), "", xlPart
        Next i
    End With
End Sub
 
Upvote 0
Solution
VBA Code:
[CODE=vba]
Sub test()
Dim ws As Worksheet
Dim a
Dim i&
a = Array("ARIZONA ", "ATLANTA ", "BALTIMORE ", "BUFFALO ", "CAROLINA ", "CHICAGO ", "CINCINNATI ", _
                "CLEVELAND ", "DALLAS ", "DENVER ", "DETROIT ", "GREEN BAY ", "HOUSTON ", "INDIANAPOLIS ", _
                "JACKSONVILLE ", "KANSAS CITY ", "LOS ANGELES ", "MIAMI ", "MINNESOTA ", "NEW ENGLAND ", _
                "NEW ORLEANS ", "NEW YORK ", " LAS VEGAS ", "PHILADELPHIA ", "PITTSBURGH ", "SAN FRANCISCO ", _
                "SEATTLE ", "TAMPA BAY ", "TENNESSEE ", "WASHINGTON ")
    With sheets("Remove Cities")
    For i = 0 To UBound(a)
    On Error Resume Next
        .Cells.Replace What:=a(i, 1), Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
      Next
     End With

End Sub
[/CODE]

I gad mention to you to fill the array Like kevin
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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