+ Reply to Thread
Results 1 to 22 of 22

VBA To copy cells in a range that contain data skipping formulas

  1. #1
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    VBA To copy cells in a range that contain data skipping formulas

    I have the following code in a macro that selects all cells in the range. This works fine when the range on the sheet is either null or contains data. What i am looking for is a way to modify this so that it does not copy cells that contain a formula, only actual values. Any help is greatly appreciated.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: VBA To copy cells in a range that contain data skipping formulas

    If a cell contains a formula, the value of the leftmost character of the formula property of the cell will be an equals sign.

    Please Login or Register  to view this content.
    Martin

  3. #3
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: VBA To copy cells in a range that contain data skipping formulas

    Thank you mrice, however when I change the first IF to your suggestion it still selects the entire range. I am trying to figure out how to combine both conditions. Skip blanks and where cell contains formula.

    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA To copy cells in a range that contain data skipping formulas

    Please Login or Register  to view this content.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA To copy cells in a range that contain data skipping formulas

    Maybe ...

    Please Login or Register  to view this content.
    Last edited by shg; 04-16-2016 at 02:37 PM.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: VBA To copy cells in a range that contain data skipping formulas

    I should have posted a copy of the sheet am working on for this as well. It is shown below. The formulas are all based on cells to the left having data, so some of the cells may contain actual values and others just the formula. With the addition of both the last two pieces of code I am getting the "No Cells Were Found" error.

    CarbPic.JPG

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA To copy cells in a range that contain data skipping formulas

    If the formula is based to the left of column N, why are using column N then? All of the range you specified may have a formula, hence getting the error. Please attach a sample book, not a picture.
    Last edited by AB33; 04-16-2016 at 03:01 PM.

  8. #8
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: VBA To copy cells in a range that contain data skipping formulas

    Column N contains the total carbs for the line, there are five lines per section on the Daily Meals sheet, not all 5 lines will be used in most cases. So, for lines that do contain a food item, col N will contain the product of Servings and Carbs\Serving. I want to allow for a submit process that will copy over the item and the total carbs to a log sheet.

    For some reason this will not allow me to attach my file, even using the advanced reply method.

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA To copy cells in a range that contain data skipping formulas

    In this cells Range("N4:N8"). all have formulas, hence for getting the error.
    You can try this one

    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: VBA To copy cells in a range that contain data skipping formulas

    Now this is triggering the Object variable or With block variable not set error. By the way, thank you for your assistance on this, it is very much appreciated.

    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA To copy cells in a range that contain data skipping formulas

    If you look at my code, I have defined my variables. You need to define the find function as an object (Range)

    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: VBA To copy cells in a range that contain data skipping formulas

    I have it at the top of the sub, here is the full sub as it stands now, there will be the paste code in at the end as well, once I get past this issue.

    Please Login or Register  to view this content.

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA To copy cells in a range that contain data skipping formulas

    You have removed the Rng function from the code.

    Set rng = Nothing should be at the bottom, just before the end sub

    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: VBA To copy cells in a range that contain data skipping formulas

    I have two different copy loops in the sub, 1 for the Items and the other for the Total Carbs section, so rather than using 2 rng variables, I am pasting in the items, then setting the rng variable to nothing and then run the second loop to get the carbs. I do not do this, excel will try to copy two non-connecting ranges, it doesn't like that.

    I ran this as a stand alone sub and it also produced the Object variable or With block variable not set error

    Please Login or Register  to view this content.

  15. #15
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA To copy cells in a range that contain data skipping formulas

    I really do not know with out seeing the sample data, but it might be this line

    Rng.Select

    This could be your code does not find any value, so nothing to select hence for the error.

    You could change it to
    Please Login or Register  to view this content.
    We then go back to square one as to why the code is not working. Please attach a sample to short the circular journey

  16. #16
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: VBA To copy cells in a range that contain data skipping formulas

    I think I see what is happening here. when I step into the code, the
    Please Login or Register  to view this content.
    line returns true even though the cell contains the data that is the result of the formula therefore skipping the rest of the code

    Please Login or Register  to view this content.
    this would explain why I was getting the No Cells Were Found error, and possibly the block error seeing as the range would be empty based on the code being skipped.

    This has turned into being a way more complex issue that what I had thought it was. I also used I.E. rather than edge on this reply, which does seem to let me attach the book, the whole file should now be on here.
    Attached Files Attached Files

  17. #17
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA To copy cells in a range that contain data skipping formulas

    With out looking much further, I can see all the rows in column N have formulas, so the code will skip all rows and will not find any cell which is fixed (Does not have formula). It should work on column K.
    Last edited by AB33; 04-16-2016 at 04:14 PM.

  18. #18
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: VBA To copy cells in a range that contain data skipping formulas

    Right, that is what I was trying to figure out, how to only copy the cells that have a product from the formula but not copy the ones that do not have a product (result). Does VBA have the ability to do this, so far, it looks like that answer is no.

  19. #19
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA To copy cells in a range that contain data skipping formulas

    I am not sure I understand what you are after. The reverse would be to copy cells which have formulas only.

    You can change this line

    Please Login or Register  to view this content.
    INTO

    Please Login or Register  to view this content.
    Or change these line in to true
    Please Login or Register  to view this content.
    INTO

    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: VBA To copy cells in a range that contain data skipping formulas

    With the sheet the way it is now, what I want this to do is to copy N4:N6 as these are the only lines where an item (column K) exist. In each of the changes above, the entire range of N is selected (N4:N8)

    what I may need to do is set the range to only go to the last row that contains data in a range of the K column.

  21. #21
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA To copy cells in a range that contain data skipping formulas

    You need to add another if statements

    If cell.Offset(, -3) <> "" Then
    Please Login or Register  to view this content.

  22. #22
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: VBA To copy cells in a range that contain data skipping formulas

    VIOLA' PERFECT.

    Thank you so much. This works like a charm, awesome.

+ 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. Data Range Manipulations Skipping over unwanted cells.
    By Slurry Pumper in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-05-2016, 03:50 PM
  2. Copy Range of Formulas to a Variable Range of Cells
    By xace in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 09-07-2015, 10:28 PM
  3. copy range from one sheet to another and then copy 5 times skipping ten rows
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-13-2015, 01:35 PM
  4. [SOLVED] Copy a range skipping formulas when pasting
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 02-08-2015, 04:50 PM
  5. Dragging Formulas - Skipping Referenced Cells
    By markpratt in forum Excel General
    Replies: 2
    Last Post: 11-03-2011, 10:03 AM
  6. Save ufrm info to db skipping cells with formulas
    By zebra in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-07-2009, 11:01 AM
  7. formulas skipping empty cells
    By Jocote46 in forum Excel General
    Replies: 1
    Last Post: 05-06-2009, 04:09 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