Oh-your code is in a worksheet module so if you refer to Range or Columns without specifying a worksheet it refers to the sheet containing the code and you can't select anything on that sheet unless it is active.
Private Sub CB_Bakery_Click()
Dim wsM As Worksheet, wsM2 As Worksheet
Dim LRow2 As String
'sets the variables wsM equal to Main worksheet and wsM2 equal to Main2
Set wsM = Sheets("Main")
Set wsM2 = Sheets("Main2")
'sets variable LRow2 equal to the last row in column A of the
'activesheet that contains data
LRow2 = ActiveSheet.Range("A65535").End(xlUp).Row
'select the Main worksheet
With wsM2
'select all the cells
wsM.UsedRange.AutoFilter Field:=3, Criteria1:="Bakery"
wsM.Columns("b:b").Copy .Range("a1")
wsM.Columns("d:d").Copy .Range("b1")
wsM.Columns("f:f").Copy .Range("c1")
wsM.Columns("q:q").Copy .Range("e1")
wsM.Columns("r:r").Copy .Range("f1")
wsM.Columns("g:g").Copy .Range("g1")
wsM.Columns("bm:bm").Copy .Range("h1")
wsM.Columns("bn:bn").Copy .Range("i1")
wsM.Columns("bo:bo").Copy .Range("j1")
wsM.Columns("bp:bp").Copy .Range("k1")
'put a value in column D cell D1
.Range("D1").Value = "Due"
'value in D2 equal to the formula
.Range("D2").Formula = "=c2-(TODAY()-182)"
'copy the formula
.Range("D3").Copy Destination:=.Range("D4:D" & LRow2)
'set conditional formating THE NEXT LINE CAUSES THE ERROR
With .Columns("D:D")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="30"
.FormatConditions(1).Font.ColorIndex = 2
.FormatConditions(1).Interior.ColorIndex = 2
.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="1", Formula2:="30"
.FormatConditions(2).Font.ColorIndex = 2
.FormatConditions(2).Interior.ColorIndex = 37
.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=TODAY()-182"
.FormatConditions(3).Font.ColorIndex = 2
.FormatConditions(3).Interior.ColorIndex = 9
End With
'set widths
.Columns("A:A").ColumnWidth = 15
.Columns("B:B").ColumnWidth = 12
.Range("C:C,E:F").ColumnWidth = 7
.Columns("D:D").ColumnWidth = 4
.Columns("F:F").EntireColumn.AutoFit
End With
End Sub
Bookmarks