+ Reply to Thread
Results 1 to 9 of 9

Looping through a list of city names

  1. #1
    Registered User
    Join Date
    05-16-2019
    Location
    Tampa, Florida
    MS-Off Ver
    MS Office 365
    Posts
    46

    Looping through a list of city names

    I have a list of city names running across the top of my spreadsheet starting with column B1.

    Miami Dayton Detroit Milwaukee Houston Boston Phoenix St. Petersburg Pittsburgh Indianapolis Taos

    I want to create a macro that finds *only certain cities* and deletes the five cells underneath that city name.
    In English, it would be something like this.

    Loop through this list (Detroit, Boston, Pittsburgh)
    Delete the five cells under Detroit
    Delete the five cells under Boston
    Delete the five cells under Pittsburgh
    End Loop

    How would I do something like this? Thank you.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: Looping through a list of city names

    Some Air code for you to test:

    Please Login or Register  to view this content.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    05-16-2019
    Location
    Tampa, Florida
    MS-Off Ver
    MS Office 365
    Posts
    46

    Re: Looping through a list of city names

    It threw an error on the line beginning with "crit =" that stated:

    Compile error:
    Statement invalid outside Type block

    Also, I don't wish to be prompted for the cities that I wish to delete. I would rather hard code a list of cities somewhere and then it deletes the cities in that list.

    Many thanks.

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Looping through a list of city names

    Try this. I've put the list with cities to lookup in column T but you can put that anywhere.
    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  5. #5
    Registered User
    Join Date
    05-16-2019
    Location
    Tampa, Florida
    MS-Off Ver
    MS Office 365
    Posts
    46

    Re: Looping through a list of city names

    Thanks Bakerman but it removed city names from column T where the list resides.

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,080

    Re: Looping through a list of city names

    Try this mod to bakerman2's code
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-16-2019
    Location
    Tampa, Florida
    MS-Off Ver
    MS Office 365
    Posts
    46

    Re: Looping through a list of city names

    Thank you. Worked great. I appreciate the help.

  8. #8
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: Looping through a list of city names

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, as a relatively new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  9. #9
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,080

    Re: Looping through a list of city names

    Quote Originally Posted by jm594 View Post
    Thank you. Worked great. I appreciate the help.
    You're welcome & thanks for the feedback

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 9
    Last Post: 09-19-2018, 08:50 AM
  2. Replies: 8
    Last Post: 10-16-2017, 03:08 AM
  3. [SOLVED] Looping through rows of data to find a certain city
    By bilich in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-10-2017, 12:27 PM
  4. [SOLVED] How can I get country name based on city names in the columns
    By alipezu in forum Excel General
    Replies: 10
    Last Post: 11-29-2016, 02:50 AM
  5. Replies: 3
    Last Post: 02-07-2016, 03:58 AM
  6. Translating City Codes to Names (example attached)
    By cmf0106 in forum Excel General
    Replies: 13
    Last Post: 01-30-2014, 02:07 PM
  7. extract city using city list lookup
    By terrysoper1973 in forum Excel General
    Replies: 1
    Last Post: 09-07-2011, 01:43 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1