+ Reply to Thread
Results 1 to 10 of 10

Copying all rows that meet a criteria to another worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    08-16-2012
    Location
    Auckland New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    15

    Copying all rows that meet a criteria to another worksheet

    I am trying to copy entire rows that have a value in column A from an entry worksheet - starts at A10 (Sheet1) to the next empty line in a sales record worksheet(Sales), then clear the entry worksheet. I am very new to VBA coding. I also have a hidden column in the entry worksheet with contains a TODAY() Forumla the actual result needs to also be copied - in fact there are a number or formula in the entry worksheet. Both appeared to be working this morning however as I have renamed sheets - and modified the names in the formula while working on other things today I seem to have lost all but the first row when I run the marco. Can any one point out where I am going wrong please: Code as follows:

    Sub CopyRowC()
    
        Dim ws As Worksheet
        Dim r As Range
    
        Set ws = Worksheets("Sales")
        Set r = Range("A10", Range("A65536").End(xlUp))
    
        For Each c In r.Cells
            If c <> "" Then
                c.EntireRow.Copy Destination:=ws.Range("A65536").End(xlUp).Offset(1, 0)
          Cells.Select
    Selection.SpecialCells(xlCellTypeConstants, 1).Select
    Selection.ClearContents
        
       End If
         Next
        MsgBox "Thankyou the sales records have been updated"
        
        
    End Sub
    Sorry if I have posted this wrong - not sure how to load the code differently than this - Like I say I am new at this I bit off a little more than I can chew but need to get the workbook working. Yes I did save three different copies but the error has occured prior to the first save.

    I am sure the answer is staring me in the face -
    Thanks
    Kazza
    Last edited by arlu1201; 08-17-2012 at 04:31 AM.

  2. #2
    Registered User
    Join Date
    08-16-2012
    Location
    Auckland New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Copying all rows that meet a criteria to another worksheet

    I Have found where the problem lies - it is clearing the contents of the other rows prior to copying - if I delete the selection.special cells etc from the code it pastes each relevant row. Can someone please point me to the coding I should use in the same macro to clear the rows after all have been copied or can i lead one marco into another? Like I say I have little knowledge.

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Copying all rows that meet a criteria to another worksheet

    Try this code
    Sub CopyRowC()
    
        Dim ws As Worksheet
        Dim r As Range
        Dim lrow As Long
        Dim c As Range
        
        Set ws = Worksheets("Sales")
        lrow = Worksheets("Entry").Range("A" & Rows.Count).End(xlUp).Row
        Set r = Worksheets("Entry").Range("A10:A" & lrow)
    
        For Each c In r.Cells
            If c <> "" Then
                c.EntireRow.Copy Destination:=ws.Range("A65536").End(xlUp).Offset(1, 0)
                c.EntireRow.ClearContents
            End If
        Next c
        
        MsgBox "Thankyou the sales records have been updated"
            
    End Sub
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Copying all rows that meet a criteria to another worksheet

    Welcome to the forum.

    I have added code tags to your post. As per forum rule 3, you need to use them whenever you put any code in your post. Please add them in future. If you need more information on how to use them, check my signature.

  5. #5
    Registered User
    Join Date
    08-16-2012
    Location
    Auckland New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Copying all rows that meet a criteria to another worksheet

    Will this not clear all my forumlas to? I need thm to stay there.

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Copying all rows that meet a criteria to another worksheet

    So which are the cells / columns you want to be cleared?

  7. #7
    Registered User
    Join Date
    08-16-2012
    Location
    Auckland New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Copying all rows that meet a criteria to another worksheet

    Thanks Arlette
    I have solved the problem - added Next, repeated the if statment and then the Special Cell selection and it works. Problem I have now though that I did not realise is that the col with the TODAY() formula is copying the formula and not the result. Thus it updates to today for every record in the Sales Records worksheet. I "think" I need to remove the col/forumla off the sales input sheet and instead add something to the code to get it to insert the Value of Today() to each line just pasted.

  8. #8
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Copying all rows that meet a criteria to another worksheet

    You can try this - change the column as per your sheet -
    ws.Range("P" & rows.count).end(xlup).offset(1,0).value = date

  9. #9
    Registered User
    Join Date
    08-16-2012
    Location
    Auckland New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Copying all rows that meet a criteria to another worksheet

    Thank you so much
    That works perfectly - can I confirm that this is a date stamp and tommorrow the date on today's entries won't change? But tommorrows will de displayed for tommorrows entries?

    Kazza

  10. #10
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Copying all rows that meet a criteria to another worksheet

    Yes, the date will be input as a value and not as a formula, so it wont change.

+ 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