conditonal formatting in VB


G

Guest

All,

i'e got some VB that is working beautifully putting in a formula on a sheet
it then goes on to create various other sheets. The problem is I'm pretty new
to VB have recorded little macro to do conditional formatting of cells and
used (i thought) same rules, but always get compile error when I insert C
formatting instructions after formula one.

here is bit that works

ActiveSheet.Name = "BB SCM " & FormatDateTime(Date, vbLongDate)



myval = Application.CountIf(ActiveSheet.Cells, "NPQ") - 1


Range("Q6").Select
ActiveCell.FormulaR1C1 = _
"=AVERAGE(IF(RC[-13]:RC[-2]>0,RC[-13]:RC[-2],RC[-13]))"
Range("Q6").Select
Selection.FormulaArray = _
"=AVERAGE(IF(RC[-13]:RC[-2]>0,RC[-13]:RC[-2],RC[-13]))"


Dim counter As Integer

For counter = 1 To myval


Cells(6 + (counter * 8), 17).Select
ActiveCell.FormulaR1C1 = _
"=AVERAGE(IF(RC[-13]:RC[-2]>0,RC[-13]:RC[-2],RC[-13]))"
Cells(6 + (counter * 8), 17).Select
Selection.FormulaArray = _
"=AVERAGE(IF(RC[-13]:RC[-2]>0,RC[-13]:RC[-2],RC[-13]))"



Range("A6").Select
ActiveCell.FormulaR1C1 = "Stk Wk"
Range("B6").Select
ActiveCell.FormulaR1C1 = "=IF(RC[15]>0,R[-1]C[2]/RC[15],""NF"")"
Range("B6").Select
Selection.NumberFormat = "0.0"


Cells(6 + (counter * 8), 1).Select
ActiveCell.FormulaR1C1 = "Stk Wk"
Cells(6 + (counter * 8), 2).Select
ActiveCell.FormulaR1C1 = "=IF(RC[15]>0,R[-1]C[2]/RC[15],""NF"")"
Cells(6 + (counter * 8), 2).Select
Selection.NumberFormat = "0.0"

Range("Q1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Fc ave"



Next counter


here is bit for one cell that I wish to copy down to all releveant cells as
above

conditionalformatting Macro
' Macro recorded 12/10/2007 by jaberesf
'

'
Range("B6").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="2"
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.ColorIndex = 3
End With
Range("B7").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=""1.2*D6""", Formula2:="=""0.8*D6"""
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.ColorIndex = 3
End With
 
Ad

Advertisements

G

Guest

Since you are just learning VBA, then one leson to learn RIGHT NOW is that
there is rarely a need to SELECT a cell or a range. So, your code

Range("Q6").Select
ActiveCell.FormulaR1C1 = _
"=AVERAGE(IF(RC[-13]:RC[-2]>0,RC[-13]:RC[-2],RC[-13]))"
Range("Q6").Select
Selection.FormulaArray = _
"=AVERAGE(IF(RC[-13]:RC[-2]>0,RC[-13]:RC[-2],RC[-13]))"

could be simplified to

Range("Q6").FormulaArray = _
"=AVERAGE(IF(RC[-13]:RC[-2]>0,RC[-13]:RC[-2],RC[-13]))"

Similarly, this section of code

Cells(6 + (counter * 8), 17).Select
ActiveCell.FormulaR1C1 = _
"=AVERAGE(IF(RC[-13]:RC[-2]>0,RC[-13]:RC[-2],RC[-13]))"
Cells(6 + (counter * 8), 17).Select
Selection.FormulaArray = _
"=AVERAGE(IF(RC[-13]:RC[-2]>0,RC[-13]:RC[-2],RC[-13]))"

Range("A6").Select
ActiveCell.FormulaR1C1 = "Stk Wk"
Range("B6").Select
ActiveCell.FormulaR1C1 = "=IF(RC[15]>0,R[-1]C[2]/RC[15],""NF"")"
Range("B6").Select
Selection.NumberFormat = "0.0"


can be shortened to

Cells(6 + (counter * 8), 17).FormulaArray = _
"=AVERAGE(IF(RC[-13]:RC[-2]>0,RC[-13]:RC[-2],RC[-13]))"

Range("A6") = "Stk Wk"
With Range("B6")
.FormulaR1C1 = "=IF(RC[15]>0,R[-1]C[2]/RC[15],""NF"")"
.NumberFormat = "0.0"
End With

and so on.


JBW said:
All,

i'e got some VB that is working beautifully putting in a formula on a sheet
it then goes on to create various other sheets. The problem is I'm pretty new
to VB have recorded little macro to do conditional formatting of cells and
used (i thought) same rules, but always get compile error when I insert C
formatting instructions after formula one.

here is bit that works

ActiveSheet.Name = "BB SCM " & FormatDateTime(Date, vbLongDate)



myval = Application.CountIf(ActiveSheet.Cells, "NPQ") - 1


Range("Q6").Select
ActiveCell.FormulaR1C1 = _
"=AVERAGE(IF(RC[-13]:RC[-2]>0,RC[-13]:RC[-2],RC[-13]))"
Range("Q6").Select
Selection.FormulaArray = _
"=AVERAGE(IF(RC[-13]:RC[-2]>0,RC[-13]:RC[-2],RC[-13]))"


Dim counter As Integer

For counter = 1 To myval


Cells(6 + (counter * 8), 17).Select
ActiveCell.FormulaR1C1 = _
"=AVERAGE(IF(RC[-13]:RC[-2]>0,RC[-13]:RC[-2],RC[-13]))"
Cells(6 + (counter * 8), 17).Select
Selection.FormulaArray = _
"=AVERAGE(IF(RC[-13]:RC[-2]>0,RC[-13]:RC[-2],RC[-13]))"



Range("A6").Select
ActiveCell.FormulaR1C1 = "Stk Wk"
Range("B6").Select
ActiveCell.FormulaR1C1 = "=IF(RC[15]>0,R[-1]C[2]/RC[15],""NF"")"
Range("B6").Select
Selection.NumberFormat = "0.0"


Cells(6 + (counter * 8), 1).Select
ActiveCell.FormulaR1C1 = "Stk Wk"
Cells(6 + (counter * 8), 2).Select
ActiveCell.FormulaR1C1 = "=IF(RC[15]>0,R[-1]C[2]/RC[15],""NF"")"
Cells(6 + (counter * 8), 2).Select
Selection.NumberFormat = "0.0"

Range("Q1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Fc ave"



Next counter


here is bit for one cell that I wish to copy down to all releveant cells as
above

conditionalformatting Macro
' Macro recorded 12/10/2007 by jaberesf
'

'
Range("B6").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="2"
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.ColorIndex = 3
End With
Range("B7").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=""1.2*D6""", Formula2:="=""0.8*D6"""
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.ColorIndex = 3
End With
 
Ad

Advertisements

G

Guest

as far as your conditional formatting question goes, try something along
these lines

Dim r As Range
Set r = Range("b7")
With r
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, _
Operator:=xlBetween, _
Formula1:="=1.2*" & .Offset(-1, 2).Address, _
Formula2:="=0.8*" & .Offset(-1, 2).Address
with .FormatConditions(1).Font
.Bold = True
.Italic = False
.ColorIndex = 3
END with
end with
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top