macro deleting formulas

ASadStudent

New Member
Joined
Oct 26, 2022
Messages
20
Office Version
  1. 365
Platform
  1. Windows
This is what the original macro does:
My macro copies data from 1 excel document sheet to another. The document where the data is coming from is called “report” and the document where the data needs to go to is called “maandafsluiting”. The sheet in report is called Page1 and the sheet in maandafsluiting isn't really called in the code.

The data is copied based on the product code that is in the A Column of both documents. If the product codes on both files match then it needs to copy the amount that is in the R column in the “report” document to the F Column in the “Maandverband” document.

What I want to change:
Right now code deletes the =som formulas when it copies the different amounts. I want to find a way so it doesn't do that anymore.
I have asked this question before and made it work for some time, but because of the other changes in the code I did after that it stopped working.

I would greatly appreciate it if you can help me with this problem.


VBA Code:
VBA Code:
 Sub Kijken_2()
  Dim Report As Worksheet, Maandafsluiting As Worksheet
  Dim data As Variant, ky As Variant
  Dim lr As Long, rw As Long
  Dim d As Object, d2 As Object
  Dim rng As Range
 
  Set d = CreateObject("Scripting.Dictionary")
  Set d2 = CreateObject("Scripting.Dictionary")
 
  Set Report = Workbooks.Item("Report").Sheets("Page1")
  Set Maandafsluiting = Workbooks.Item("Maandafsluiting").Sheets(1)
 
  lr = Report.Cells(Rows.Count, 1).End(3).Row
  With Report.Cells(1, 1).Resize(lr, 18)
    data = .Value
    .Interior.ColorIndex = xlNone
  End With
 
  For rw = LBound(data) To UBound(data)
    If data(rw, 18) <> 0 Then
      ky = data(rw, 1)
      If Not d.exists(ky) Then
        d(ky) = data(rw, 18) & "|" & rw
      End If
    End If
  Next rw
 
  lr = Maandafsluiting.Cells(Rows.Count, 1).End(3).Row
  data = Maandafsluiting.Cells(1, 1).Resize(lr, 6).Formula
 
  For rw = LBound(data) To UBound(data)
    ky = data(rw, 1)
    d2(ky) = Empty
    If d.exists(ky) Then
      data(rw, 6) = Split(d(ky), "|")(0)
    End If
  Next rw

  For Each ky In d.keys
    If Not d2.exists(ky) Then
      rw = Split(d(ky), "|")(1)
      If rng Is Nothing Then
        Set rng = Report.Cells(rw, 1)
      Else
        Set rng = Union(rng, Report.Cells(rw, 1))
      End If
    End If
  Next
 
  If Not rng Is Nothing Then rng.Interior.Color = vbRed
 
 
  Maandafsluiting.Cells(1, 6).Resize(UBound(data)).Formula = Application.Index(data, 0, 6)
End Sub

Excel files:
Report.xlsx
ABCDEFGHIJKLMNOPQR
1Total
2Product codeAmount of times sold
32500615
42100216
52501417
6250121
7250174
8250227
92601543
10260082
1126004563
12
13
142311644
152311723
16231013
1723106657
182311289
19231117
20231302
21
22
232400645
24240012
2524004462
26235076
272350846
2823501427
292350427
3023132246
Page1


Maandafsluiting.xlsx
ABCDEF
1Total
2Product codesAantal
321001
42100216
521003
621004
721005
821006
9Total
1022000
1122002
1222004
1322006
1422007
1522010
1622012
1722014
1822017
1922018
2022022
2122023
2222031
2322032
24Total
25231013
2623102
2723103
2823104
2923106657
3023107
3123108
3223109
33231117
342311289
352311644
362311723
3723121
38Total
3923501427
4023502
4123503
422350427
4323505
4423506
45235076
462350846
4723511
48 Total
49240012
5024003
5124004462
52Total
532500615
5425008
55250121
5625013
572501417
5825016
59250174
60250227
6125060
6225061
6325062
6425063
6525064
66Total
6726002
6826004563
69260082
7026010
7126012
7226014
732601543
7426018
75Total
76231302
7723131
7823132246
7923133
802400645
812400645
82338
Blad1
Cell Formulas
RangeFormula
F82F82=SUM(F76:F81)
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Or another solution I can think of is making it so that the macro ignores empty spaces.
Does anyone know a way to implement that ?
 
