+ Reply to Thread
Results 1 to 12 of 12

Macro – If Equal to Variable, Copy and move to Next

Hybrid View

  1. #1
    Registered User
    Join Date
    01-10-2011
    Location
    Houston, United States
    MS-Off Ver
    Excel 2003
    Posts
    6

    Macro – If Equal to Variable, Copy and move to Next

    I have been unable to locate a solution to the following:

    A workbook with two spreadsheets titled “Sheet 1” and “Sheet 2”. In “Sheet 1”, I have a table that lists employee names, whether or not they are used on the project (Posed with a question and answered with a drop down list of “Yes” or “No”), and charge rates in columns down. I need to transfer the names of employees that are used on the project (“Yes”) to “Sheet 2” in a row format (B3:L3 for example). I need a macro that will check the first value in the specified range in “Sheet 1” of “Yes” and “No”, add the employee name to a new list if the answer is “Yes”, ignore if the answer is “no” and go through the entire range until it reaches the end.

    Any help would be much appreciated.

    Example portion of “Sheet 1”


    Column A Column B Column C

    Name Used on Project? Rate

    Alexander Yes $213.00
    MacNeill No $213.00
    Kirkland No $223.00
    Karpathy Yes $213.00

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro – If Equal to Variable, Copy and move to Next

    You could autofilter col B on Yes, then copy and paste the results to the other sheet.

    If you want to automate that, record a macro, and then post back if you need help cleaning it up.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,359

    Re: Macro – If Equal to Variable, Copy and move to Next

    Hi milissadianna and welcome to the forum,

    I agree with shg from above that it would be easy to do a filter but you asked for a Macro, so here it is. Also find it in the attached file.
    Option Explicit
    
    Sub MoveYesToSheet2()
    Dim Sh1CurrentRow As Double
    Dim Sh1LastRow As Double
    Dim Sh2BlankRow As Double
    
    Sh1LastRow = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
    
        For Sh1CurrentRow = 2 To Sh1LastRow
            If Worksheets("Sheet1").Cells(Sh1CurrentRow, "B").Value = "Yes" Then
                Sh2BlankRow = Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row + 1
                Worksheets("Sheet1").Rows(Sh1CurrentRow & ":" & Sh1CurrentRow).Copy _
                Destination:=Worksheets("Sheet2").Range("A" & Sh2BlankRow)
                
            End If
        Next Sh1CurrentRow
        
    End Sub
    See the attached.
    Attached Files Attached Files
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Registered User
    Join Date
    01-10-2011
    Location
    Houston, United States
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Macro – If Equal to Variable, Copy and move to Next

    Thank you both for your responses. I should have been more clear in my first request. I have attached a spreadsheet now in hopes that it will help better explain what I am trying to achieve.

    In the tab called “2011 Rates”, in Column E there is a drop down list that the user can choose “Yes”, “No”, or “N/A”. If “Yes” is chosen I would like the “Project Title” (Column B) to be shown in Row 3 (C3:….) of the tab called “Workscope_BY_CTR”. Currently there is a drop down list in Row 3 of “Workscope_By_CTR”, I am trying to eliminate the step of having to use the drop down list. I would like to put a button in the “Workscope_By_CTR” tab that will populate the entire Row 3 with unique “Project Titles” that “Yes” has been chose for in the “2011 Rates” sheet. I would also like to avoid any issues that may arise if the user pushes the button twice.

    Thanks again!
    Attached Files Attached Files

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,359

    Re: Macro – If Equal to Variable, Copy and move to Next

    Hi mellisadianne,
    Find the attached with a Private code (behind the worksheet) that will build your list on the second page.

    When you change any of the dropdowns of Yes/No it will cycle through and put the corrent info on your sheet two.

    Hope this gives you a way of getting your job done easier.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    01-10-2011
    Location
    Houston, United States
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Macro – If Equal to Variable, Copy and move to Next

    Thanks for the response again MarvinP. Is it possible to have you paste the code in a post? I think something may be happening in the conversion process because I’m getting some error messages about referencing external formulas and the code is appearing in a “Sheet 18”.

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,359

    Re: Macro – If Equal to Variable, Copy and move to Next

    I have no idea how it got behind sheet18 but here it is..

    This code needs to be behind Sheet 2011 Rates.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim NoProjects As Double
    Dim CurRow As Double
    Dim BLankCol As Double
    
        If Target.Row > 4 Then
            If Target.Column = 5 Then
            Worksheets("Workscope_by_CTR").Range("c3:am3").ClearContents
            NoProjects = cells(5, "E").End(xlDown).Row
                For CurRow = 5 To NoProjects
                    If cells(CurRow, "E").Value = "Yes" Then
                        BLankCol = Worksheets("Workscope_by_CTR").Range("AM3").End(xlToLeft).Column + 1
                        Range("B" & CurRow).Copy Destination:=Worksheets("Workscope_by_CTR").Range(cells(3, BLankCol).Address)
                    End If
                Next CurRow
            End If
        End If
    
    End Sub

  8. #8
    Registered User
    Join Date
    01-10-2011
    Location
    Houston, United States
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Macro – If Equal to Variable, Copy and move to Next

    MarvinP, I really appreciate the help. I have one more question about a macro to complete the spreadsheet. I know that I should be able to figure out how to make a similar code as the previous one that you sent me, but I seem to be having trouble. Similar to the other code, in the same workbook there is a “Summary Tab” that summarizes the amount of hours allocated for each employee, I need the code to extract the name of the employee that has hours in the respective CTR sheets to the sheet titled “CTR-01” in the section titled “Personnel”. The “CTR-01” sheet currently has a drop down list, again I’m trying to eliminate a step in which the user would have to manually chose the name of the “Personnel.” The original spreadsheet has 20 CTR sheets, but I assume that once the code is written it should be easy to include in all the CTR sheets. I have attached another spreadsheet to help my inquiry make more sense.
    Attached Files Attached Files

  9. #9
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,359

    Re: Macro – If Equal to Variable, Copy and move to Next

    Try this code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim NoProjects As Double
    Dim CurRow As Double
    Dim BLankCol As Double
    Dim CRTBlankRow As Double 'New
    
        If Target.Row > 4 Then
            If Target.Column = 5 Then
            Worksheets("Workscope_by_CTR").Range("c3:am3").ClearContents
            
            Worksheets("CTR-01").Range("A38:f53").ClearContents ' New
            
            NoProjects = cells(5, "E").End(xlDown).Row
                For CurRow = 5 To NoProjects
                    If cells(CurRow, "E").Value = "Yes" Then
                        BLankCol = Worksheets("Workscope_by_CTR").Range("AM3").End(xlToLeft).Column + 1
                        Range("B" & CurRow).Copy Destination:=Worksheets("Workscope_by_CTR").Range(cells(3, BLankCol).Address)
                    
                        CRTBlankRow = Worksheets("CTR-01").Range("A36").End(xlDown).Row + 1 'New
                        Worksheets("CTR-01").Range("A" & CRTBlankRow) = Range("B" & CurRow) 'New
                        
                    End If
                Next CurRow
            End If
        End If
    
    End Sub
    It replaces the code from before.

  10. #10
    Registered User
    Join Date
    01-10-2011
    Location
    Houston, United States
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Macro – If Equal to Variable, Copy and move to Next

    Actually, I figured it out. Thank you so much!!!! This will make things a lot easier!

  11. #11
    Registered User
    Join Date
    01-10-2011
    Location
    Houston, United States
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Macro – If Equal to Variable, Copy and move to Next

    MarvinP = I’m wondering if it would be better to have two Macros, a separate one in the “Summary” tab. Currently the macro is listing all the Employee Names in CTR-01, even the ones that don’t actually have hours. So I was wondering about having a macro that went only off of the summary tab, for example if the value in column 3 of the summary tab is greater than 0, then it lists only those names in the CTR-01 tab.

    Thanks again for the help!

  12. #12
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,359

    Re: Macro – If Equal to Variable, Copy and move to Next

    Hi Melissadianna,
    If you record a macro and it does Range("A1").Select type of commands, it is working on the active worksheet. If you run that macro from a different sheet (the one it wasn't meant to run on) it can really mess things up.

    In my code above I was very careful to specify which worksheet was the one being used.

    My concern is that if you run a macro from a key combination like Ctrl-B you need to make sure you are on the correct sheet before letting the macro run.

    This is my concern for making more macros using the recorder and simply calling them.

    I hope that helps.

+ 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