Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Kurt,
The code below should work. Just copy it to a new module and adjust any of the code that you need as applicable. Hope this helps, Ben Code: Option Explicit Sub MonthToYear() Dim wsYear As Worksheet 'Destination Sheet Dim wsMonth As Worksheet 'Source Sheet Dim rAdd As Range 'Range with month number Dim rCell As Range 'Client number cells Dim rCopy As Range 'Client information Dim rPaste As Range 'Destination cells Dim strError As String 'Error message Dim x As Long '# of Errors Dim y As Long '# of Clients 'Set sources of data Set wsYear = Sheets("Year") Set wsMonth = Sheets("Month") Set rAdd = Sheets("Start").Range("A3") x = 0 y = 0 'Loop through each client on Month sheet and paste data to applicable month if client found For Each rCell In wsMonth.Range("A2:A" & wsMonth.Range("A50000").End(xlUp).Row) Set rCopy = rCell.Offset(0, 2).Resize(1, 5) 'Client data range Set rPaste = GetRange(rCell.Value, wsYear, rCell) 'Calls a function to get paste range If rPaste.Address = rCell.Address Then 'Could not find client number on Year tab strError = strError & rCell.Value & vbCr 'Build error message x = x + 1 'Increment the error counter Else rCopy.Copy 'Copy the client data and paste special to Year tab rPaste.Offset(0, 1 + rAdd.Value).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ False, Transpose:=True Application.CutCopyMode = False End If y = y + 1 'Increment client counter Next rCell 'Now that all data is complete, display a message If x 0 Then 'Errors occurred If x = y Then 'No client numbers found MsgBox "None of the client numbers could not be found. Clients searched include: " _ & vbCr & vbCr & strError, vbOKOnly, "Clients not found" Else 'Some clients found, others not found MsgBox "The following client client numbers could not be found: " & vbCr & vbCr & _ strError & vbCr & vbCr & "All others transferred succesfully.", vbOKOnly, _ "Clients not found" End If Else 'All clients found MsgBox "All clients transferred successfully", vbOKOnly, "Transfer Successful" End If End Sub Function GetRange(strFind As String, wsFind As Worksheet, rSource As Range) As Range Dim rRange As Range On Error Resume Next 'strFind is the client number, wsFind is the Year sheet and rSource is the range containing 'the client number on the Month tab ' 'If strFind is found, then the function returns its location, otherwise the function returns 'the source cell Set rRange = wsFind.Columns("B:B").Find(What:=strFind, After:=wsFind.Range("B1"), LookIn:=xlFormulas, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If rRange Is Nothing Then Set GetRange = rSource Else Set GetRange = rRange End If End Function |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Find last column & copy/paste values | Excel Programming | |||
Find number in Row 1, Copy Paste Values in Rows below | Excel Programming | |||
Find end of column and paste formula in range | Excel Worksheet Functions | |||
to find change and paste existing values/rows in excel with help of form | Excel Discussion (Misc queries) | |||
Find Empty Column and paste cell values | Excel Programming |