Upvote 0
VBA Code:
Sub testing()
        Dim wk1, wk2 As Worksheet
        Dim lr, lr2 As Long
        Set wk1 = Workbooks("Book1").Sheets("Sheet1") ' this is the report workbook
        Set wk2 = Workbooks("Book2").Sheets("Sheet1")  ' this is the Maandafsluiting workbook
        Dim k As Integer
        Dim dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        lr = wk1.Range("A" & Rows.Count).End(xlUp).Row
        lr2 = wk2.Range("A" & Rows.Count).End(xlUp).Row
       
        For k = 3 To lr
                If Not dic.Exists(wk1.Range("A" & k).Value) And wk1.Range("A" & k) <> "" Then
                    dic(wk1.Range("A" & k).Value) = wk1.Range("R" & k).Value
                End If
        Next k
       
        For k = 3 To lr2
                If dic.Exists(wk2.Range("A" & k).Value) And wk2.Range("A" & k) <> "" Then
                    wk2.Range("F" & k) = dic(wk2.Range("A" & k).Value)
                End If
        Next k
       
        dic.RemoveAll
       
End Sub
 
Upvote 0
VBA Code:
Sub testing()
        Dim wk1, wk2 As Worksheet
        Dim lr, lr2 As Long
        Set wk1 = Workbooks("Book1").Sheets("Sheet1") ' this is the report workbook
        Set wk2 = Workbooks("Book2").Sheets("Sheet1")  ' this is the Maandafsluiting workbook
        Dim k As Integer
        Dim dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        lr = wk1.Range("A" & Rows.Count).End(xlUp).Row
        lr2 = wk2.Range("A" & Rows.Count).End(xlUp).Row
      
        For k = 3 To lr
                If Not dic.Exists(wk1.Range("A" & k).Value) And wk1.Range("A" & k) <> "" Then
                    dic(wk1.Range("A" & k).Value) = wk1.Range("R" & k).Value
                End If
        Next k
      
        For k = 3 To lr2
                If dic.Exists(wk2.Range("A" & k).Value) And wk2.Range("A" & k) <> "" Then
                    wk2.Range("F" & k) = dic(wk2.Range("A" & k).Value)
                End If
        Next k
      
        dic.RemoveAll
      
End Sub
Thanks for answering my question.
I do have a question I would like to ask you.
Where in my existing code would I add this part ?
Because I don't fully understand how to add the copying part of my code to the code that you wrote.
 
Upvote 0
Thanks for answering my question.
I do have a question I would like to ask you.
Where in my existing code would I add this part ?
Because I don't fully understand how to add the copying part of my code to the code that you wrote.
I rewrote the code because your original code is very bad. I'd have to debug it, also why do you have 2 dictionaries in your code. Just use mine and tell me if it works, then I'll explain to you how it works.
 
Upvote 0
I rewrote the code because your original code is very bad. I'd have to debug it, also why do you have 2 dictionaries in your code. Just use mine and tell me if it works, then I'll explain to you how it works.
Right now it doesn't work. It doesn't give me a error notice so I don't know why it doesn't work.
Here is what I used:
VBA Code:
Sub testing()
        Dim wk1, wk2 As Worksheet
        Dim lr, lr2 As Long
        Set wk1 = Workbooks("Report").Sheets("Page1")
        Set wk2 = Workbooks("Maandafsluiting").Sheets("Blad1")
        Dim k As Integer
        Dim dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        lr = wk1.Range("A" & Rows.Count).End(xlUp).Row
        lr2 = wk2.Range("A" & Rows.Count).End(xlUp).Row
      
        For k = 3 To lr
                If Not dic.Exists(wk1.Range("A" & k).Value) And wk1.Range("A" & k) <> "" Then
                    dic(wk1.Range("A" & k).Value) = wk1.Range("R" & k).Value
                End If
        Next k
      
        For k = 3 To lr2
                If dic.Exists(wk2.Range("A" & k).Value) And wk2.Range("A" & k) <> "" Then
                    wk2.Range("F" & k) = dic(wk2.Range("A" & k).Value)
                End If
        Next k
      
        dic.RemoveAll
      
