+ Reply to Thread
Results 1 to 5 of 5

command button vba to copy formula from cell above

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    command button vba to copy formula from cell above

    Hi,
    I have sheet1 having two formulas.
    Ideally I can copy and paste the formula to the entire column,
    However this then populates , say 3000 cells unnecessarily which slows down excel performance or increases size of the file.

    I am looking for a vba ( this is used to open userform)
    To include copy formula from filled cell above to next empty row
    ( cells will be in column K & L)

    So every-time userform is opened, the formula is copied to next empty row.

    Any help please

    kind regards
    Private Sub CommandButton1_Click()
    UserForm1.Show
    'Include here to copy and paste
    End Sub

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: command button vba to copy formula from cell above

    something like
    UserForm1.Show
    'Include here to copy and paste
    Dim WS As Worksheet
    Dim NextEmptyRow As Long
    
    Set WS = ActiveWorkbook.Worksheets(1)
    
    With WS
        NextEmptyRow = .Cells(.Rows.Count, "K").End(xlUp).Row + 1
        
        .Range("K" & NextEmptyRow).Formula = "=sum(A" & NextEmptyRow & " + B" _
            & NextEmptyRow & ")"
    
        .Range("L" & NextEmptyRow).Formula = "=sum(A" & NextEmptyRow & " + B" _
            & NextEmptyRow & ")"
    End With
    David
    (*) Reputation points appreciated.

  3. #3
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    Re: command button vba to copy formula from cell above

    Hi Tinbendr,
    Thanks for your reply.
    I tried the code , but it does not seem to work.
    May be I am making a mistake somewhere.

    The formula I am using are ( in K say row 9)
    =IF(A9="","",IF(H9=1,A9+1/12,IF(H9=2,A9+1/4,IF(H9=3,A9+1,IF(H9=4,A9+5,IF(H9=5,A9+28,"Urgency?"))))))
    In L ( say in row 9 again)
    =IF(A9="","",IF(K9<X9,"past PDC",""))
    Can you guide me further please.
    I have clicked * to thank you for offering me your valuable help.
    Kind regards

  4. #4
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: command button vba to copy formula from cell above

    Uhhh, OK, plan B

    Dim WS As Worksheet
    Dim NER As Long
    
    Set WS = ActiveWorkbook.Worksheets(1)
    
    With WS
        NER = .Cells(.Rows.Count, "K").End(xlUp).Row
    
        .Range("K" & NER).Copy
        .Range("K" & NER + 1).PasteSpecial xlPasteFormulas
        
        .Range("L" & NER).Copy
        .Range("L" & NER + 1).PasteSpecial xlPasteFormulas
        
        Application.CutCopyMode = False
    End With

  5. #5
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    Re: command button vba to copy formula from cell above

    Hi TinBendr,
    Perfect solution.
    Much appreciated.
    Once again thanks for a great solution.
    Kind regards

+ 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. copy cell value with addition of desired number with command button control
    By mubashar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-09-2014, 01:33 PM
  2. [SOLVED] macro for command button to copy cell content to other cells
    By Emma_B in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-19-2013, 06:32 AM
  3. copy and paste data from one worksheet cell to another using command button
    By jasonruiz in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-13-2011, 04:34 AM
  4. Copy cell value to list with command button click
    By ArtZ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-20-2008, 02:48 PM
  5. Command Button to copy conetents of one cell to another
    By Gazzauk2000 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-20-2005, 12:10 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