replace blank cell with formula and formula with blank cells based on empty cell or numbers

abdo meghari

Active Member
Joined
Aug 3, 2021
Messages
465
Office Version
  1. 2019
Hello

I search for macro dealing with the last columns ARRIVED,SALES for

if the cells for columns ARRIVED,SALES before TTL row are empty then should replace blank cell with formula for TTL row , and if the cells for columns ARRIVED,SALES before TTL row are number then should return the same formula was already existed. I add three columns( ARRIVED,SALES,STOCK ) every month ,then should deal with the last columns ARRIVED,SALES have added when implement the conditions .

orginal data

Stock Sales report (12) .xls
ABCDEFGHIJKLM
1OCTOBERNOVEMBERDECEMBER
2CategorySizePatternOriginArrivedSalesStockArrivedSalesStockArrivedSalesStock
3PSR (LRD)BS 175/70R13B25A32INDO-16184--184184
4BS 175/70R13EP150INDO-------
5BS 175/70R14MY02THI--296--296296
6BS 195/65R15MY02THI-50361--361361
7BS 205/65R15EP150INDO----4--
8BS 235/95R15CD618JAP--56--5656
9BS 255/70R15CD840THI-661329100142513291329
10FS 31*10.50R15DEST ATJAP-------
11BS 205R16CD840THI---10048--
12BS 205/R16CD697THI-4293--293293
13BS 205/55R16AR20INDO-1-----
14BS 225/55R16T001JAP----2--
15BS 225/60R16AR20INDO--2--22
16BS 255/70R16T697INDO--1--11
17TTL-137252220014792522--2522
18PSR (HRD)BS 215/45R17T001JAP-------
19BS 225/45R17RE050AJAP--26--2626
20BS 225/45R17EA03JAP-------
21FS 225/45R17TZ700JAP-------
22BS 225/65R17T697INDO--24--2424
23BS 225/70R17D697THI--50--5050
24BS 275/45R20DSPORTJAP-------
25BS 295/35R20S001JAP-------
26BS 315/35R20SPORTJAP--3--33
27TTL--103--103--103
28LTRBS 650R16R230JAP-82193505219219
29BS 700R16R230JAP-3161193700611931193
30BS 750R16R230JAP-12219105253836491059105
31BS 195/70R15CR623JAP--389--389389
32BS 205/70R15CR624INDO-171030--10301030
33BS 215/70R15CR624INDO-401225--12251225
34BS 235/65R16CR660TR---28---
35TTL-160213161361637513161--13161
36LSRBS 750R16VSJJAP-444939--49394939
37BS 825R16R180JAP-------
38TTL-444939--4939--4939
39TBRBS 1200R20G580JAP-357026413967146026412641
40BS 1200R20R187JAP-60748110019481481
41BS 1400R20VSJJAP-34436--436436
42BS 1400R20R180JAP-------
43BS 1400R20R180BZJAP-------
44BS 11R22.5R187JAP--128--128128
45BS 12R22.5R187JAP--79--7979
46BS 13R22.5R187JAP---82--
47BS 275/70R22.5R294JAP--166--166166
48BS 315/80R22.5R184JAP-3422222
49BS 315/80R22.5R184THI-8948-12948948
50BS 1200R24G580JAP-1351661098492166166
51TTL-43885047517519875047--5047
In & Out Balance
Cell Formulas
RangeFormula
E17:M17E17=SUM(E3:E16)
E27:M27E27=SUM(E18:E26)
F29F29=310+6
E35:M35E35=SUM(E28:E34)
E38:M38E38=SUM(E36:E37)
F40F40=586+21
J39:J50,J36:J37,J28:J34,J18:J26,J3:J16J3=G3
M39:M50,M36:M37,M28:M34,M18:M26,M3:M16M3=J3+K3-L3
E51:M51E51=SUM(E39:E50)


firs condition replace blank cell with formula for TTL row


