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 .
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I am not very clear on what you are trying to do, so lets start with this.

VBA Code:
Sub AddTotals()

    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 strToFind As String, FirstAddr 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")
    End With
    
    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
        With sht
            totalRow = totalCell.Row
            .Range(.Cells(totalRow, lastCol - 2), .Cells(totalRow, lastCol)).FormulaR1C1 = _
                                                .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
Hi Alex,
thanks for your code after many bumps !
I no know why this is not clear .
all of what I want macro clear the formula are existed in TTL row for two last columns Arrived & Sales when the cells where precede TTL row for two last columns Arrived & Sales are empty .
and if when the cells where precede TTL row for two last columns Arrived & Sales contain numbers then return the formula in TTL row for two last columns Arrived & Sales
BTW: your code shows the formula for two both cases whether when the cells where precede TTL row for two last columns Arrived & Sales are empty or if cells where precede TTL row for two last columns Arrived & Sales contain numbers .
the first case should clear formula to become empty when the cells are empty where precede TTL row for two last columns Arrived & Sales .
I hope to give thing maybe you help .
 
Upvote 0
What I think you are saying doesn't seem to make sense.
You seem to be saying that if all cells in K3:K16 are blank then you don't want a total formula in K17 (and the same for Column L).
Is that correct ?
(if it is that a very strange way of setting up a total line in a spreadsheet)
 
Upvote 0
What I think you are saying doesn't seem to make sense.
yes you're right , but you don't know why I want this way.

the main reason I have macro pulls data from another sheet based on all of empty cells for two last columns Arrived & Sales even TTL row , but if all of cells are empty for two last columns Arrived & Sales even if TTL row contain formula ,then the macro will not pull the numbers to fill into last columns Arrived & Sales from another sheet becuase contains formula in TTL row
You seem to be saying that if all cells in K3:K16 are blank then you don't want a total formula in K17 (and the same for Column L).
Is that correct ?
yes that's correct.
(if it is that a very strange way of setting up a total line in a spreadsheet)
you're right , but as I said this depends on another macro to pull data
my problem is stopping macro to pull numbers from another sheet if the TTL row contains formula .
so after pulling the numbers in blank cells , then will return the formula again as I earlier mentioned in OP .
I hope to understand my needing for fix that.
 
Upvote 0
See if this works for you.

VBA Code:
Sub AddTotals()

    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 rngTTL_Latest As Range, 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")
    End With
    
    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 rngTTL_Latest = .Range(.Cells(totalRow, lastCol - 2), .Cells(totalRow, lastCol))
        End With
        
        With rngTTL_Latest
            .FormulaR1C1 = sht.Cells(totalRow, lastCol - 3).FormulaR1C1

            ' Arrived Total - Clear if no values
            With .Cells(1)
                strFormula = Replace(.Formula, "Sum", "Count")
                cntValues = Evaluate(strFormula)
                If cntValues = 0 Then .Value = Empty
            End With
            
             ' Sales Total - Clear if no values
            With .Cells(2)
                strFormula = Replace(.Formula, "Sum", "Count")
                cntValues = Evaluate(strFormula)
                If cntValues = 0 Then .Value = Empty
            End With
        End With
    
        Set totalCell = rngSize.FindNext(After:=totalCell)
        If totalCell Is Nothing Then Exit Do
    
    Loop Until totalCell.Address = FirstAddr
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,078
Messages
6,122,997
Members
449,093
Latest member
masterms

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