Home » excel » excel – Write a formula into a cell depending on another cell value

excel – Write a formula into a cell depending on another cell value

Posted by: admin March 9, 2020 Leave a comment

Questions:

I was hoping to write a Macro that does a very repetitive task for me but entering VBA is harder than expected. I will learn how to program macros for excel when I have some time because it seem extremely useful, but I can’t spend 5 to 12 hours this week.

Maybe someone here can help!

I have a few excel files that follow this pattern:

Column C - Column D
--------------------
text     | (empty)
number   | (empty)
number   | (empty)
text     | (empty)
number   | (empty)
text     | (empty)
text     | (empty)
number   | (empty)
text     | (empty)
number   | (empty)

Where text and number alternate randomly for a few thousand cells. I need column D to hold, when column C is a number, the difference with previous number, otherwise it must stay blank:

Column C - Column D
--------------------
text     | (empty)
3        | (empty)
14       | (=C3-C2) : 11
text     | (empty)
16       | (=C5-C3) : 2
text     | (empty)
text     | (empty)
21       | (=C8-C5) : 5
22       | (=C9-C8) : 1

So the algorithm is:

var previousNumberCell
var i = 1

for all (selected) cells/rows 
 if (Row(i).column(C) holds number) {
   Row(i).column(D).value = "=C"+i+"-"C"+previousNumberCell
   previousNumberCell = i;
 }
 i++

End

I don’t care if for the first or last cell it doesn’t work.

Thank you so much for the help, or if you can point me to where I can find the answer to this.

EDIT: this is a simplified version of the problem, there are 2 things I don’t know how do well with excel macros: select a cell, and tell if cell is a number… for the record, number cells have been converted from text to number format.

How to&Answers:

Give this a shot:

Sub MyMacro()
Dim rng as Range
Dim cl as Range
Dim lastNum as Range
Set rng = Range(Selection.Address) 'Make sure that your active SELECTION is correct before running the macro'

If Not rng.Columns.Count = 1 Then
    MsgBox "Please select only 1 column of data at a time.",vbCritical
    Exit SUb
Else:
    For each cl in rng
        If IsNumeric(cl.Value) Then
            If lastNum Is Nothing Then
                cl.Offset(0,1).Formula = "=" & cl.Address
            Else:
                cl.Offset(0,1).Formula = "=" & cl.Address & "-" & lastNum.Address

            End If
            set lastNum = cl
        End If
    Next
End If

End Sub

Answer:

Do you require VBA?

Insert a new Column before column C
Column C with your values becomes column D
You might need columnheaders..
In cell C2 put: =IF(E2=0;0;SUM(E$2:$E2)) this identifies rows with number
In cell E2 put: =IF(ISNUMBER(D2);1;0) this sets order for each row with a number to use next in vlookup
in cell F2 put: =IF(ISNUMBER(D2);ABS(D2-VLOOKUP(MAX($C$1:C1);$C$1:D1;2;0));"")
Autofill columns C, E and F.
In column F you get your results, except first, which is “#VALUE”

Answer:

Hi you can do this with an if formula and a named formula . if (isnumber ,named formula,0)

named formula (=lookup formula)