My macro deletes formulas.

Status
Not open for further replies.

ASadStudent

New Member
Joined
Oct 26, 2022
Messages
20
Office Version
  1. 365
Platform
  1. 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:

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:

1668501539898.png


Maandafsluiting:

1668501496542.png


Thank you all very much for helping me solve this problem!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Status
Not open for further replies.

Forum statistics

Threads
1,214,838
Messages
6,121,885
Members
449,057
Latest member
Moo4247

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