End Sub
 
Last edited:
Upvote 0
VBA Code:
Sub testing()
        Dim wk1, wk2 As Worksheet
        Dim lr, lr2 As Long
        Set wk1 = Workbooks("Report.xlsx").Sheets("Page1")
        Set wk2 = Workbooks("Maandafsluiting.xlsx").Sheets("Blad1")
        Dim k As Integer
        Dim dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        lr = wk1.Range("A" & Rows.Count).End(xlUp).Row
        lr2 = wk2.Range("A" & Rows.Count).End(xlUp).Row
      
        For k = 3 To lr
                If Not dic.Exists(wk1.Range("A" & k).Value) And wk1.Range("A" & k) <> "" Then
                    dic(wk1.Range("A" & k).Value) = wk1.Range("R" & k).Value
                End If
        Next k
      
        For k = 3 To lr2
                If dic.Exists(wk2.Range("A" & k).Value) And wk2.Range("A" & k) <> "" Then
                    wk2.Range("F" & k) = dic(wk2.Range("A" & k).Value)
                End If
        Next k
      
        dic.RemoveAll
      
End Sub
Now try
 
Upvote 0
VBA Code:
Sub testing()
        Dim wk1, wk2 As Worksheet
        Dim lr, lr2 As Long
        Set wk1 = Workbooks("Report.xlsx").Sheets("Page1")
        Set wk2 = Workbooks("Maandafsluiting.xlsx").Sheets("Blad1")
        Dim k As Integer
        Dim dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        lr = wk1.Range("A" & Rows.Count).End(xlUp).Row
        lr2 = wk2.Range("A" & Rows.Count).End(xlUp).Row
     
        For k = 3 To lr
                If Not dic.Exists(wk1.Range("A" & k).Value) And wk1.Range("A" & k) <> "" Then
                    dic(wk1.Range("A" & k).Value) = wk1.Range("R" & k).Value
                End If
        Next k
     
        For k = 3 To lr2
                If dic.Exists(wk2.Range("A" & k).Value) And wk2.Range("A" & k) <> "" Then
                    wk2.Range("F" & k) = dic(wk2.Range("A" & k).Value)
                End If
        Next k
     
        dic.RemoveAll
     
End Sub
Now try
It still isn't working.
 
Upvote 0
The code seems to work if I change Value with formula.
Now I only need to add the part where if the code in the A Column isn't found it becomes red.

VBA Code:
Sub testing()
        Dim wk1, wk2 As Worksheet
        Dim lr, lr2 As Long
        Set wk1 = Workbooks("Report.xlsx").Sheets("Page1")
        Set wk2 = Workbooks("Maandafsluiting.xlsx").Sheets("Blad1")
        Dim k As Integer
        Dim dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        lr = wk1.Range("A" & Rows.Count).End(xlUp).Row
        lr2 = wk2.Range("A" & Rows.Count).End(xlUp).Row
      
        For k = 3 To lr
                If Not dic.Exists(wk1.Range("A" & k).Formula) And wk1.Range("A" & k) <> "" Then
                    dic(wk1.Range("A" & k).Formula) = wk1.Range("R" & k).Formula
                End If
        Next k
      
        For k = 3 To lr2
                If dic.Exists(wk2.Range("A" & k).Formula) And wk2.Range("A" & k) <> "" Then
                    wk2.Range("F" & k) = dic(wk2.Range("A" & k).Formula)
                End If
        Next k
      
        dic.RemoveAll
      
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,093
Messages
6,123,067
Members
449,090
Latest member
fragment

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