+ Reply to Thread
Results 1 to 9 of 9

Copy cells if fit condition

  1. #1
    Registered User
    Join Date
    03-09-2015
    Location
    US
    MS-Off Ver
    2007
    Posts
    5

    Copy cells if fit condition

    Hi all,
    i'm trying to write a code that will copy cells that fit a certain condition.
    For example if I have column A as follows

    1
    2
    3
    4
    5
    6

    I would like to copy from A1-A6 the values (X) that are 3<X<6, in this case 4 and 5 are between 3 and 6 not including.
    I want to copy them to C4. Since I have 2 values to copy they will be copied to C4 and C5

    I thought that if I record a Macro of conditional formatting I could use that but i don't believe I can.
    Once I copy them I want to do a few things.
    I want to know how many (Count) I have copies (in this case 2) and the average of them (in this case 4.5) and then show a msgbox with that value (4.5)

    Anyone can advice on that process?
    Thanks

  2. #2
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Copy cells if fit condition

    Sure. There's a few ways you could approach this.

    One that comes to mind is a For Each/Loop. For Each can only be applied to Collections and Arrays. Forunately, a 'Range' is considered a Collection.


    Please Login or Register  to view this content.
    For Each is good for breaking into VBA as it's sort of a literal expression. For each Range (cell) in A1:A6, this routine will check and see if it meets criteria, and then paste it into the next blank cell in C. Put something in C3 and the data will autofill below.

    After the procedure loops through the whole range, it outputs via MsgBox.

    As described earlier, a Range can be a cell Range("A1") or a group of cells Range("A1:A6").

    Range(Range("C4"),Range("C" & Rows.Count).End(xlUp)) means C4 to the last cell in C. We count these cells and we also average them.
    Last edited by daffodil11; 03-09-2015 at 06:47 PM.
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  3. #3
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Copy cells if fit condition

    Another alternative:
    Please Login or Register  to view this content.
    or,
    Please Login or Register  to view this content.

    Cheers,
    berlan
    Last edited by berlan; 03-09-2015 at 08:56 PM.

  4. #4
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: Copy cells if fit condition

    Since you mention you had considered conditional formatting: you could avoid all VBA for the project as far as you've described (though I suppose your project could have more pieces). If you don't mind having the desired values shown in C and a dash otherwise, just
    C1=if(and(a1>3,a1<6),a1,"-")
    and copy down. Then C7 could be SUM or AVERAGE (or COUNT, but perhaps not COUNTA) and the dashes would not foul that up. (The reason for the dashes is so that it would be apparent that those cells in C are being used, possibly simplifying maintenance down the road.)

    Personally, if I went with the above, I'd parameterize the 3 and 6, maybe by having them in their own cells, and named as ranges.

    I don't mean to diminish the code solutions above and your needs may be such that you should use them. However as a matter of philosophy and pragmatism and a few other reasons, I often avoid code when noncomplex worksheet functions simply get the result. I say that even though I loooove coding personally, and in many situations I proceed immediately to code, definitely when looping or certain branching is needed, or maybe even if it's more compact to use code. I code a real lot to use InputBox and to process buttons and events. However there are often tradeoffs by using VBA in the long run when others have to maintain a workbook so often I have none.

    There's not a strict answer as to whether VBA should be preferred or avoided depending on several factors, but I thought I'd make the suggestion that in this case you could just "stay on the sheets." Of course, for any individual, their "shop", the ultimate users, and the application, "your mileage may vary."

    No offense to anyone, especially the repliers above who worked to provide good answers; yes I know I'm in a VBA forum!!
    Last edited by Oppressed1; 03-09-2015 at 09:55 PM. Reason: typo, period on sentence
    Chip, if you can read this, you're STILL making the world a better place with your immeasurably valuable gifts. Every week, you continue to help me (and untold numbers of others) tremendously. Practically no alternative outlets can match your effectiveness, even over the vast number of topics you demystify and empower usage of. You were, and still are, amazing.

  5. #5
    Registered User
    Join Date
    03-09-2015
    Location
    US
    MS-Off Ver
    2007
    Posts
    5

    Re: Copy cells if fit condition

    Thanks everyone for the help.
    I ended up using daffodil11's solution as it was easy to understand and manipulate to fit my exact needs.

  6. #6
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Copy cells if fit condition

    @Oppressed1, if you are talking about formulas, I would suggest the use of a simple array formula in C4 to be copied down, filtering out the values not wanted. However, for a msgbox you need the VBA..

    //berlan

  7. #7
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: Copy cells if fit condition

    That's fine berlan, but just to again add perspective, array formulas do add complexity and are more difficult to maintain in my long experience; for example, many times a workbook user either forgot to go shift-enter, or didn't even know about shift-enter. Filters often cause even more drama - for example, inserting/deleting rows while filtered is an adventure, or simply forgetting that you're looking at a subset rather than full unfiltered data can cause false trails and time burn. Again, whether to use them or not depends on numerous factors, namely who will use and maintain the workbook. Filter and arrays are powerful. They're also compact, so in that sense, they're easier to maintain. I'm just saying that there may be a tradeoff. Moreover, for this problem, the formula I suggested is extremely simple to follow and maintain, vs. VBA which would be overkill IMO.

    Another point: over time I've concluded that, when you use filters, an explicit indication on a worksheet (e.g. a caption, or just a prominently visible cell, but more than just a comment) can save immense time down the road, so when you look at a sheet you immediately know there's filtering and array formula. I can recall a lot of time that I and others have wasted because we didn't immediately realize that there was filtering and array formulas.

  8. #8
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Copy cells if fit condition

    @Opressed1, appreciate your inputs. While I'm not totally sure whether you refer to the use of autofilter or not (which is not what I meant), I agree with some of the points you are making. In the end, it needs to be user-friendly and it can be a trade-off between complexity and efficiency.

    But just to illustrate what I meant. For Excel 2010 we could use a non-array formula solve this issue, filtering out the unwanted values by formula (in this case for cell C4 and then copied down) :
    Please Login or Register  to view this content.
    Have a good day.

    Cheers,
    berlan

  9. #9
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: Copy cells if fit condition

    Agreed on all. To clarify, yes, I was referring to autofilter and the "advanced filter." Thanks for your clarification as well.

+ 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] Macro to copy cells into another column if cells contain a certain condition
    By lsm33000 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-11-2014, 02:27 PM
  2. How to copy paste specific cells is a condition is met
    By PriyankaV in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-13-2014, 10:27 PM
  3. [SOLVED] Copy Cells Where Another Cell On Same Row Meets Condition
    By marshak in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-25-2013, 08:36 PM
  4. Copy cells based on a condition
    By ozdemirozgur in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2011, 03:08 PM
  5. Copy cells by multply condition
    By ccsmith in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-06-2010, 03:02 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