Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a formula into a cell depending on another cell value

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.

like image 438
Daren Avatar asked Apr 03 '13 11:04

Daren


People also ask

How do you auto populate a cell based on data from another cell?

Anyone who has used Excel for some time knows how to use the autofill feature to autofill an Excel cell based on another. You simply click and hold your mouse in the lower right corner of the cell, and drag it down to apply the formula in that cell to every cell beneath it (similar to copying formulas in Excel).

How do you write an IF THEN formula in Excel?

Use the IF function, one of the logical functions, to return one value if a condition is true and another value if it's false. For example: =IF(A2>B2,"Over Budget","OK") =IF(A2=B2,B4-A4,"")

How do you write an IF THEN formula in Excel with multiple criteria?

Another way to get an Excel IF to test multiple conditions is by using an array formula. To complete an array formula correctly, press the Ctrl + Shift + Enter keys together. In Excel 365 and Excel 2021, this also works as a regular formula due to support for dynamic arrays.


2 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
like image 51
David Zemens Avatar answered Oct 13 '22 01:10

David Zemens


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"

like image 44
Alin I Avatar answered Oct 13 '22 03:10

Alin I