+ Reply to Thread
Results 1 to 18 of 18

VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

Hybrid View

  1. #1
    Registered User
    Join Date
    10-07-2016
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    11

    VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    Hi All,


    I've struck upon what seems to be impossible to me.

    So here I have the main calendar of employees and their availability.

    ROTA.PNG

    Here we have a list of employees and what's their availability throughout the year.

    What I'm trying to do is to "extract" only those who are available (Active) on a specific day and be able to see the row (up to, but not including Column I) on a separate sheet.

    This is the code that I'm using at this point.

    Code.PNG

    As you can probably already tell, the code copies an entire row, which is not desired, however it successfully tracks the "Active" people and places them in a separate sheet. Nonetheless, the goal is to have the process as automated as possible.

    In the "daily" sheet where all the "active" staff will be located they will also have hourly tasks allocated next to their names, and therefore once the data is "extracted" it overwrites all the useful stuff in the "daily" sheet.

    How can I work around this and possibly automate the process - so that when changes are made in the "yearly" sheet would automatically reflect in the "daily" sheet?


    Many thanks everyone.

    SS

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,927

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    (How to) Upload a Workbook directly to the Forum
    (please do not post pictures or links to worksheets)
    • Click Advanced next to Quick Post button at the bottom right of the editor box.
    • Scroll down until you see "Manage Attachments",
    • Click the "Choose" button at the upper left (upload from your computer).
    • Select your file, click "open", click "upload"
    • Once the upload is completed the file name will appear below the input boxes in this window.
    • Close the Attachment Manager window.
    • Click "Submit Reply"
    Note: Please do not attach password protected workbooks/worksheets
    Ensure to disable any Workbook Open/Autorun macros before attaching!
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    10-07-2016
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    11

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    Hello,

    Many thanks for your reply.

    My file exceeds the max upload size (it's at 3mb).


    Regards,
    SS

  4. #4
    Registered User
    Join Date
    10-07-2016
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    11

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    Sorry for double post. I've shrunk down the info to the most basic - no code or formulas, and am now able to upload the file


    In the file you will see the main sheet - Sheet 1, which contains information (diaries) of individuals. Their availability is marked in this sheet for the entire week.

    Now, if you go to sheet Sunday, you will see a slightly different list of individuals, same goes for sheet Monday, and that is because I've manually copied and pasted individuals who are available on that day and their availability times from the main sheet and pasted the info on to the relevant day sheet.

    What I want VBA to do is to identify who's "Active" on Sheet1 and transfer (extract) specific information (as shown in sheets Sunday and Monday) and their availability hours for that day into another sheet, or in other words automate the manual process that I've described above.


    Thank you all for having a look at this.
    Attached Files Attached Files
    Last edited by SolidSmoke; 10-08-2016 at 03:00 AM.

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

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    This one macro will do it on any sheet that is active when you run the macro. I've changed the name of your main sheet from Sheet1 to Availability. I also tweaked your headers slightly for appearance.

    Option Explicit
    
    Sub DailyAvailability()
    Dim wsMAIN As Worksheet, wsME As Worksheet, meFIND As Range, LR As Long
    
    Set wsMAIN = Sheets("Availability")             'the sheet with the hours
    Set wsME = ActiveSheet                          'the currrent sheet to update
    
    On Error Resume Next
    With wsMAIN
        LR = .Range("A" & .Rows.Count).End(xlUp).Row        'find the last row of availability date
        Set meFIND = .Rows(2).Find(wsME.Name, LookIn:=xlValues, LookAt:=xlWhole)    'find the current day
        If Not meFIND Is Nothing Then                       'if sheetname/day is found, proceed
            wsME.UsedRange.Clear                            'clear prior info
            .AutoFilterMode = False                         'remove prior filters
            .Rows(3).AutoFilter meFIND.Column, "Active"     'filter daily column for "active" only
            .Range("A2:F" & LR).Copy wsME.Range("A2")       'copy name info, then copy hours
            .Range(.Cells(2, meFIND.Column - 2), .Cells(LR, meFIND.Column - 1)).Copy wsME.Range("G2")
            .AutoFilterMode = False
            Application.CutCopyMode = False
            wsME.Columns.AutoFit
        End If
    End With
    
    End Sub
    Attached Files Attached Files
    _________________
    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!)

  6. #6
    Registered User
    Join Date
    10-07-2016
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    11

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    Thank you JBeaucaire. This actually works very nicely. I can edit the code to run the transfer from the main sheet.
    However, the main sheet will also include availability for next week and possibly for the whole year in advance, how would I tweak the code (and make a separate macro) for the next week etc?

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

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    Couldn't say. The suggestion given matched the data available in the sample workbook.

  8. #8
    Registered User
    Join Date
    10-07-2016
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    11

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    Hm, I see.

    Say for instance the user wants to extract weekly availability from the main sheet (I've added next week's availability), so for example the main sheet will have command buttons that will extract specific weeks.
    As an example, we have "Week 45" and "Week 46" groups in the main sheet and the user first extracts availability for Week 45. Once Week 45 is coming to an end, the user would then extract availability for Week 46, which would replace information in the daily sheets (Sunday Week 45 availability would then get replaced by Sunday Week 46 availability).

    Also, I imagine any updates in the main sheet, say for instance someone decides to become unavailable on Monday of whichever week we're working with and then the user extracts the updated data, will the code replace the old data with new data?
    Attached Files Attached Files

  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: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    I cant seem to open that file.

  10. #10
    Registered User
    Join Date
    10-07-2016
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    11

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    Sorry, try changing it to .xlam

  11. #11
    Registered User
    Join Date
    10-07-2016
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    11

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    My apologies once again. Not sure why Excel saved it in .xlam but I've changed to .xlsm just to upload it. Anyway, the working workbook is attached below.
    Attached Files Attached Files

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

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    OK, added some new features.

    1) The DAILYAVAILABILITY Macro now looks at cell C1 on each sheet from which it is called to see what "week #" you have selected to display.
    2) C1 has a drop down listing all the Week #s currently contained on the Availability sheet
    3) The ThisWorkbook module now has a "SheetActivate" macro that updates the drop down list in realtime anytime you activate a sheet. So the drop down will always be current for the sheet you are reviewing.
    4) If the current value of C1 is NOT a week that is found on the Availability sheet any longer, the sheet automatically run the update macro to display the first week it does find. After that, you can select any other week from the C1 drop down and UPDATE it again.

    ThisWorkbook code:
    Option Explicit
    
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Dim buf As String, cell As Range
    
    If Sh.Name <> "Availability" Then
        With Sheets("Availability")
            For Each cell In .Rows(1).SpecialCells(xlConstants)
                If cell.Value <> "" Then
                    If buf = "" Then
                        buf = cell.Value
                    Else
                        buf = buf & "," & cell.Value
                    End If
                End If
            Next cell
        End With
        If buf <> "" Then
            With Sh.Range("C1").Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=buf
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .ShowInput = True
                .ShowError = True
            End With
            Range("C1").Activate
            If InStr(buf, Range("C1").Value) = 0 Then
                If InStr(buf, ",") = 0 Then
                    Range("C1") = buf
                Else
                    Range("C1") = Left(buf, InStr(buf, ",") - 1)
                End If
                Call Module1.DailyAvailability
            End If
        End If
    End If
    
    End Sub
    Updated DailyAvailibilty code in module1:
    Option Explicit
    
    Sub DailyAvailability()
    Dim wsMAIN As Worksheet, wsME As Worksheet, meFIND As Range, LR As Long
    Dim wkFIND As Range
    
    Set wsMAIN = Sheets("Availability")             'the sheet with the hours
    Set wsME = ActiveSheet                          'the currrent sheet to update
    
    On Error Resume Next
    With wsMAIN
        Set wkFIND = wsMAIN.Rows(1).Find(wsME.Range("C1").Text, LookIn:=xlValues, LookAt:=xlWhole).MergeArea.Cells
        LR = .Range("A" & .Rows.Count).End(xlUp).Row        'find the last row of availability date
        Set meFIND = Intersect(.Range(wkFIND.Address).EntireColumn, .Rows(2)).Find(wsME.Name, _
                       LookIn:=xlValues, LookAt:=xlWhole)   'find the current day
        If Not meFIND Is Nothing Then                       'if sheetname/day is found, proceed
            wsME.UsedRange.Offset(1).Clear                  'clear prior info
            .AutoFilterMode = False                         'remove prior filters
            .Rows(3).AutoFilter meFIND.Column, "Active"     'filter daily column for "active" only
            .Range("A2:F" & LR).Copy wsME.Range("A2")       'copy name info, then copy hours
            .Range(.Cells(2, meFIND.Column - 2), .Cells(LR, meFIND.Column - 1)).Copy wsME.Range("G2")
            .AutoFilterMode = False
            Application.CutCopyMode = False
            wsME.Columns.AutoFit
        End If
    End With
    
    End Sub
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    10-07-2016
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    11

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    Holy sheet!

    That's amazing!

    Is there a way to have the drop down on Availability sheet?

    I'm sure if I make a separate call macro that will activate each day sheet and call for DailyAvailability macro I will be able to transfer all week info with a click of a button!
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    10-07-2016
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    11

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    There's another problem, however, which I think is related to the UsedRannge.Clear command.

    For example, if I plan daily activities for the day and someone calls in sick or decides to change their availability time, I update the information on the Availability sheet and once I extract the updated data for whatever day, then everything else gets deleted, which is not very convenient in case if I've had daily tasks allocated to other people etc etc.

    Is there a way to work around this?

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

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    EEK. You want a macro to put what could be randomized information from the Availability sheet into a Daily sheet, then you are going to manually add data to the Daily sheets? You see how out-of-sync that will get with even the smallest "update"?

  16. #16
    Registered User
    Join Date
    10-07-2016
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    11

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    Ok let me try to explain it this way.

    The availability sheet has all these people and their availability throughout the year. Let's assume that we plan only one week ahead (thus why we extract only one week at a time, and once we're done with it, we extract the next one). The daily availability sheets (Mo, Tue, Wed, etc) will show the availability of those people in hours. For instance, even though they're available on the main calendar, doesn't mean that they're available for full 24 hours, thus the need for hourly availability but without flooding the data with those who are completely unavailable on that day.

    Let's assume a couple of more things.
    1) The daily availability will only be amended on the main sheet
    2) The hourly availability will only be amended on the day sheet.

    For instance, I'm starting to plan my next week, which starts on the 30/10/2016. I extract the data. I get all the available people separated from whoever is on a holiday or is unavailable for any other reason.
    Now that I have a group of people to work with, I allocate hourly tasks for them so I can plan the daily operations etc.
    Suddenly, one of the people who is supposed to be available calls in sick. I amend their availability on the main sheet. Extract (update) the data again. Now, I understand that there will be 1 row less on the day sheet and I know that I'll have to delete/remove whatever I've had planned for that person and I am fine with that. I don't think that there is a way to automate something like that.

    So all in all. The main sheet is the one that updates the daily sheets, not the other way around. The only other amendments to the daily sheets are manual and only to fix the "update" that came in from the main sheet. But the way the code works right now is that any update from the main sheet will also delete the rest of the sheet and any information with it, which means that any small update will result in having to do all the hourly allocations all over again.

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

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    I understood all that implicitly. The dangers of what you're doing is what I'm forcing a conversation about.

    In terms of process, the current new design will only change the sheet, wiping out your notes, if you click the UPDATE button.
    Were I forced into this design/process, I'd never click the UPDATE button again on a sheet I'd already starting the final planning as you're describing. Once that had begun, someone calling in sick, so to speak, I would change their availability right on the daily sheet. I'd never use the Update at all once I'd started manual notations.

  18. #18
    Registered User
    Join Date
    10-07-2016
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    11

    Re: VBA Macro: Copy and Paste Specific Row Range based on the Specific Value in a Cell.

    I see. I think that I can work around that. Thank you for clarifying.

    Is there a way to have the extraction run from the main sheet and for the entire week with just 1 button?

    Many thanks, friend.

+ 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. macro to copy range hidden paste in a specific location
    By graiggoriz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-26-2014, 11:27 AM
  2. [SOLVED] Macro to copy specific data from one WB to specific cells in another WB based on specific
    By d_rose in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-26-2014, 11:05 AM
  3. Macro to copy specific line from text file and paste into specific cell in excel
    By keeneye in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-05-2013, 10:35 AM
  4. Copy and paste specific range to date specific range in alternate sheet
    By alanalmarza in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-21-2013, 08:29 AM
  5. macro to copy a range of worksheets based on cell entry and rename specific sheets
    By Lbischoff in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-14-2012, 12:13 PM
  6. HELP! Macro to Copy specific cells from one sheet to another based on specific criteria
    By atriscritti in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-11-2012, 11:05 AM
  7. [SOLVED] Macro - Copy and Paste to a Specific Row based on the input in a cell
    By usc1382 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-22-2012, 05:05 PM
  8. Copy & paste specific column based on cell reference
    By ccsmith in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-26-2011, 03:59 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