Hi everyone,
I'm quite new to VBA on Excel and haven't quite been able to figure out how to get my macro working yet.
I have data that looks like this:
[TABLE="width: 499"]
<tbody>[TR]
[TD]Date[/TD]
[TD]Amount 1[/TD]
[TD]Type[/TD]
[TD]Amount 2[/TD]
[TD]Amount 3[/TD]
[TD]Macro Output[/TD]
[/TR]
[TR]
[TD]01/01/2013[/TD]
[TD]-100[/TD]
[TD]A[/TD]
[TD]-80[/TD]
[TD]-20[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]01/02/2013[/TD]
[TD]-150[/TD]
[TD]B[/TD]
[TD]
[/TD]
[TD]-30[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]03/05/2014[/TD]
[TD]-200[/TD]
[TD]
[/TD]
[TD]
[/TD]
[TD][/TD]
[TD]
[/TD]
[/TR]
</tbody>[/TABLE]
What I would like the macro to do is to look in the Type column (i.e. column C) and to go through each cell in the column looking for whether the cell contains the string "A", "B" or is empty. However, this process should only continue until the cell to its left (in column B) is empty.
Depending on which string the cell in column C contains (or if it is empty), I want to then have a formula entered into the Macro Output column (i.e. column F) on the same row. This formula is as follows:
If cell contains "A" then Macro Output cell = (Amount 3/Amount 2)
If cell contains "B" then Macro Output cell = (Amount 3/Amount 1)
If cell is empty then Macro Output cell = (Amount 3/Amount 1).
If cell is not empty and does not contain "A" or "B" then Macro Output cell = "UNDEFINED"
What I have written at the moment is this:
At the moment this only gives me a #DIV/0! error in the second cell of column F (Macro Output column), but it is the closest I have come so far. (My previous attempts just gave blank cells in that column).
Any help on this would be greatly appreciated.
Thanks in advance,
Alex
I'm quite new to VBA on Excel and haven't quite been able to figure out how to get my macro working yet.
I have data that looks like this:
[TABLE="width: 499"]
<tbody>[TR]
[TD]Date[/TD]
[TD]Amount 1[/TD]
[TD]Type[/TD]
[TD]Amount 2[/TD]
[TD]Amount 3[/TD]
[TD]Macro Output[/TD]
[/TR]
[TR]
[TD]01/01/2013[/TD]
[TD]-100[/TD]
[TD]A[/TD]
[TD]-80[/TD]
[TD]-20[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]01/02/2013[/TD]
[TD]-150[/TD]
[TD]B[/TD]
[TD]
[/TD]
[TD]-30[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]03/05/2014[/TD]
[TD]-200[/TD]
[TD]
[/TD]
[TD]
[/TD]
[TD][/TD]
[TD]
[/TD]
[/TR]
</tbody>[/TABLE]
What I would like the macro to do is to look in the Type column (i.e. column C) and to go through each cell in the column looking for whether the cell contains the string "A", "B" or is empty. However, this process should only continue until the cell to its left (in column B) is empty.
Depending on which string the cell in column C contains (or if it is empty), I want to then have a formula entered into the Macro Output column (i.e. column F) on the same row. This formula is as follows:
If cell contains "A" then Macro Output cell = (Amount 3/Amount 2)
If cell contains "B" then Macro Output cell = (Amount 3/Amount 1)
If cell is empty then Macro Output cell = (Amount 3/Amount 1).
If cell is not empty and does not contain "A" or "B" then Macro Output cell = "UNDEFINED"
What I have written at the moment is this:
Code:
[COLOR=#232323][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana]Option Explicit[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana]Sub MyMacro()[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana]Dim cell As String[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana]Workbooks(“MyWorkbook).Sheets("Sheet1").Cells(2, 2).Select[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana] Do Until ActiveCell.Value = Empty[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana] ActiveCell.Offset(0, 1).Select[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana] Select Case ActiveCell[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana] Case cell = “A”[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana] ActiveCell.Offset(0, 3).FormulaR1C1 = "=RC[-1]/RC[-2]”[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana] Case cell = "Non IRE"[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana] ActiveCell.Offset(0, 3).FormulaR1C1 = "=RC[-1]/RC[-4]”[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana] Case cell = Empty[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana] ActiveCell.Offset(0, 3).FormulaR1C1 = “=RC[-1]/RC[-4]” [/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana] Case Else[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana] ActiveCell.Offset(0, 3) = "UNDEFINED"[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana] End Select[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana] ActiveCell.Offset(1, -4).Select[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana] Loop[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana]End Sub[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#232323][FONT=Verdana]
[/FONT][/COLOR]
At the moment this only gives me a #DIV/0! error in the second cell of column F (Macro Output column), but it is the closest I have come so far. (My previous attempts just gave blank cells in that column).
Any help on this would be greatly appreciated.
Thanks in advance,
Alex
Last edited: