+ Reply to Thread
Results 1 to 5 of 5

I can't get the code to copy the formula, only the values.

  1. #1
    Registered User
    Join Date
    10-13-2015
    Location
    Washington, USA
    MS-Off Ver
    2013
    Posts
    3

    I can't get the code to copy the formula, only the values.

    Hello everyone. I have a sheet that populates a portion of another sheet based on criteria in one of the columns. The code I am using is based (almost exactly) off of a bit another user posted on here and it has worked fantastically. That is, except for when it comes to copying the formulas present in the rows being copied. It copies the values, but not the formulas. The reference locations are the same on both sheets, but the values in some of the reference cells change so it leads to incorrect numbers. Below is the code. I tried modifying it myself in various ways but have failed pretty universally. Any help is greatly appreciated!

    Option Explicit

    Private Sub Worksheet_Activate()

    Dim LR As Long

    Worksheets("SheetC").Range("A26:z100").Clear 'clear existing data

    With Sheets("SheetI")
    .AutoFilterMode = False 'remove any prior filtering
    .Rows(25).AutoFilter 'activate autofilter
    .Rows(25).AutoFilter 3, "=1" 'filter column C for 1
    LR = .Range("B" & .Rows.Count).End(xlUp).Row 'is any data visible?
    If LR > 1 Then
    .Range("A26:H" & LR).Copy Range("A26") 'copy any data visible to report
    Else
    Range("A26") = "no data found" 'if none, give that message
    End If
    .AutoFilterMode = False 'turn off autofilter
    End With

    End Sub



    Edit: Formulars are found in Column H rows 26 - 44 (rows differ based on autofilter)

  2. #2
    Forum Contributor Obsessed's Avatar
    Join Date
    05-22-2013
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 365
    Posts
    215

    Re: I can't get the code to copy the formula, only the values.

    I'd try separating out the worksheets first. Neither of your sheets are dimmed so I'd be wondering if the Range.Copy command isn't functioning as you'd expect due to that...it will copy to whatever Range is in the activesheet. Just bad practice. Also, make sure your wrap your code in code tags. I'd recommend uploading a copy of the workbook you're working in so users can bet a better understanding of what you're doing.
    Want to show appreciation for the help you received from a member? Give them reps by clicking the bottom left of their post!

  3. #3
    Registered User
    Join Date
    10-13-2015
    Location
    Washington, USA
    MS-Off Ver
    2013
    Posts
    3

    Re: I can't get the code to copy the formula, only the values.

    Thanks for the reply Obsessed!

    Bad form is what I do! I'm pretty amateur and usually just modify existing code to make it work for what I need. Writing my own turns into a debug nightmare. Someday I'll learn. Anyway, now attached is a sanitized version of my workbook. Copying into the active sheet is fine for this purpose as the two sheets are almost identical. If you have a better method I am, of course, open to it.

    Thanks again for your help!

    Test.xlsm

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: I can't get the code to copy the formula, only the values.

    Since it is part of a worksheet_activate event you technically don't need to declare the sheet (since the respective sheet will be the active sheet). However, as mentioned it is still good practice. Change this line:

    Please Login or Register  to view this content.
    to these two lines:
    Please Login or Register  to view this content.
    Does that accomplish what you want?
    If you are happy with my response please click the * in the lower left of my post.

  5. #5
    Registered User
    Join Date
    10-13-2015
    Location
    Washington, USA
    MS-Off Ver
    2013
    Posts
    3

    Re: I can't get the code to copy the formula, only the values.

    That works! Thank you so much for helping a noob!

+ 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] Vba Code to copy the formula file as values and save it another location
    By Masa1989 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-05-2015, 07:30 AM
  2. [SOLVED] VB Code to Copy Range without formula and values only
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-04-2015, 12:04 PM
  3. Code to copy values to formula's in other sheets
    By Miehjs in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-03-2015, 07:50 PM
  4. vba copy/paste code don't copy the new values...
    By zois in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-10-2015, 01:44 PM
  5. Replies: 2
    Last Post: 07-19-2013, 02:54 PM
  6. VBA Code to Copy Non Blanks/Cells with Values until Formula returns as blank
    By FLani in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-16-2013, 10:18 PM
  7. Code to write formula in 2 cells, copy, paste values, repeat next row until end of sheet
    By Oly Steel Man in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-14-2013, 11:13 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