vba for vlookup to paste info onto next line down in next open cell in new workbook

Saoirse

New Member
Joined
Jan 27, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Trying to copy with a vlookup info from the list from workbook "Shrinkage" sheet "Friday" then paste it onto workbook "Productivity tracker" sheet "Allie" on the next row down in columns Y:AD.

here afre the macros i keep trying but neither one seems to pull the info the paste onto the "Allie" spreadsheet on the next line down starting on column Y.
Sub shrinkpull()

Dim NextRow As Integer

NextRow = sheets("Allie").Cells(Rows.Count, 23).End(xlUp).Row + 1

sheets("Allie").Range("Y" & NextRow) = Workbooks("Shrinkage.xlsx").Worksheets("Monday").Range("C4:H4").copy _
("Productivity Tracker.xlsm").Worksheets("Allie").Range("Y & NextRow")

End Sub
Sub vlook()
'
Dim NextRow As Integer

NextRow = sheets("Allie").Cells(Rows.Count, 23).End(xlUp).Row + 1
'
Sheets("Allie").Range("Y" & NextRow)= WorksheetFunction.VLOOKUP([Shrinkage.xlsm](sheets("Friday").Range ("A4"),[Shrinkage.xlsm]sheets("Friday").Range("A3:H32"), 3,FALSE))


End Sub


Shrinkage.xlsm
ACDEFGH
1Enter the minutes worked for each category. EX: 1 hour = 60; 2 hours = 120ComplianceSystem issuesFrontierLetters / Special projects Call pulls, fee calculations, large casesbuzz, trianing, coaching
2
3Alexis
4Allie 601080452030
5Aaron
6 Breonna
Friday


Productivity Tracker.xlsm
ABCDIJMNSTUVWXYZAAABACAD
2DateAdjustments madePrep Cases WorkedTimeDenial ClosuresTimeACETimeApproved ClosureTimeWorldpay SettlementsTimeTotal Minutes Worked % time working casesComplianceSystem issuesFrontierLetters / Special ProjectsCall pulls / fee calculations / large casesbuzz, trianing, coaching
32/1/2022462721617 0114472128864%601080452030
42/2/202236118853551528133918541%
52/3/2022482161611213176892722647%
62/6/202218216321 01040398618%
Allie
Cell Formulas
RangeFormula
W3:W6W3=SUM(D3+J3+N3+T3+V3)
X3:X4X3=W3/450
Y3Y3=VLOOKUP('Z:\Departmental Shares\dOps\POC Training Material\Intake\Lesli Johnson\[Shrinkage.xlsm]Friday'!$A$4,'Z:\Departmental Shares\dOps\POC Training Material\Intake\Lesli Johnson\[Shrinkage.xlsm]Friday'!A$3:H$32,3,FALSE)
Z3Z3=VLOOKUP('Z:\Departmental Shares\dOps\POC Training Material\Intake\Lesli Johnson\[Shrinkage.xlsm]Friday'!$A$4,'Z:\Departmental Shares\dOps\POC Training Material\Intake\Lesli Johnson\[Shrinkage.xlsm]Friday'!A$2:H$32,4,FALSE)
AA3AA3=VLOOKUP('Z:\Departmental Shares\dOps\POC Training Material\Intake\Lesli Johnson\[Shrinkage.xlsm]Friday'!$A$4,'Z:\Departmental Shares\dOps\POC Training Material\Intake\Lesli Johnson\[Shrinkage.xlsm]Friday'!A$3:H$32,5,FALSE)
AB3AB3=VLOOKUP('Z:\Departmental Shares\dOps\POC Training Material\Intake\Lesli Johnson\[Shrinkage.xlsm]Friday'!$A$4,'Z:\Departmental Shares\dOps\POC Training Material\Intake\Lesli Johnson\[Shrinkage.xlsm]Friday'!A$3:H$32,6,FALSE)
AC3AC3=VLOOKUP('Z:\Departmental Shares\dOps\POC Training Material\Intake\Lesli Johnson\[Shrinkage.xlsm]Friday'!$A$4,'Z:\Departmental Shares\dOps\POC Training Material\Intake\Lesli Johnson\[Shrinkage.xlsm]Friday'!A$3:H$32,7,FALSE)
AD3AD3=VLOOKUP('Z:\Departmental Shares\dOps\POC Training Material\Intake\Lesli Johnson\[Shrinkage.xlsm]Friday'!$A$4,'Z:\Departmental Shares\dOps\POC Training Material\Intake\Lesli Johnson\[Shrinkage.xlsm]Friday'!A$3:H$32,8,FALSE)
X5:X6X5=W5/480
I3:I6,S3:S6I3=SUM(E3:H3)
M3:M6M3=SUM(K3:L3)
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I haven't tested it but if you want the formula on the next row of Column Y I am not sure why you are using column 23 (W) to work out the last row.

Try changing this:
VBA Code:
NextRow = sheets("Allie").Cells(Rows.Count, 23).End(xlUp).Row + 1
To this:
VBA Code:
NextRow = sheets("Allie").Cells(Rows.Count, "Y").End(xlUp).Row + 1
 
