Macro that replaces pre-existing number

dnasim

New Member
Joined
Jul 11, 2014
Messages
16
I am trying to have this Macro take data from workbook1 (ItemReceipts) column A and column C and matches it with the data from column A and B of Workbook2, named Demand Planning Prem respectively, take numbers from column D of ItemReceipts and sum it up, and then place that sum in column E of Demand Planning Prem and Reg.

Sub AddReturns()
Dim wb1 As Workbook, wb2 As Workbook, sh1 As Worksheet, sh2 As Worksheet, lr As Long, rng As Range, c As Range
Dim fLoc As Range, fAdr As String
Set wb1 = Workbooks("ItemReceipts")
Set wb2 = Workbooks("Demand Planning Prem")
Set sh1 = wb1.Sheets(1)
Set sh2 = wb2.Sheets(1)
lr = sh1.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh1.Range("A2:A" & lr)
For Each c In rng
Set fLoc = sh2.Range("B2", sh2.Cells(Rows.Count, 2).End(xlUp)).Find(c.Offset(0, 2).Value, , xlValues, xlWhole)
If Not fLoc Is Nothing Then
fAdr = fLoc.Address


Do
If Application.WeekNum(fLoc.Offset(0, -1).Value) = Application.WeekNum(c.Value) Then
sh2.Range("E" & fLoc.Row) = sh1.Range("D" & c.Row) + sh2.Range("E" & fLoc.Row)
Exit Do
End If
Set fLoc = sh2.Range("B2", sh2.Cells(Rows.Count, 2).End(xlUp)).FindNext(fLoc)
Loop While fAdr <> fLoc.Address
End If
Next
End Sub

The issue I am having is that prior to the macro running I have AVERAGE formulas in each of the cells in column E of Workbook2, and when I run the macro it seems to add on to the Average, but I want it to remove the formula and simple display the total sum.

Here are the ItemReceipt Columns


DateNumberItemQuantityCreated FromClassCompany NameLocationShipping CountrySum of Amount
8/8/2014140981AHFMT-0012rmaWebstore/Phone SalesstoreafricaUnited States46
7/7/2014140981CHDX-3022rmaWebstore/Phone SalesstorejapanUnited States240
6/25/2014140981ASDMC-3321rmaWebstore/Phone SalessoretexasUnited States2.76
6/20/2014141984CHHX-30230rmaRetailerAmaafricaUnited States3,7.34
6/20/2014142008280-00133-0001rmaWebstore/Phone Sales- None -japanFrance5.30
6/20/2014142009210-00510-0001rmaWebstore/Phone Sales- None -texasFrance124.08
6/20/2014142010210-00128-R11rmaWebstore/Phone Sales- None -africaFrance68.73

<tbody>
</tbody>





Here are the Demand Planning Prem and Reg columns

DateSKUNameBegin FG'sAdd ReturnsDemand FGEnd FG'sWeeks FGExcess FG
6/20/2014ASDRK-301Tent57181585661985339
6/27/2014ASDRK-301Tent566173656321565310
7/4/2014ASDRK-301Tent563243855981475276
7/11/2014ASDRK-301Tent559803855601465238
7/18/2014ASDRK-301Tent556003855221455200
7/25/2014ASDRK-301Tent552203854841445162
8/1/2014ASDRK-301Tent548403854461435124
8/8/2014ASDRK-301Tent544603854081425086

<tbody>
</tbody>
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
This code will convert formulas in column E on the active sheet to values.
Code:
Sub ConvertFormulasToText()

    Dim lLastERow As Long
    
    With ActiveSheet
        lLastERow = .Cells(.Rows.Count, 5).End(xlUp).Row
        With .Range("E2:E" & lLastERow)
            .Copy
            .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
                SkipBlanks:=False, Transpose:=False
        End With
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,657
Members
449,462
Latest member
Chislobog

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