Stock Sales report (12) .xls
ABCDEFGHIJKLM
1OCTOBERNOVEMBERDECEMBER
2CategorySizePatternOriginArrivedSalesStockArrivedSalesStockArrivedSalesStock
3PSR (LRD)BS 175/70R13B25A32INDO-16184--184184
4BS 175/70R13EP150INDO-------
5BS 175/70R14MY02THI--296--296296
6BS 195/65R15MY02THI-50361--361361
7BS 205/65R15EP150INDO----4--
8BS 235/95R15CD618JAP--56--5656
9BS 255/70R15CD840THI-661329100142513291329
10FS 31*10.50R15DEST ATJAP-------
11BS 205R16CD840THI---10048--
12BS 205/R16CD697THI-4293--293293
13BS 205/55R16AR20INDO-1-----
14BS 225/55R16T001JAP----2--
15BS 225/60R16AR20INDO--2--22
16BS 255/70R16T697INDO--1--11
17TTL-1372522200147925222522
18PSR (HRD)BS 215/45R17T001JAP-------
19BS 225/45R17RE050AJAP--26--2626
20BS 225/45R17EA03JAP-------
21FS 225/45R17TZ700JAP-------
22BS 225/65R17T697INDO--24--2424
23BS 225/70R17D697THI--50--5050
24BS 275/45R20DSPORTJAP-------
25BS 295/35R20S001JAP-------
26BS 315/35R20SPORTJAP--3--33
27TTL--103--103103
28LTRBS 650R16R230JAP-82193505219219
29BS 700R16R230JAP-3161193700611931193
30BS 750R16R230JAP-12219105253836491059105
31BS 195/70R15CR623JAP--389--389389
32BS 205/70R15CR624INDO-171030--10301030
33BS 215/70R15CR624INDO-401225--12251225
34BS 235/65R16CR660TR---28---
35TTL-16021316136163751316113161
36LSRBS 750R16VSJJAP-444939--49394939
37BS 825R16R180JAP-------
38TTL-444939--49394939
39TBRBS 1200R20G580JAP-357026413967146026412641
40BS 1200R20R187JAP-60748110019481481
41BS 1400R20VSJJAP-34436--436436
42BS 1400R20R180JAP-------
43BS 1400R20R180BZJAP-------
44BS 11R22.5R187JAP--128--128128
45BS 12R22.5R187JAP--79--7979
46BS 13R22.5R187JAP---82--
47BS 275/70R22.5R294JAP--166--166166
48BS 315/80R22.5R184JAP-3422222
49BS 315/80R22.5R184THI-8948-12948948
50BS 1200R24G580JAP-1351661098492166166
51TTL-438850475175198750475047
In & Out Balance
Cell Formulas
RangeFormula
M17,E17:J17E17=SUM(E3:E16)
M27,E27:J27E27=SUM(E18:E26)
F29F29=310+6
M35,E35:J35E35=SUM(E28:E34)
M38,E38:J38E38=SUM(E36:E37)
F40F40=586+21
J39:J50,J36:J37,J28:J34,J18:J26,J3:J16J3=G3
M51,E51:J51E51=SUM(E39:E50)
M39:M50,M36:M37,M28:M34,M18:M26,M3:M16M3=J3+K3-L3



second condition replace formula with blank cell
Stock Sales report (12) .xls
ABCDEFGHIJKLM
1OCTOBERNOVEMBERDECEMBER
2CategorySizePatternOriginArrivedSalesStockArrivedSalesStockArrivedSalesStock
3PSR (LRD)BS 175/70R13B25A32INDO-16184--184184
4BS 175/70R13EP150INDO-------
5BS 175/70R14MY02THI--296--296296
6BS 195/65R15MY02THI-50361--361361
7BS 205/65R15EP150INDO----4--
8BS 235/95R15CD618JAP--56--5656
9BS 255/70R15CD840THI-661329100142513291329
10FS 31*10.50R15DEST ATJAP-------
11BS 205R16CD840THI---10048--
12BS 205/R16CD697THI-4293--29311304
13BS 205/55R16AR20INDO-1-----
14BS 225/55R16T001JAP----2--
15BS 225/60R16AR20INDO--2--22
16BS 255/70R16T697INDO--1--11
17TTL-13725222001479252211-2533
18PSR (HRD)BS 215/45R17T001JAP-------
19BS 225/45R17RE050AJAP--26--2626
20BS 225/45R17EA03JAP-------
21FS 225/45R17TZ700JAP-------
22BS 225/65R17T697INDO--24--2424
23BS 225/70R17D697THI--50--5050
24BS 275/45R20DSPORTJAP-------
25BS 295/35R20S001JAP------11
26BS 315/35R20SPORTJAP--3--33
27TTL--103--1031-104
28LTRBS 650R16R230JAP-82193505219219
29BS 700R16R230JAP-3161193700611931193
30BS 750R16R230JAP-12219105253836491059105
31BS 195/70R15CR623JAP--389--389389
32BS 205/70R15CR624INDO-171030--1030111919
33BS 215/70R15CR624INDO-401225--12251225
34BS 235/65R16CR660TR---28---
35TTL-160213161361637513161-11113050
36LSRBS 750R16VSJJAP-444939--49394939
37BS 825R16R180JAP-------
38TTL-444939--4939--4939
39TBRBS 1200R20G580JAP-357026413967146026412641
40BS 1200R20R187JAP-60748110019481481
41BS 1400R20VSJJAP-34436--43611425
42BS 1400R20R180JAP-------
43BS 1400R20R180BZJAP-------
44BS 11R22.5R187JAP--128--128128
45BS 12R22.5R187JAP--79--7979
46BS 13R22.5R187JAP---82--
47BS 275/70R22.5R294JAP--166--166166
48BS 315/80R22.5R184JAP-34222213
49BS 315/80R22.5R184THI-8948-12948948
50BS 1200R24G580JAP-1351661098492166166
51TTL-438850475175198750471115037
In & Out Balance
Cell Formulas
RangeFormula
E17:M17E17=SUM(E3:E16)
E27:M27E27=SUM(E18:E26)
F29F29=310+6
E35:M35E35=SUM(E28:E34)
E38:M38E38=SUM(E36:E37)
F40F40=586+21
J39:J50,J36:J37,J28:J34,J18:J26,J3:J16J3=G3
M39:M50,M36:M37,M28:M34,M18:M26,M3:M16M3=J3+K3-L3
E51:M51E51=SUM(E39:E50)