Upvote 0
Unfortunately that was unsuccessful. I am unable to make the vlookup formulas work within the macro at all. I have tried several different formulas and none seem to work to pull the information and paste to the next empty row of column Y. I am very very new to vba and am sure I look like a fool, but I really want to learn and figure this out.

I have tried all the below scenarios without results.

1)
Dim NextRow As Integer

NextRow = Sheets("Allie").Cells(Rows.Count, "Y").End(xlUp).Row + 1

Sheets("Allie").Range("Y" & NextRow) = Workbooks("Shrinkage.xlsx").Worksheets("Monday").Range("C4:H4").copy _
("Productivity Tracker.xlsm").Worksheets("Allie").Range("Y & NextRow")

End Sub

2)
Dim NextRow As Integer

NextRow = Sheets("Allie").Cells(Rows.Count, "Y").End(xlUp).Row + 1


VLookupResult = WorksheetFunction.VLookup(A4, shrinkage.Monday.A3:H35, 3, False)

3)
Dim NextRow As Integer

NextRow = Sheets("Allie").Cells(Rows.Count, "Y").End(xlUp).Row + 1


Sheets("Allie").Range("Y" & NextRow) = VLookupResult = WorksheetFunction.VLookup(A4, shrinkage.Monday.A3:H35, 3, False)


End Sub

4)
Dim NextRow As Integer

NextRow = Sheets("Allie").Cells(Rows.Count, "Y").End(xlUp).Row + 1


Sheets("Allie").Range("Y" & NextRow) = WorksheetFunction.Vlookup([Shrinkage.xlsm]Monday!$A$4,[Shrinkage.xlsm]Monday!A$3:H$33,3,FALSE)


End Sub

