+ Reply to Thread
Results 1 to 10 of 10

Copy a row to a separate sheet with a condition

Hybrid View

  1. #1
    Registered User
    Join Date
    07-03-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    24

    Copy a row to a separate sheet with a condition

    Hi guys

    Requesting your guidance to copy the cells (B:I and K) in each row in sheet " 7 Jan 14 " to cells B:J in sheet " 14 Jan 14 " when the text in K is " Still In Care", which is the condition. Please note the condition in "K" in sheet " 7 Jan 14 " is not copied to "K" in sheet " 14 Jan 14 " but it should be copied in to cell J instead. Likewise when the text in K is " Still In Care", when entered in to sheet " 14 Jan 14 ", the cells (B:I and K) should be copied to the next sheet which is " 21 Jan 14 ".


    I will have 52 sheets which indicates the 52 weeks for the year 2014. The above said code should be applied to all the 52 sheets. ( The attached excel book has got only 2 sheets.I am more than happy to upload a complete work book with 52 sheets if that helps.)

    Looking forward for your valuable guidance.

    Thanks

    Malitec
    Attached Files Attached Files

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Copy a row to a separate sheet with a condition

    Would you mind put some data in your sheets so we see what you want to be copied? I suppose you only want to copy rows starting at row 9 and below.
    Also, what do you mean by "the code should be applied to all 52 sheets"? Can you detail your process a little bit more?
    Regards
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

  3. #3
    Registered User
    Join Date
    07-03-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Copy a row to a separate sheet with a condition

    Hi p24leclerc

    Apologies for not giving the details. As you said I have to copy rows from 9 onwards only.

    I have uploaded a new workbook with data. As an example I have manually copied the data from sheet "7 Jan 2014" and paste it in "14 Jan 2014". You will notice the text in cell "K" of "07 Jan 2014" worksheet is copied to cell "J" in "14 Jan 2014". Rest of the data from "04 Jan 2014" are copied in to the same cells in "14 Jan 2014".

    My final work book will have 52 work sheets, one for each week of the year. That is why I said when you write the code please ensure the VBA can be applied to all 52 sheets.

    Hope I am clearer this time

    Regards

    Malitec
    Attached Files Attached Files

  4. #4
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Copy a row to a separate sheet with a condition

    here is a macro you could try. It could be run from any sheet and will copy data on a sheet's name defined by the actual sheet name (which should be a date in a format suitable for the DateValue function) with 7 days added to it.

    Please note that I had to reposition a cell in column B into column C because the macro uses a function to locate the last entry in column B and that this TITLE was in the way. You can place it where you want except in column B.

    Public Sub Transfer_Data()
    Dim WS_Name As String, wsSheet As Worksheet
    Dim D_ate As Date, New_Date As Date, WS2_Name As String, C_ell As Range
    Application.ScreenUpdating = False
    WS_Name = ActiveSheet.Name
    D_ate = DateValue(WS_Name)
    'Next sheet name will be 7 days later
    New_Date = D_ate + 7
    'Here is where you define the new sheet name
    WS2_Name = Day(New_Date) & " " & Left(MonthName(Month(New_Date)), 3) & " " & Year(New_Date)
    'Checking if next sheet exists
    On Error Resume Next
    Set wsSheet = Sheets(WS2_Name)
    On Error GoTo 0
    If wsSheet Is Nothing Then
       MsgBox "Sheet " & WS2_Name & " does NOT exist"
       Application.ScreenUpdating = True
       Exit Sub
    End If
    'Starts copying data to next sheet
    For Each C_ell In Range("B9", Cells(Rows.Count, 2).End(xlUp))
       If C_ell.Offset(0, 9) = "Still in Care" Then
          C_ell.Resize(1, 8).Select
          Selection.Copy
          Sheets(WS2_Name).Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial
          Sheets(WS2_Name).Cells(Rows.Count, 2).End(xlUp).Offset(0, 8) = "Still in Care"
          C_ell.Resize(1, 1).Select
       End If
    Next
    Application.ScreenUpdating = True
    End Sub
    Hope this help
    Attached Files Attached Files
    Last edited by p24leclerc; 10-08-2014 at 11:54 AM.

  5. #5
    Registered User
    Join Date
    07-03-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Copy a row to a separate sheet with a condition

    Hi

    Thanks for the macro. Also please accept my apologies for the delayed reply.

    I have tried the macro but it seems something is preventing it from giving me the results. I am not at all good in macros. Please check the macro I have added to this work book.

    I have attached a 52 sheet workbook as a zip file for your perusal. Would you mind checking the macro in this work book and advice me where I went wrong.

    Thank you

    malitec
    Attached Files Attached Files

  6. #6
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Copy a row to a separate sheet with a condition

    Hi,
    First thing is to put this macro in a Module. Actually it is at the sheet level and this means it is not available to the other sheets. You want to place it in a Module (any one) so that you will be able to call it form anywhere in the workbook. DO NOT FORGET TO ERASE IT AT THE SHEET LEVEL.

    Secondly, the macro was modified and you made some mistake doing it. Here is a version that works in your workbook:
    Public Sub Transfer_Data()
    Dim WS_Name As String, wsSheet As Worksheet
    Dim D_ate As Date, New_Date As Date, WS2_Name As String, C_ell As Range
    Application.ScreenUpdating = False
    WS_Name = ActiveSheet.Name
    D_ate = DateValue(WS_Name)
    New_Date = D_ate + 7
    WS2_Name = Day(New_Date) & " " & Left(MonthName(Month(New_Date)), 3) & " " & Year(New_Date)
    
    For Each C_ell In Range("B9", Cells(Rows.Count, 2).End(xlUp))
      If C_ell.Offset(0, 9) = "Still In Care" Then
          C_ell.Resize(1, 8).Select
          Selection.Copy
          Sheets(WS2_Name).Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial
          Sheets(WS2_Name).Cells(Rows.Count, 2).End(xlUp).Offset(0, 8) = "Still In Care"
          C_ell.Resize(1, 1).Select
      End If
    Next
    Application.ScreenUpdating = True
    End Sub
    There were 2 main errors:
    1. The variable name WS2_Name was changed to WS2_ "14 jan 2014"
    2. there was an "End IF" statement left when you deleted some part of the macro.

    I saw you deleted the part that is checking if the next week sheet exists. It's up to you to do so but if for some reason the macro does not find the next week sheet, you'll get an error meesage and it will not as obvious as the message box the macro was displaying when it happens. I'd rather leave it there.
    Hope this helps.

  7. #7
    Registered User
    Join Date
    07-03-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Copy a row to a separate sheet with a condition

    Hi

    I tried but failed to get the result. And I am unable to work out the issue that hindering the macro function.

    If time permits, could you please try the macro in to the 52 weeks work book (zip file) I have attached earlier and let me know the problem.

    Thanks in advance

    Regards

    malitec

  8. #8
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Copy a row to a separate sheet with a condition

    Here is your workbook with the macro in Module1. It workslike this:
    When in any sheet, you run the macro named Transfer_Data which will take the sheet name, convert it in a date, add 7 days to this data and come up with the next sheet name. It then scan all rows in the active sheet and copy those that have "Still In Care" in column K to the next sheet as required.
    Hope this will work for you. If not, then ask again for help.
    Regards
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    07-03-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Copy a row to a separate sheet with a condition

    Hi

    Could not log on to the forum due to some personal issues. Thanks for the reply. May be because of my inexperience with VB the macro is not working. Anyway thanks a lot for your effort. If time permits please have a second look at the request.
    Thanks

  10. #10
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Copy a row to a separate sheet with a condition

    Check if your Excel is configured to accept to run macros. By default, it is not. You have to change it in the Files/Options/Trust Center/Trust Center Settings/Macro Settings menu.

+ 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, Intercept 2 giving range from 2 sheet,If condition, intercepted copy to 3rd sheet
    By david gonzalez in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-20-2014, 09:32 PM
  2. Getting rows from a separate sheet that matches a condition
    By sleepy777 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 03-11-2014, 11:41 PM
  3. Replies: 1
    Last Post: 09-17-2013, 01:38 PM
  4. [SOLVED] Copy rows from active sheet to another sheet (last row) if condition in column X is met
    By mortless in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-17-2013, 08:11 AM
  5. [SOLVED] VBA for separate sheet with particular condition
    By santosh226001 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-22-2012, 08:18 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