Copy from one sheet to another sheet depend on cell value

KlausW

Active Member
Joined
Sep 9, 2020
Messages
378
Office Version
  1. 2016
Platform
  1. Windows
Hi all Excel helpers.
I have a challenge.
In a sheet, I test whether my employees have received the uniform parts they need, if I put a number next to the uniform parts in question and the employee in question, they have not received what they need.
In column B, the uniform parts are cell B2 to cell B122. In rows C1 to V1, employee no.
Now it's like that I would like to have moved employee number and the uniform parts with number over to a new sheet. It has to be formal.
All help will be appreciated
With best regards
Klaus W
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi KlausW,

Please share the excel data to understand the requirement better.

Thanks,
Saurabh
 
Upvote 0
Hi Saurabhj

When I enter in cell C3 number how many uniform parts employees 6301 are missing. Should what is in cell A3 Material no.8465999572838 is transferred to another sheet “Tøj mangler”, and when I type in cell D4, what is in cell A4 Material no. 8465997252005 must be transferred to the same sheets “Tøj mangler”. But only where something stands out. Employees 6303 should not be transferred to the Sheet “Tøj mangler”. See picture 1.

The result should look like picture 2.

Yours sincerely Klaus W

Picture 1.jpg
Picture 2.jpg
 
Upvote 0
Hi KlausW,

Use below code:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lastCol As Integer, colno As Integer, lastRow As Integer
    Dim colExists As String, employee As Integer
    colExists = "N"
    lastCol = Sheets("TOJ MANGLER").Cells(1, Columns.Count).End(xlToLeft).Column
    If Target.Column > 2 Then
        If Target.Value <> "" Then
            employee = Sheets("Material").Cells(1, Target.Column)
            For colno = 1 To lastCol
                If Sheets("TOJ MANGLER").Cells(1, colno) = employee Then
                    colExists = "Y"
                    lastRow = Sheets("TOJ MANGLER").Cells(Rows.Count, colno).End(xlUp).Row
                    Sheets("Material").Range("A" & Target.Row & ":B" & Target.Row).Copy Sheets("TOJ MANGLER").Cells(lastRow + 1, colno)
                    Sheets("TOJ MANGLER").Cells(lastRow + 1, colno + 2) = Target.Value
                End If
            Next
            If colExists = "N" Then
                Sheets("TOJ MANGLER").Cells(1, lastCol + 3) = employee
                Sheets("Material").Range("A" & Target.Row & ":B" & Target.Row).Copy Sheets("TOJ MANGLER").Cells(2, lastCol + 3)
                Sheets("TOJ MANGLER").Cells(2, lastCol + 5) = Target.Value
            End If
           
        End If
    End If
End Sub

I have used sheet as below:
CopyPasteData-Condition.xlsm
ABCDEFGHI
1Material NoMaterial6301630263036304630563066307
222657615A
333456945B1
430287017C
538787886D
646476555E
746356993F
821836799G4
946587808H
1043076941I
1136356316J
1225867822K
1343266937L
1432027954M
1520126099N
1647517530O1
1746727837P1
1832006883Q
1927646617R
2040286934S
2149486437T
2241306226U
2334317743V
2441646165W
2521997425X
2623107349Y
2733656833Z
2847576723AA
Material
 
Upvote 0
Dear

Saurabhj it lookes very nice, I have a some questions but they will come tomorrow as it is evening here in Denmark. Klaus W

 
Upvote 0
Dear Saurabh If I put the code into my own sheet. And deletes everything in sheets Material, and insert numbers again, insert the numbers in column D instead of in column A of the TOJ MISSING sheet. Sincerely, Klaus W
 
Upvote 0
Hi, check below code:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lastCol As Integer, colno As Integer, lastRow As Integer
    Dim colExists As String, employee As Integer
    colExists = "N"
    lastCol = Sheets("TOJ MANGLER").Cells(1, Columns.Count).End(xlToLeft).Column
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column > 2 Then
        'MsgBox Target.Column
        If Target.Value <> "" Then
            'MsgBox Target.Value
            employee = Sheets("Material").Cells(1, Target.Column)
            For colno = 1 To lastCol
                If Sheets("TOJ MANGLER").Cells(1, colno) = employee Then
                    colExists = "Y"
                    lastRow = Sheets("TOJ MANGLER").Cells(Rows.Count, colno).End(xlUp).Row
                    Sheets("Material").Range("A" & Target.Row & ":B" & Target.Row).Copy Sheets("TOJ MANGLER").Cells(lastRow + 1, colno)
                    Sheets("TOJ MANGLER").Cells(lastRow + 1, colno + 2) = Target.Value
                End If
            Next
            If colExists = "N" Then
                If Sheets("TOJ MANGLER").Cells(1, 1) = "" Then
                    Sheets("TOJ MANGLER").Cells(1, lastCol) = employee
                    Sheets("Material").Range("A" & Target.Row & ":B" & Target.Row).Copy Sheets("TOJ MANGLER").Cells(2, lastCol)
                    Sheets("TOJ MANGLER").Cells(2, lastCol + 2) = Target.Value
                Else
                    Sheets("TOJ MANGLER").Cells(1, lastCol + 3) = employee
                    Sheets("Material").Range("A" & Target.Row & ":B" & Target.Row).Copy Sheets("TOJ MANGLER").Cells(2, lastCol + 3)
                    Sheets("TOJ MANGLER").Cells(2, lastCol + 5) = Target.Value
                End If

            End If
           
        End If
    End If
End Sub
 
Last edited:
Upvote 0
Solution
Hi, check below code:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lastCol As Integer, colno As Integer, lastRow As Integer
    Dim colExists As String, employee As Integer
    colExists = "N"
    lastCol = Sheets("TOJ MANGLER").Cells(1, Columns.Count).End(xlToLeft).Column
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column > 2 Then
        'MsgBox Target.Column
        If Target.Value <> "" Then
            'MsgBox Target.Value
            employee = Sheets("Material").Cells(1, Target.Column)
            For colno = 1 To lastCol
                If Sheets("TOJ MANGLER").Cells(1, colno) = employee Then
                    colExists = "Y"
                    lastRow = Sheets("TOJ MANGLER").Cells(Rows.Count, colno).End(xlUp).Row
                    Sheets("Material").Range("A" & Target.Row & ":B" & Target.Row).Copy Sheets("TOJ MANGLER").Cells(lastRow + 1, colno)
                    Sheets("TOJ MANGLER").Cells(lastRow + 1, colno + 2) = Target.Value
                End If
            Next
            If colExists = "N" Then
                If Sheets("TOJ MANGLER").Cells(1, 1) = "" Then
                    Sheets("TOJ MANGLER").Cells(1, lastCol) = employee
                    Sheets("Material").Range("A" & Target.Row & ":B" & Target.Row).Copy Sheets("TOJ MANGLER").Cells(2, lastCol)
                    Sheets("TOJ MANGLER").Cells(2, lastCol + 2) = Target.Value
                Else
                    Sheets("TOJ MANGLER").Cells(1, lastCol + 3) = employee
                    Sheets("Material").Range("A" & Target.Row & ":B" & Target.Row).Copy Sheets("TOJ MANGLER").Cells(2, lastCol + 3)
                    Sheets("TOJ MANGLER").Cells(2, lastCol + 5) = Target.Value
                End If

            End If
          
        End If
    End If
End Sub
Good morning Saurabhj, thank you very much, it seems it is just as it should be. Have a nice day, greetings from Denmark, Klaus
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,874
Members
449,056
Latest member
ruhulaminappu

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