dmikhailov4
New Member
- Joined
- Jul 23, 2018
- Messages
- 10
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Reference
[/TD]
[TD]Result of Formula
[/TD]
[/TR]
[TR]
[TD]Dog
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]Dog
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]Cat
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]Cat
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]Cat
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]Horse
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]Sheep
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]Sheep
[/TD]
[TD]2
[/TD]
[/TR]
</tbody>[/TABLE]
Hi,
I’m hoping someone can help me with writing a VBA code to create a function that converts a formula I use to an easier function, using existing Excel Functions (namely, INDIRECT, ADDRESS, ROW, COLUMN):
Regular Formula
=IF(A2=INDIRECT(ADDRESS(ROW(A2)-1,COLUMN(A2))),INDIRECT(ADDRESS(ROW()-1,COLUMN())),IF(INDIRECT(ADDRESS(ROW()-1,COLUMN()))=1,2,1))
…into this
=GROUPBANDING(Ref,Val1,Val2)…where in the above formula Ref=A2, Val1=1, Val2=2.
Description of whatthe formula is doing:
As per the table above, IF the Ref cell equals the row above it, the Result equals the Result above that (in the 2nd column). Ifnot, it equals an alternative value. In this case, the numbers alternatebetween 1 and 2 (later, I use this to conditionally format all the 2s to befilled with another color – it’s like colored banding, but instead ofalternating between every row, it alternates between every group of referencecells - I can't get colors to work in the example table above, though).
My current VBA Code:
-------------------------------------------------------
Function GROUPBANDING(Ref, Val1, Val2)
If Range(Ref) =INDIRECT(ADDRESS(ROW(Ref) - 1, COLUMN(Ref))) Then
End Function
-------------------------------------------------------
Error message I get:
“Compile Error: Sub or Function not defined” and points to the first instance of “ROW”
I’ve not needed to make Functions in the past so I’m not as adept at these, so any help or guidance would be appreciated! Thanks!
<tbody>[TR]
[TD]Reference
[/TD]
[TD]Result of Formula
[/TD]
[/TR]
[TR]
[TD]Dog
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]Dog
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]Cat
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]Cat
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]Cat
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]Horse
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]Sheep
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]Sheep
[/TD]
[TD]2
[/TD]
[/TR]
</tbody>[/TABLE]
Hi,
I’m hoping someone can help me with writing a VBA code to create a function that converts a formula I use to an easier function, using existing Excel Functions (namely, INDIRECT, ADDRESS, ROW, COLUMN):
Regular Formula
=IF(A2=INDIRECT(ADDRESS(ROW(A2)-1,COLUMN(A2))),INDIRECT(ADDRESS(ROW()-1,COLUMN())),IF(INDIRECT(ADDRESS(ROW()-1,COLUMN()))=1,2,1))
…into this
=GROUPBANDING(Ref,Val1,Val2)…where in the above formula Ref=A2, Val1=1, Val2=2.
Description of whatthe formula is doing:
As per the table above, IF the Ref cell equals the row above it, the Result equals the Result above that (in the 2nd column). Ifnot, it equals an alternative value. In this case, the numbers alternatebetween 1 and 2 (later, I use this to conditionally format all the 2s to befilled with another color – it’s like colored banding, but instead ofalternating between every row, it alternates between every group of referencecells - I can't get colors to work in the example table above, though).
My current VBA Code:
-------------------------------------------------------
Function GROUPBANDING(Ref, Val1, Val2)
If Range(Ref) =INDIRECT(ADDRESS(ROW(Ref) - 1, COLUMN(Ref))) Then
INDIRECT(ADDRESS(ROW() - 1, COLUMN()))
ElseIf INDIRECT(ADDRESS(ROW() - 1, COLUMN())) = Val1 Then
Val2
Else
Val1
End If
End Function
-------------------------------------------------------
Error message I get:
“Compile Error: Sub or Function not defined” and points to the first instance of “ROW”
I’ve not needed to make Functions in the past so I’m not as adept at these, so any help or guidance would be appreciated! Thanks!