Excel VBA - macro to choose workbook to paste into depending on persons name

Status
Not open for further replies.

Lisa Harris

New Member
Joined
Sep 19, 2016
Messages
17
Hi all,

Need to develop my macros further - write code for excel to choose the relevant workbook to open and paste the row information into. The code works perfectly at the moment pasting into one master workbook. Now need it to choose the managers relevant workbook which is saved on their desktop. Tried method of 'setting' their workbooks with 'IF' conditions, but wouldn't work. Then found using a cell to include the filename location - however, can't get this to work but can't see what I am doing wrong....checked the filepath is typed correctly etc. The filepath name is featured on the 'PMs own Wip' tab of their workbook on cell "A1":

Col A
Row 1
C:\Users\user.name\Desktop\Project Manager WIP.xlsm

<tbody>
</tbody>


The code I have added in for this element is

Code:
[Dim wb1 As Workbook
 Dim wb2 As Workbook
 Dim ws1 As Worksheet
 Dim ws2 As Worksheet
 
Set wb1 = ThisWorkbook 'Workbook 1
Set ws1 = wb1.Sheets("PMs own WIP") 'change to whatever sheet called
MsgBox ws1.Range("A1")
Set wb2 = GetObject(ws1.Range("A1"))
Set ws2 = wb2.Sheets("PMs own WIP") 'change to whatever sheet called
/CODE]

Any help would be much appreciated!! many thanks, Lisa 

Full code is:

[CODE]Sub PMLoopCopyPaste()
'
 Dim wb1 As Workbook
 Dim wb2 As Workbook
 Dim ws1 As Worksheet
 Dim ws2 As Worksheet
 
Set wb1 = ThisWorkbook 'Workbook 1
Set ws1 = wb1.Sheets("PMs own WIP") 'change to whatever sheet called
MsgBox ws1.Range("A1")
Set wb2 = GetObject(ws1.Range("A1"))
Set ws2 = wb2.Sheets("PMs own WIP") 'change to whatever sheet called
'
 
lr = ws1.Range("B" & Rows.Count).End(xlUp).ROW 'lastrow of data to get number of loops required
If lr = 1 Then Exit Sub 'exit if no data
For i = 2 To lr
    myVal = ws1.Range("B" & i).Value
    If myVal > 0 Then 'checks cell not just a zero
        lr2 = ws2.Range("B" & Rows.Count).End(xlUp).ROW 'lastrow of target worksheet
        With ws2.Range("B2:B" & lr2)
            Set c = .Find(myVal, LookIn:=xlValues, LookAt:=xlWhole)
            If Not c Is Nothing Then
                    ws1.Range("A" & i & ":AD" & i).Copy
                    ws2.Cells(c.ROW, 1).PasteSpecial xlPasteValues
                Else
                    ws1.Range("A" & i & ":AD" & i).Copy
                    ws2.Cells(lr2 + 1, 1).PasteSpecial xlPasteValues
                End If
            End With
        End If
    Next i
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
 
 Range("A3").Select
    
    'then tells it to go back to cell A3 so not left at bottom of a spreadsheet
    
    
    Application.CutCopyMode = False
    ActiveWorkbook.Save
    ActiveWindow.Close
    
    'tells it to save WIP and then close it down
    
    
    Sheets("PMs Own WIP").Select
    Range("B2").Select
    Sheets("Workflow Brief").Select
    Range("A3").Select
    ActiveWorkbook.Save
    
    'tells it to go back to the workflow sheet and select workflow brief tab
    
    MsgBox "The PM WIP has successfully been updated.", vbInformation + vbOKOnly, "PM Update Complete"
    
    'provides a message box to confirm to user the update to WIP has happened
     
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,216,031
Messages
6,128,422
Members
449,450
Latest member
gunars

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