notice: it's not necessary every part should be filled number when replace formula with blank cell as the last picture

the most important if there is any number for any part then should fill formula for all parts for TTL row .
 
thanks
seem to something missed
if fill any value for any each part contains TTL should return the formula for all of TTL rows for two both columns ARRIVED & SALES even if some parts are blank cell without any value .
it's not necessity to fill numbers for each part to return the formula again . just search any part contains values in columns AARIVED , SALES , then fill the formulas for all of TTL rows .
sorry about this detail could be missed!
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I'm confused, so either all TTL rows have a formula or nine have a formula ?

If that is the case, before the macro starts are the TTL rows blank or with formula ?

Once I have answers I will look at it tomorrow.
 
Upvote 0
I don't understand what you mean nine word
before the macro starts are the TTL rows blank or with formula ?
before run the macro contains formula and based on your last version it will clear the formula for all of TTL rows when they are empty cells precede TTL rows and this what I want.
the problem If one of TTL rows fill numeric values just in column Arrived into Blank cell will just add the formula for the TTL row for the column Arrived has been filled numeric values and ignore adding formula to SALES column and ignore adding formula to Arrived & SALES columns for others parts contain TTL rows.
so the first case is ok as I have ever said based on your last version.
second case when add one of two columns or two columns together(Arrived ,Sales) for one part or two part whatever how many fill numeric values for each TTL row , directly should adding formula to Arrived & SALES columns for others parts contain TTL rows , even if some parts are Blank cell ( there is no values for columns Arrived & sales at all) .
note: I'm struggling for post some pictures based on XL2BB , despite of the same size as in OP , but gives message about limiting 10000 characters and try decreasing data but still gives the message
if you still need pictures I will capture by tool from win system .
 
Last edited:
Upvote 0
Not 100% clear but try this:

VBA Code:
Sub AddTotals_v03()

    Dim sht As Worksheet
    Dim lastRow As Long, lastCol As Long, totalRow As Long
    Dim rngSize As Range, firstCell As Range, totalCell As Range
    Dim rngLatest As Range, sumrngLatest As Double, cntValues As Long
    Dim strToFind As String, FirstAddr As String
    Dim strFormula As String
    
    strToFind = "TTL"
    
    Set sht = Worksheets("In & Out Balance")
    With sht
        lastCol = .Cells(2, Columns.Count).End(xlToLeft).Column
        lastRow = .Cells(Rows.Count, "B").End(xlUp).Row
        Set rngSize = .Range("B2:B" & lastRow)
        Set firstCell = .Range("B2")
        ' Last 2 data columns (exclude last row total column)
        Set rngLatest = .Range(.Cells(3, lastCol - 2), .Cells(lastRow, lastCol - 1))
    End With
    
    ' Test for no values in Latest columns and exit if no values
    sumrngLatest = Application.WorksheetFunction.Sum(rngLatest)
    
    If sumrngLatest = 0 Then
        rngLatest.ClearContents
        Exit Sub
    End If
    
    ' Values exist contintue to add totals
    Set totalCell = rngSize.Find(What:=strToFind, After:=rngSize.Cells(1, 1), LookIn:=xlFormulas, _
                                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                MatchCase:=False, SearchFormat:=False)
       
    If Not totalCell Is Nothing Then FirstAddr = totalCell.Address
    
    Do
        totalRow = totalCell.Row
        With sht
            Set rngLatest = .Range(.Cells(totalRow, lastCol - 2), .Cells(totalRow, lastCol))
        End With
        
        With rngLatest
            .FormulaR1C1 = sht.Cells(totalRow, lastCol - 3).FormulaR1C1
        End With
    
        Set totalCell = rngSize.FindNext(After:=totalCell)
        If totalCell Is Nothing Then Exit Do
    
    Loop Until totalCell.Address = FirstAddr
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,103
Messages
6,123,110
Members
449,096
Latest member
provoking

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