Need help to Copy the entire row to another sheet

spycein

Board Regular
Joined
Mar 8, 2014
Messages
135
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,
Need your help to copy data from one sheet to another based on cell value.
For example, I have the following source data set and I am looking for a VBA solution which would copy the row value to a separate sheet as mentioned under the Target Sheet Name example.

i=

Source Sheet Name = Master Sheet
Transaction TypeReference NumberDr Account NoPayment NarrationBeneficiary NameSheet Name
ChequeJAN123
123​
RentalABCJanuary
ChequeFEB123
456​
StationeryXYZFebruary
NEFTMAR123
789​
FoodXYZMarch
NEFTMAR456
587​
TravelABCMarch
Target Sheet Name = January
Transaction TypeReference NumberDr Account NoPayment NarrationBeneficiary Name
ChequeJAN123
123​
RentalABC
Target Sheet Name = February
Transaction TypeReference NumberDr Account NoPayment NarrationBeneficiary Name
ChequeFEB123
456​
StationeryXYZ
Target Sheet Name = March
Transaction TypeReference NumberDr Account NoPayment NarrationBeneficiary Name
NEFTMAR123
789​
FoodXYZ
NEFTMAR456
587​
TravelABC
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hello
With VBA?
A cycle that sweeps all the Sheets, excluding the first one, and, if the first three letters of "Reference Number" are equal to the first three letters of the Sheet name, then find the first empty cell and paste the data you copied.

Hello,
Mario
 
Upvote 0
I have the following source data set and I am looking for a VBA solution which would copy the row value to a separate sheet as mentioned under the Target Sheet Name example
Assuming that the sheets January, February, etc etc already exist, try this macro:
VBA Code:
Sub SpreadToMonth()
Dim I As Long, cMon As String
Dim MCol As String, EMess
Dim CopyCol As String, cCol
'
Sheets("MasterSheet").Select        '<<< The correct name for the starting sheet
MCol = "F"                          '<<< The column with the "month"
CopyCol = "A:E"                     '<<< The columns to be copied
'
For I = 2 To Cells(Rows.Count, "A").End(xlUp).Row
    On Error Resume Next
        cMon = Sheets(Cells(I, MCol).Value).Name
    On Error GoTo 0
    If cMon = Cells(I, MCol).Value Then
        Range(Cells(I, Split(CopyCol, ":", , vbTextCompare)(0)), Cells(I, Split(CopyCol, ":", , vbTextCompare)(1))).Copy _
        Destination:=Sheets(cMon).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
    Else
        EMess = EMess & ", " & Cells(I, MCol).Value
    End If
Next I
If Len(EMess) > 3 Then
    MsgBox ("Unallocated:" & vbCrLf & Mid(EMess, 3))
Else
    MsgBox ("Completed")
End If
End Sub
Check that the lines marked <<< contains the right parametres

A messagebox will report the completion of the allocation, with the list of any "month" that was not found
 
Upvote 0
Solution
Assuming that the sheets January, February, etc etc already exist, try this macro:
VBA Code:
Sub SpreadToMonth()
Dim I As Long, cMon As String
Dim MCol As String, EMess
Dim CopyCol As String, cCol
'
Sheets("MasterSheet").Select        '<<< The correct name for the starting sheet
MCol = "F"                          '<<< The column with the "month"
CopyCol = "A:E"                     '<<< The columns to be copied
'
For I = 2 To Cells(Rows.Count, "A").End(xlUp).Row
    On Error Resume Next
        cMon = Sheets(Cells(I, MCol).Value).Name
    On Error GoTo 0
    If cMon = Cells(I, MCol).Value Then
        Range(Cells(I, Split(CopyCol, ":", , vbTextCompare)(0)), Cells(I, Split(CopyCol, ":", , vbTextCompare)(1))).Copy _
        Destination:=Sheets(cMon).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
    Else
        EMess = EMess & ", " & Cells(I, MCol).Value
    End If
Next I
If Len(EMess) > 3 Then
    MsgBox ("Unallocated:" & vbCrLf & Mid(EMess, 3))
Else
    MsgBox ("Completed")
End If
End Sub
Check that the lines marked <<< contains the right parametres

A messagebox will report the completion of the allocation, with the list of any "month" that was not found
Thank you so much @Anthony47 !!
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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