+ Reply to Thread
Results 1 to 5 of 5

Loop through range until empty cell

  1. #1
    Registered User
    Join Date
    02-20-2020
    Location
    Ukraine
    MS-Off Ver
    Microsoft Office for Mac, 16.34
    Posts
    13

    Loop through range until empty cell

    Hi, I have two columns with some data in hidden sheet. That is some mapping for substitution, i.e. I need to loop through that data and make substitutions in some specific table.
    I found this solution for looping:
    Please Login or Register  to view this content.
    This works perfectly if I am on this worksheet. But how to do the same if user is on another sheet, and user shouldn't see this hidden sheet?

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop through range until empty cell

    Hi,
    Try to avoid code like .Select. On the whole it's not needed. Address objects like ranges etc. directly and preface them with the VBA Sheet name. That's the VBA sheet name not the tab name. Below I'm using the VBA sheet code name Sheet2 which may of course be different to the sheets tab name.

    In addition, whenever you find yourself creating looping code stop and think is this really necessary? Most of the time it isn't. Loops are inherently slow when they have to jump back and forth between VBA and the Excel application - there's a big time overhead at each pass through the loop.

    In your case you appear to be wanting to infill blanks in column A using an earlier cell value in column A. This can be achieved with one line of code

    Please Login or Register  to view this content.

    In other situations where copying or deleting stuff is needed the fastest, most efficient and elegant way for this sort of stuff is to use an Autofilter to filter the rows you're interested in.
    Then use syntax like

    Please Login or Register  to view this content.
    then paste to your destination cell.

    This uses just one Copy / Paste instruction rather than many if you're looping through many cells.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    02-20-2020
    Location
    Ukraine
    MS-Off Ver
    Microsoft Office for Mac, 16.34
    Posts
    13

    Re: Loop through range until empty cell

    Thanks, I'm investigating your approach. But I need not a previous value, but a cell on right, I mean, I have the following structure:
    Old value | New Value
    xxx | yyy
    aaa | bbb

    And I need to find all "old values" in another sheet and replace them with appropriate new values

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop through range until empty cell

    Hi,
    If you upload the actual workbook or at least a representative cut down copy in which you've manually added the results you expect I'll take a look.

    This is clearly a much simplified example and probably doesn't represent the size of your actual data. We often find that when we give an answer to what is a trivial non representative example when the solution is used with the real world data it doesn't work because of factors like string length and other stuff which is important but hasn't been mentioned.

  5. #5
    Registered User
    Join Date
    02-20-2020
    Location
    Ukraine
    MS-Off Ver
    Microsoft Office for Mac, 16.34
    Posts
    13

    Re: Loop through range until empty cell

    Ok, here is mockup of my Workbook (should be attached to this post) Note, Sheet2 is hidden!
    Sheet2 contains standard column names in col A which should be substituted with values from col B.
    So, on Sheet1 we have some table with headers "Col1, Col2, ..., Col9, Col10" and we need to rename some headers.
    There might be any number of substitutions, so what I was thinking about - loop until we face empty cell, then loop through table headers and perform substitution.
    Attached Files Attached Files

+ 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. [SOLVED] Using macro to find first empty cell in column. If range is empty returns error.
    By A440 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 05-23-2019, 11:03 AM
  2. How To Loop a copy range macro until lookup cell is blank/empty
    By iggypop in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-02-2017, 09:44 AM
  3. [SOLVED] Loop copy cell of range to 1 cell, and so on till cell empty
    By countryfan_nt in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-16-2017, 02:33 AM
  4. [SOLVED] How to create a dynamic range if the last cell is empty and the columns have empty cells
    By peter_swe in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-03-2016, 11:18 PM
  5. Replies: 0
    Last Post: 08-03-2016, 07:01 PM
  6. [SOLVED] How to copy into next empty row using "for each cell in range" loop
    By GIS2013 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-23-2013, 02:30 PM
  7. MACRO to empty a range of cells based on whether specified cell is empty
    By TBJV in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-01-2013, 10:35 PM

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