+ Reply to Thread
Results 1 to 12 of 12

Help on copy and paste macro to next empty row

Hybrid View

  1. #1
    Registered User
    Join Date
    01-08-2011
    Location
    Bahrain
    MS-Off Ver
    Excel 2003
    Posts
    13

    Help on copy and paste macro to next empty row

    Hi..

    I want to copy the value of cell B21 (if not zero), to next empty cell in column B (after B26)and clear the cells B7:B8.

    Pl. help me with a macro to do the above.

    Thanks
    Last edited by sarath25; 01-09-2011 at 04:48 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Help on copy and paste macro to next empty row

    How will you run the code?

      If Cells(21, 2) <> 0 Then
            Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Value = Cells(21, 2)
            Range("B7:b8").ClearContents
        End If
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    01-08-2011
    Location
    Bahrain
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Help on copy and paste macro to next empty row

    By assigning it as macro to a button.. Thanks

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Help on copy and paste macro to next empty row

    Then add the code that I gave to you to a button on the sheet

  5. #5
    Registered User
    Join Date
    01-08-2011
    Location
    Bahrain
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Help on copy and paste macro to next empty row

    Did, but get an error "Compile error; Invalid outside procedure"

  6. #6
    Registered User
    Join Date
    01-08-2011
    Location
    Bahrain
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Help on copy and paste macro to next empty row

    Pls. note, I have attached the sheet for u,

    On clicking the save button,


    I need the total value of B21 to be copied to the next empty row in after B26 and

    entries in B7:B8 & B16:B20 to be cleared.

    Thanks
    Last edited by sarath25; 01-09-2011 at 04:49 AM.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Help on copy and paste macro to next empty row

    Hello sarath25,

    Welcome to the Forum!

    The following macro has been added to "Save" button in the attached workbook.
    Sub SaveData()
    
      Dim C As String
      Dim Cell As Range
      Dim EmptyCell As Range
      Dim Rng As Range
      Dim Wks As Worksheet
      
        C = "B"
        Set Wks = ActiveSheet
        
        
        Set Rng = Wks.Range(Wks.Cells(26, C), Wks.Cells(42, C))
        
        For Each Cell In Rng
          If Cell = "" Then Set EmptyCell = Cell: Exit For
        Next Cell
        
        If EmptyCell Is Nothing Then
           MsgBox "There are No Empty Cells in Range " & Rng.Address
        Else
           EmptyCell.Value = Wks.Cells(21, C).Value
           Wks.Cells(7, C).Resize(2, 1).ClearContents
           Wks.Cells(16, C).Resize(5, 1).ClearContents
        End If
        
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  8. #8
    Registered User
    Join Date
    01-08-2011
    Location
    Bahrain
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Help on copy and paste macro to next empty row

    Thanks a lot dear, can an If condition added in the beginning to perform the macro only if B21 is not zero.

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Help on copy and paste macro to next empty row

    Just add this near the top:
        C = "B"
        Set Wks = ActiveSheet
        If Wks.Range("B1") = 0 Then Exit Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Help on copy and paste macro to next empty row

    dOES THIS WORK?
    
    Option Explicit
    
    Sub CopyData()
    If Cells(21, 2) <> 0 Then
            Cells(43, 2).End(xlUp).Offset(1, 0).Value = Cells(21, 2)
            Range("B7:b8,b16:b20").ClearContents
        End If
    End Sub

  11. #11
    Registered User
    Join Date
    01-08-2011
    Location
    Bahrain
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Help on copy and paste macro to next empty row

    Thank you all for the kind help... I got it alright..

  12. #12
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Help on copy and paste macro to next empty row

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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