Nothing seems to get the info from one sheet onto the other :(
 
Upvote 0
There was a lot more to that then the question indicated.
Ideally I would get answers to the following questions first but being in a different time zone I will post some code as well.
  • I assume Productitivity Tracker contains a number of people, is this correct ?
    This would mean having to loop through the sheets
  • How many days does the Workbook Shrinkage contain ?
    In the below I have assumed 1 week's worth with the days with sheets named Monday, Tuesday etc
    This means looping through the dates in the Productivity Tracker for each person.
I am assuming at this stage that the workbook Shrinkage is open and that the code is in the workbook Productivity Tracker.
You will need to list the worksheets to exclude (that are not sheets with the names of people to be tracked and joining them with AND in the if statement, replacing the ones I have)

VBA Code:
Sub CopyShrinkage()

    ' Lookups are based on Individual's names in the sheetname
    ' Date using Day of Week as Shrinkage Sheet name
    
    Dim wbProd As Workbook, wbShrink As Workbook
    Dim shtProd As Worksheet, shtShkDay As Worksheet
    Dim prdFirstRow As Long, prdLastRow As Long
    Dim shkFirstRow As Long, shkLastRow As Long
    Dim dtCell As Range, dtCellDay As String
    Dim persName As String, shkPersRow As Variant
    Dim rngShkName As Range, rngShkPers As Range

    Set wbProd = ThisWorkbook
    Set wbShrink = Workbooks("Shrinkage.xlsm")
    
    prdFirstRow = 3
    shkFirstRow = 3
    
    For Each shtProd In wbProd.Worksheets
        ' Test for Names of sheets in Productivity Tracker that are not names of people and need to be excluded
        If shtProd.Name <> "Sample Monday" And _
            shtProd.Name <> "Allie Sample Output" Then
                prdLastRow = shtProd.Range("A" & Rows.Count).End(xlUp).Row
                ' Get Person name from sheet name
                persName = shtProd.Name
                            
                For Each dtCell In shtProd.Range("A" & prdFirstRow & ":A" & prdLastRow)
                    dtCellDay = Format(dtCell, "dddd")
                    Set shtShkDay = wbShrink.Worksheets(dtCellDay)
                    shkLastRow = shtShkDay.Range("A" & Rows.Count).End(xlUp).Row
                    Set rngShkName = shtShkDay.Range("A1" & ":A" & shkLastRow)
                    
                    If Not IsError(Application.Match(persName, rngShkName, 0)) Then
                        shkPersRow = Application.Match(persName, rngShkName, 0)
                        
                        Set rngShkPers = shtShkDay.Range("C" & shkPersRow & ":H" & shkPersRow)
                        shtProd.Range("Y" & dtCell.Row).Resize(, rngShkPers.Columns.Count).Value = rngShkPers.Value
                    Else
                        MsgBox persName & " Not found on " & dtCellDay
                    End If
                
                Next dtCell
        End If
    
    Next shtProd

End Sub
 
Upvote 0
PS: I had issues with some of your names in shrinkage having spaces before/after the names, make sure that is cleaned up before running the code.
 
Upvote 0
OMG WOW! this is waaaay more intense then I imagined!! I am so intimidated by this and didn't realize it would be this much. Sorry anyway, OK to answer your questions,

-The productivity tracker has about 30 tabs with each employee's name on it. Tab "allie" tab "Breonna", tab Courtney" etc I have another macro written to pull info from a tab and put it onto each employees tabs daily.
-The Shrinkage workbook has 5 tabs, "Monday", "Tuesday", "Wednesday", "Thursday" and "Friday" my analysts enter their information in the respective columns on the respective dates, daily. I planned on running the macro to pull the data from the shrinkage chart one day at a time and place it onto the respective analysts' tab in the productivity chart in columns Y through AD, 2-3 times per week to keep it updated.
-Yes, both workbooks are already opened and the code is in the Productivity Tracker.
 
Upvote 0
The code I gave you will look at every date on the employee tab so it will redo previous days as it stands. Since previous days won't change it shouldn't matter that much.
It would be safer if your shrinkage workbook has all 5 days set up.

You might want to comment out the line MsgBox persName & " Not found on " & dtCellDay, I suspect you may get a lot of those and the message box will just get annoying.

You will need to change this line to exclude any non-employee sheets in the Productivity Tracker workbook.
VBA Code:
       If shtProd.Name <> "Sample Monday" And _
            shtProd.Name <> "Allie Sample Output" Then
 
Upvote 0
This error came up

1644513435282.png


Sub CopyShrinkage()

' Lookups are based on Individual's names in the sheetname
' Date using Day of Week as Shrinkage Sheet name

Dim wbProd As Workbook, wbShrink As Workbook
Dim shtProd As Worksheet, shtShkDay As Worksheet
Dim prdFirstRow As Long, prdLastRow As Long
Dim shkFirstRow As Long, shkLastRow As Long
Dim dtCell As Range, dtCellDay As String
Dim persName As String, shkPersRow As Variant
Dim rngShkName As Range, rngShkPers As Range

Set wbProd = ThisWorkbook
Set wbShrink = Workbooks("Shrinkage.xlsm")

prdFirstRow = 3
shkFirstRow = 3

For Each shtProd In wbProd.Worksheets
' Test for Names of sheets in Productivity Tracker that are not names of people and need to be excluded
If shtProd.Name <> "Adjustments" And _
shtProd.Name <> "Compilation" And _
shtProd.Name <> "timing" Then
prdLastRow = shtProd.Range("A" & Rows.Count).End(xlUp).Row
' Get Person name from sheet name
persName = shtProd.Name

For Each dtCell In shtProd.Range("A" & prdFirstRow & ":A" & prdLastRow)
dtCellDay = Format(dtCell, "dddd")
Set shtShkDay = wbShrink.Worksheets(dtCellDay) <- This line was highlighted
shkLastRow = shtShkDay.Range("A" & Rows.Count).End(xlUp).Row
Set rngShkName = shtShkDay.Range("A1" & ":A" & shkLastRow)

If Not IsError(Application.Match(persName, rngShkName, 0)) Then
shkPersRow = Application.Match(persName, rngShkName, 0)

Set rngShkPers = shtShkDay.Range("C" & shkPersRow & ":H" & shkPersRow)
shtProd.Range("Y" & dtCell.Row).Resize(, rngShkPers.Columns.Count).Value = rngShkPers.Value

End If

Next dtCell
End If

Next shtProd

End Sub
 
Upvote 0
That error would indicate that you have a date in the Productivity Tracker Employee Sheet that you don't have a Day of the Week sheet for in your Shrinkage workbook.

For testing purposes, replace this line in the code (the error line)
Set shtShkDay = wbShrink.Worksheets(dtCellDay)

With this code
It should give you a message box telling you where the problem is in the Productivity Tracker workbook

VBA Code:
                        On Error Resume Next
                    Set shtShkDay = wbShrink.Worksheets(dtCellDay)
                        If Err Then
                            MsgBox "Prod Sheet: " & vbTab & shtProd.Name & vbLf & _
                                "Cell: " & vbTab & vbTab & dtCell.Address & vbLf & _
                                "Cell Value: " & vbTab & dtCell & vbLf & _
                                "WeekDay Sheet: " & vbTab & dtCellDay
                            Exit Sub
                        End If
                        On Error GoTo 0
 
Upvote 0
that would make sense. the productivity tracker only has the previous Date entered in with a daily macro i run. There wouldn't be a way to have the Shrinkage workbook match to a date. Which is why I just want it to fill in on the next row down in column Y... :/ I'm so sorry for all the confusion. I probably explained this all wrong in the beginning.

The shrinkage form is filled out daily by about 30 different people. Then I go into the productivity tracker and run a formula that takes Allie's info from the Shrinkage workbook for Monday and puts it onto Allie's worksheet in the productivity tracker on the next line down in column Y. But I would want it to work for every employee.
So I would want it to do the same for Alexis. Pull the info for Alexis from the Shrinkage workbook for Monday then paste it onto the next line down in column Y for the Alexis tab in the productivity tracker.

BUT then I need it to run for everyone for Tuesday, then for Wednesday etc. I got it to work individually with a vlookup or an index match, but i can't make the next macro run for the next line down in column Y on the productivity tracker. Is that even possible?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top