+ Reply to Thread
Results 1 to 7 of 7

Delete all rows and columns relative to starter cell.

  1. #1
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Red face Delete all rows and columns relative to starter cell.

    Hello ExcelForum!

    I have a function SearchCells() that searches my active worksheet for a keyword and returns the cell address (as a string). If you use it with this sample Sample.xlsx then SearchCells("Sample", 2) would return the string "C4"

    I am trying to write a subroutine to mass delete cells based on the return from SearchCells such that would function in the following way:

    DeleteCells(SearchCells("Sample", 2), Left) or DeleteCells("C4", Left) would delete column A and B, shifting cells to the left
    DeleteCells(SearchCells("Sample", 2), Right) or DeleteCells("C4", Right) would delete column D and E (all columns to the right of C)
    DeleteCells(SearchCells("Sample", 2), Up) or DeleteCells("C4", Up) would delete rows 1, 2 and 3, shifting cells up
    DeleteCells(SearchCells("Sample", 2), Down) or DeleteCells("C4", Down) would delete rows 5, 6, 7 (all rows below 4)

    Anyone know a simple way to write this?

  2. #2
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Re: Delete all rows and columns relative to starter cell.

    something like this:
    Please Login or Register  to view this content.
    HTH!

  3. #3
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: Delete all rows and columns relative to starter cell.

    That's pretty close. It's working when I call via DeleteCells(Range("C4"), "Down" but because SearchCells("Sample", 2) returns C4 as a string, it breaks. It's a fantastic starting point for me though and a way simpler approach than what I was thinking originally. Thank you for your help!


    I reworked the code a bit to make it a bit easier to expand on. I'm going to add stuff like detection if iLastRow/iLastColumn is the same as rngCell, maybe an option to delete the row/column rngCell is found in, etc. This is how it looks now.

    Please Login or Register  to view this content.
    Last edited by smpita; 07-31-2014 at 02:18 PM.

  4. #4
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: Delete all rows and columns relative to starter cell.

    Fixed the range issue like this:
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: Delete all rows and columns relative to starter cell.

    Fixed the problem where it'd delete the referenced cell if it's on the last row/column
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: Delete all rows and columns relative to starter cell.

    Option to delete source cell as well:
    Please Login or Register  to view this content.
    Last edited by smpita; 07-31-2014 at 06:48 PM.

  7. #7
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Re: Delete all rows and columns relative to starter cell.

    Good work
    Just in case you have more than one worksheet in your workbook, remember the following shall work only for the ActiveSheet.
    Please Login or Register  to view this content.

+ 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. Delete Columns and rows based on cell fill color
    By JackW in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-02-2014, 01:58 PM
  2. Macro to delete certain columns and delete rows based on time in another column
    By beepbeep27 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2012, 11:47 AM
  3. Delete & Merge Columns,Delete Rows with filter, etc
    By traderindia in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-17-2009, 02:12 AM
  4. Need to delete rows relative to selection
    By ratchick in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-22-2005, 12:55 PM
  5. Sorting rows with alphanumeric starter cells
    By axlmastr in forum Excel Formulas & Functions
    Replies: 83
    Last Post: 09-09-2005, 11:05 AM

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