Sub PasteClient()
Dim ClientFile As String 'Holds client's file name
Dim CurRange As Range 'Holds current range selected
'In case of an error this will handle it gracefully and give you
'some information.
On Error GoTo ErrorHandler
'Turn screen updating off. You won't see the client file being updated.
Application.ScreenUpdating = False
'Make sure proper worksheet is active. This assumes the
'worksheet is named Sheet1. Adjust as required.
If ActiveCell.Worksheet.Name <> "MAIN" Then Exit Sub
'Make sure a cell in column A is selected. This assumes the client
'account number is in column A. Adjust as required.
If Left(ActiveCell.Address(False, False), 1) <> "A" Then Exit Sub
'Get the current range selected. These are cells in column A and
'are contiguous.
Set CurRange = Selection
'Iterate through each cell in selected range of client IDs in
'column A, selecting each client ID then copying client info,
'opening client file, pasting, closing client file.
For Each c In CurRange
'Select the current client id in selected range
c.Select
'Get the client's file name.
ClientFile = LABOUR.xlsm
'Select the client data to be posted. This assumes client data
'spans 6 cells
ActiveCell.Range("A1:D1").Copy
'Open the client file for posting. This assumes the path to the
'1000 client files is in the path "D:\R&D\CONFERENCE\CONFERENCE\". Adjust as required.
Workbooks.Open Filename:="D:\" & ClientFileD
'Assumes the client data will be posted on a worksheet named
'"Sheet1" in the client file just opened. Adjust as required.
Sheets("Sheet1").Select
'Position the cursor in the first cell in the client file. Assumes
'client data will be posted beginning in column A. Adjust as required.
Range("B5").Select
'Find the first empty cell where the current data can be posted.
Do While ActiveCell.Text > ""
ActiveCell.Offset(1, 0).Select
'Assumes your Excel sheets have 65536 rows. This prevents running
'off the bottom of the worksheet and causing an error. Adjust as
'required.
If ActiveCell.Row > 65536 Then Exit Sub
Loop
'Paste the client data into the client worksheet file.
ActiveSheet.Paste
'Cancels the copy mode. Client data can no longer be pasted any where else.
Application.CutCopyMode = False
'Save the client workbook and the posted data just pasted.
ActiveWorkbook.Save
'Close the client workbook.
ActiveWindow.Close
'Get next client data in range selected.
Next c
'Turn screen updating back on.
Application.ScreenUpdating = True
Exit Sub
ErrorHandler:
Select Case Err.Number
Case Is = 1004 'Client file already open or client file does not exist.
'There may be
'other things that will cause this error.
'There are several ways to handle this error. I chose to inform the user
'then exit
'this sub without any alteration to the client file.
MsgBox "There is a problem with client file: " & ClientFile, vbOKOnly + vbInformation, "An error has occurred ..."
Case Else 'Catches any unexpected errors.
MsgBox "Error number " & Err.Number & " has occurred", vbOKOnly + vbInformation, "An error has occurred ..."
End Select
End Sub
Bookmarks