ASadStudent
New Member
- Joined
- Oct 26, 2022
- Messages
- 20
- Office Version
- 365
- Platform
- Windows
Hello everyone, I have a problem with my copy paste macro where it deletes the formula (=Sum) that is in the Row where the macro runs. I have posted about this problem before, but in the previous post I had 2 problems and I solved one myself so I wanted to ask again about this.
Here is an explanation about my code:
My macro needs to copy paste data from 1 excel files sheet to another. The file where the data is coming from is called “omzet” and the file where the data needs to go to is called “maandafsluiting”. The sheets in both file are just called Sheet1.
The data needs to be copied based on the product name that is in the B row of both files. If the names match the code needs to copy the amount in the N row of "Omzet" to the F row in "maandafsluiting". If the B file doesn't match then it needs to do nothing.
What I want to add
I would like it if my macro ignores the formulas that are in the row where the data needs to go. So that is in this instance row F.
This is what my macro looks like right now:
Here are my excel files:
Omzet:
Maandafsluiting:
Thank you all very much for helping me solve this problem!
Here is an explanation about my code:
My macro needs to copy paste data from 1 excel files sheet to another. The file where the data is coming from is called “omzet” and the file where the data needs to go to is called “maandafsluiting”. The sheets in both file are just called Sheet1.
The data needs to be copied based on the product name that is in the B row of both files. If the names match the code needs to copy the amount in the N row of "Omzet" to the F row in "maandafsluiting". If the B file doesn't match then it needs to do nothing.
What I want to add
I would like it if my macro ignores the formulas that are in the row where the data needs to go. So that is in this instance row F.
This is what my macro looks like right now:
VBA Code:
Sub CopyPaste()
Dim omzet As Worksheet: Set omzet = Workbooks.Item("Omzet").Sheets("Sheet1")
Dim Maandafsluiting As Worksheet: Set Maandafsluiting = Workbooks.Item("Maandafsluiting").Sheets(1)
Dim data As Variant, lr As Long, d As Object, key As String, rw As Long
lr = omzet.Cells(Rows.Count, 2).End(3).Row
data = omzet.Cells(1, 1).Resize(lr, 14).Value
Set d = CreateObject("Scripting.Dictionary")
For rw = LBound(data) To UBound(data)
If data(rw, 14) <> 0 Then
key = data(rw, 2)
If Not d.exists(key) Then
d(key) = data(rw, 14)
End If
End If
Next rw
lr = Maandafsluiting.Cells(Rows.Count, 2).End(3).Row
data = Maandafsluiting.Cells(1, 1).Resize(lr, 6).Value
For rw = LBound(data) To UBound(data)
key = data(rw, 2)
If d.exists(key) Then
data(rw, 6) = d(key)
End If
Next rw
Maandafsluiting.Cells(1, 6).Resize(UBound(data)).Value = Application.Index(data, 0, 6)
End Sub
Here are my excel files:
Omzet:
Maandafsluiting:
Thank you all very much for helping me solve this problem!