Sub that returns a value - excel

Is there a way that I can call a block of code and get the value that I am looking for and store it as the variable I want and then use it in a different sub?
For example. I want to search all the values in row 1 starting at A1 and going till the there is a blank value. I want to find the cell with the value "Frequency" in it and then return the column index number.
Sub findFrequency()
'find "Frequency" colum and save the column index number
Dim fFreq As String
Dim FreqCol As Variant
Range("A1").Select
fFreq = "Frequency"
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value = fFreq Then
FreqCol = ActiveCell.Column
Exit Do
End If
ActiveCell.Offset(0, 1).Select
Loop
End Sub
Now ideally I can write a different sub and use the value from the above code.
Sub Execute()
Call findFrequency
Cells(5, FreqCol).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
End Sub
I get an error when I run this because the value of FreqCol is not set to anything from running the Call findFrequency line.

Try this:
Function findFrequency()
'find "Frequency" colum and save the column index number
Dim fFreq As String
Dim FreqCol As Variant
Range("A1").Select
fFreq = "Frequency"
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value = fFreq Then
findFrequency = ActiveCell.Column
Exit Do
End If
ActiveCell.Offset(0, 1).Select
Loop
End Function
Sub Execute()
Dim FreqCol As Long
FreqCol = findFrequency()
Cells(5, FreqCol).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
End Sub

Why loop or select a cell to find a value? Simply use .Find
Function findFrequency() As Long
Dim ws As Worksheet
Dim aCell As Range
Set ws = ActiveSheet
With ws
Set aCell = .Columns(1).Find(What:="Frequency", LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then findFrequency = aCell.Column
End With
End Function
Also what should happen if the "Frequency" is not found. You need to cater to that as well.
Sub Execute()
Dim FreqCol As Long
FreqCol = findFrequency
If FreqCol = 0 Then
MsgBox "Frequency not found"
Exit Sub
End If
With Cells(5, FreqCol).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
End Sub

The code would to be like this.
Main Main sub is findFrequency, subprocedure is Sub Execute(rng As Range)
Sub findFrequency()
'find "Frequency" colum and save the column index number
Dim fFreq As String
Dim FreqCol As Variant
Range("A1").Select
fFreq = "Frequency"
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value = fFreq Then
FreqCol = ActiveCell.Column
Execute Cells(5, FreqCol)
Exit Do
End If
ActiveCell.Offset(0, 1).Select
Loop
End Sub
Sub Execute(rng As Range)
With rng.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
End Sub

let me tell you that you should not apologize for asking a basic question about VBA: nobody deserves to spend time with VBA and I'm glad you are more acquainted with other programming languages.
I write my answer for all people not familiar with VBA.
VBA passes arguments by default by reference so you can write your code in this way:
Sub findFrequency(FreqCol as Long)
...
End SUb
Sub Execute()
Dim FreqCol As Long
findFrequency FreqCol
...
End SUb

Related

Excel VBA sheet name list with maxing colors

I am trying to write a VBA function where I produce a new sheet, give a lists of all the sheet names in the workbook and match the cell color of the sheet name, with the tab color of the sheet name. The pseudocode will look something like this:
Create a new sheet
Loop through all sheets in the workbook
Write down the sheet name in the created sheet
Retrieve the sheet ThemeColor (e.g. xlThemeColorLight2)
Retrieve the sheet TintAndShade (e.g. 0.799981688894314
Set the cell in which the name of the sheet is to the correct ThemeColor and TintAndShade
End
Is there a way in which this is possible?
Sub SheetList()
Dim ws As Worksheet
Dim x As Integer
x = 1
Sheets.Add
sheet_name = InputBox("Please enter a sheet name")
ActiveSheet.Name = sheet_name # Create a new sheet name
For Each ws In Worksheets
Sheets(sheet_name).Cells(x, 1) = ws.Name # Set cell value to sheet name
Dim theme_color As String
Dim tint_and_shade As Single
theme_color = ... # Attain sheet ThemeColor of current ws here
tint_and_shade = ... # Attain sheet TintAndShade of current ws here
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = theme_color # Set current cell to theme_color
.TintAndShade = tint_and_shade # Set current cell to tint_and_shade
.PatternTintAndShade = 0
End With
x = x + 1
Next ws
You can use ws.Tab.ThemeColor and ws.Tab.TintAndShade to retrieve those values.
I updated your code so that you can use the wsNewvariable to refer to the new worksheet.
Furthermore I am checking that only color codes of the other worksheets are checked.
Sub SheetList()
Dim wsNew As Worksheet
With ThisWorkbook.Worksheets
Set wsNew = .Add(.Item(1))
End With
Dim sheet_name
sheet_name = InputBox("Please enter a sheet name")
wsNew.Name = sheet_name ' Create a new sheet name
Dim ws As Worksheet, c As Range, x As Long
For Each ws In Worksheets
If Not ws Is wsNew Then
x = x + 1
Set c = wsNew.Cells(x, 1)
c.Value = ws.Name ' Set cell value to sheet name
Dim theme_color As Single
Dim tint_and_shade As Single
theme_color = ws.Tab.ThemeColor ' Attain sheet ThemeColor of current ws here
tint_and_shade = ws.Tab.TintAndShade ' Attain sheet TintAndShade of current ws here
With c.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
If theme_color > 0 Then
.ThemeColor = theme_color ' Set current cell to theme_color
End If
.TintAndShade = tint_and_shade ' Set current cell to tint_and_shade
.PatternTintAndShade = 0
End With
End If
Next ws
End Sub
Thanks for your help Ike. I made a full piece of code to get a sheet overview page. It is not the most elegant piece of code, but here it is:
Sub SheetOverview()
'
' SheetOverview
'
Dim ws As Worksheet
Dim x As Integer
Dim c As Range
x = 1
' Add new sheet, ask user for sheet name
Sheets.Add
ActiveWindow.DisplayGridlines = False
sheet_name = InputBox("Please enter a sheet name")
ActiveSheet.Name = sheet_name
' Loop to obtain all sheet names
For Each ws In Worksheets
Set c = Sheets(sheet_name).Cells(x, 1)
c.Value = ws.Name
Dim theme_color As Single
Dim tint_and_shade As Single
theme_color = ws.Tab.ThemeColor ' Attain sheet ThemeColor of current ws here
tint_and_shade = ws.Tab.TintAndShade ' Attain sheet TintAndShade of current ws here
With c.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
If theme_color > 0 Then
.ThemeColor = theme_color ' Set current cell to theme_color
End If
.TintAndShade = tint_and_shade ' Set current cell to tint_and_shade
.PatternTintAndShade = 0
End With
x = x + 1
Next ws
' Cut selection
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Cut
Range("A6").Select
ActiveSheet.Paste
' Enter Sheets and Description and format
Range("A5").Select
ActiveCell.FormulaR1C1 = "Sheets"
Range("B5").Select
ActiveCell.FormulaR1C1 = "Description"
Range("A5:B5").Select
Selection.Font.Bold = True
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
' Format title
Range("A4").Select
Selection.End(xlUp).Select
Selection.Font.Bold = True
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
Selection.Font.Size = 14
Range("A2").Select
ActiveCell.FormulaR1C1 = "Author:"
Range("B2").Select
ActiveCell.FormulaR1C1 = "[Enter author here]"
Selection.Font.Italic = True
Range("A3").Select
ActiveCell.FormulaR1C1 = "Date:"
Range("B3").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Selection.Copy
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Rows("4:4").Select
Selection.Insert Shift:=xlDown
Selection.Borders(xlLeft).LineStyle = xlNone
Selection.Borders(xlRight).LineStyle = xlNone
Selection.Borders(xlTop).LineStyle = xlNone
Selection.Borders(xlBottom).LineStyle = xlNone
Range("A4").Select
ActiveCell.FormulaR1C1 = "Time:"
Range("B4").Select
ActiveCell.FormulaR1C1 = "=NOW()-TODAY()"
Range("B4").Select
Selection.Copy
Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.NumberFormat = "[$-x-systime]h:mm:ss AM/PM"
Columns("A:B").Select
Range("A5").Activate
Selection.Columns.AutoFit
Range("B1").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
Columns("B:B").ColumnWidth = 52.11
Range("B3:B4").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A1").Select
End Sub
Insert Sheet List
Sub InsertSheetList()
' Define constants.
Const Title As String = "Insert Sheet List"
Const FIRST_CELL As String = "A1"
Dim Headers() As Variant
Headers = VBA.Array("Index", "Color", "Name", "Type")
' Attempt to reference the workbook.
Dim wb As Workbook: Set wb = ActiveWorkbook
If wb Is Nothing Then
MsgBox "No visible workbooks open.", vbExclamation, Title
Exit Sub
End If
' Input sheet name.
Dim SheetName As String
SheetName = InputBox("Please enter a sheet name", Title)
If Len(SheetName) = 0 Then
MsgBox "Dialog canceled.", vbExclamation
Exit Sub
End If
' Add a worksheet to be the first and reference it.
wb.Sheets.Add Before:=wb.Sheets(1)
Dim dws As Worksheet: Set dws = wb.Sheets(1)
' Attempt to rename the worksheet.
Dim ErrNum As Long
On Error Resume Next
dws.Name = SheetName
ErrNum = Err.Number
On Error GoTo 0
If ErrNum <> 0 Then
Application.DisplayAlerts = False
dws.Delete
Application.DisplayAlerts = True
MsgBox "Couldn't use '" & SheetName & "' for a sheet name.", _
vbExclamation, Title
Exit Sub
End If
' Calcuate the number of columns (headers).
Dim ColumnsCount As Long: ColumnsCount = UBound(Headers) + 1
' Write the headers.
Dim drg As Range
Set drg = dws.Range(FIRST_CELL).Resize(, ColumnsCount)
drg.Value = Headers
' Write the list.
Dim sh As Object
Dim r As Long
For Each sh In wb.Sheets
r = r + 1
If r > 1 Then
Set drg = drg.Offset(1)
drg.Cells(1).Value = sh.Index
drg.Cells(2).Interior.Color = sh.Tab.Color
drg.Cells(3).Value = sh.Name
drg.Cells(4).Value = TypeName(sh)
End If
Next sh
' Autofit.
dws.Columns(1).Resize(, ColumnsCount).AutoFit
' Inform.
MsgBox "Sheet list created.", vbInformation, Title
End Sub

VBA invalid procedure call or argument: Conditional formatting for items stored in an array

I want to clear and reset the conditional formatting rules for my workbook. There are groups of values that I want conditionally formatted to show as Green, Yellow, Orange, Red, and Pink. The values for each color group are stored in an array.
Below is my code so far, but only showing the for loops for green and yellow. I get the error on this line Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual,Formula1:=item
Sub ResetFormat()
Dim ws As Worksheet
Dim item As Variant
Dim arrGreen As Variant
arrGreen = Array(Worksheets("Drop down").Range("N11:N14"))
Dim arrYellow As Variant
arrYellow = Array(Worksheets("Drop down").Range("O11:O13"))
Dim arrOrange As Variant
arrOrange = Array(Worksheets("Drop down").Range("P11:P14"))
Dim arrRed As Variant
arrRed = Array(Worksheets("Drop down").Range("Q11:Q14"))
Dim arrPink As Variant
arrPink = Array(Worksheets("Drop down").Range("R11:R12"))
For Each ws In Sheets
Cells.Select
Selection.Cells.FormatConditions.Delete
For Each item In arrGreen
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:=item
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 5287936
.TintAndShade = 0
End With
Next item
For Each item In arrYellow
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:=item
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 49407
.TintAndShade = 0
End With
Next item
Next ws
End Sub
What worked for me was using a range instead of an array, and adding the following line:
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
The resulting code:
Sub ResetFormat()
Dim ws As Worksheet
Dim item As Variant
Dim rngGreen As Range
Set rngGreen = Worksheets("Drop down").Range("N11:N14")
Dim arrYellow As Range
Set rngYellow = Worksheets("Drop down").Range("O11:O13")
Dim rngOrange As Range
Set rngOrange = Worksheets("Drop down").Range("P11:P14")
Dim rngRed As Range
Set rngRed = Worksheets("Drop down").Range("Q11:Q14")
Dim rngPink As Range
Set rngPink = Worksheets("Drop down").Range("R11:R12")
For Each ws In Sheets
ws.Activate
Cells.Select
Selection.Cells.FormatConditions.Delete
For Each item In rngGreen
Cells.Select
Selection.FormatConditions.Add Type:=xlTextString, String:=item, TextOperator:=xlEqual
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.Color = 5287936
End With
Selection.FormatConditions(1).StopIfTrue = False
Next item
For Each item In rngYellow
Cells.Select
Selection.FormatConditions.Add Type:=xlTextString, String:=item, TextOperator:=xlEqual
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.Color = 49407
End With
Selection.FormatConditions(1).StopIfTrue = False
Next item
Next ws
End Sub

Problems getting "with selection" to work

I'm working on a excel vba code to import and manipulate some data from CSV-file. Suddenly a part of my code didn't work any more though it had worked without problems before.
It is about range.select and afterward with selection.Interior.Pattern = xlSolid
I have tried to copy the same small part of the code to a different workbook and here it work just perfect.
Dim iPhase As Integer
iPhase = Application.WorksheetFunction.CountIf(Range("A:A"), "Phase")
Dim h As Integer
h = 1
Range("A6").Select
Do Until h > iPhase
Cells.Find(What:="Phase", after:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveSheet.Range(ActiveCell, ActiveCell.Offset(0, 16)).Select
With selection.Interior
.Pattern = xlSolid
.Interior.PatternColorIndex = xlAutomatic
.Interior.ThemeColor = xlThemeColorAccent6
.Interior.TintAndShade = 0
.Interior.PatternTintAndShade = 0
End With
With selection.Font
.Bold = True
End With
h = h + 1
Loop
I get a compile error: Expected function or variable #"selection.interior"
The comments already identify the issues with your code; but here is an alternative using Filter and SpecialCells to select the visible data. Comments are contained in the code.
Sub FliterWithConditionalFormatting()
Dim rng As Range
'properly defing and reference your workbook and worksheet, change as requiried
Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1").CurrentRegion
'The WITH..END WITH statement allows you to shorten your code and avoid using SELECT and ACTIVATE
With rng
.AutoFilter Field:=1, Criteria1:="Phase", Operator:=xlAnd 'filter the rng
'set the range, to conditionally format only the visible data, skipping the header row
With .Range(Cells(2, 1), Cells(rng.Rows.Count, 17)).SpecialCells(xlCellTypeVisible)
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = xlThemeColorAccent6
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With .Font
.Bold = True
End With
End With
.AutoFilter 'Remove the filter
End With
End Sub

Only select value according to highlighted cells

I am trying a code which would select a value from a Data Validation Dropdown list only if the cell, which is two blocks to its left, is highlighted. But I am not able to figure out how to do this. Can anyone help please? Thanks
I have the following code which is wrong but just as an example:
Sub AssignBided()
Worksheets("Monday").Select
With Worksheets("Monday")
If Hilight.range("B12") = True Then
range("B12").Activate
ActiveCell.Offset(0, -2).Select
.Selection.Value = "ABC"
End If
End With
End Sub
The code to highlight cells is as follows:
Sub Hilight(RNG As range, Hilight As Boolean)
With RNG.Interior
If Hilight Then
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = RGB(100, 250, 150)
.TintAndShade = 0
.PatternTintAndShade = 0
Else
.Pattern = xlNone
.PatternTintAndShade = 0
End If
End With
End Sub
The hilight Sub is used as follows:
Dim L8Product As String
Dim i As Long
Dim L8Rnge As range
L8Product = range("Line8_P_Mon")
'Line 8 Resource requirement code
'Determine if change was made in cell B39
If Not Intersect(Target, Me.range("Line8_P_Mon")) Is Nothing Then
Hilight range("Line8_Hilight_Mon"), False
'Below Code searches in the KP and Osgood Table and then highlights the
appropriate cells
If Trim(L8Product) <> "" Then
With Sheets("Products").range("KP_Table")
'searchs in the KP Table on Sheet Overtime_Pos_Table
'The code below will search the KP table for the product that you will select from the Line 8 drop down
Set L8Rnge = .Find(what:=L8Product, _
after:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)
If Not L8Rnge Is Nothing Then
Hilight range("KP_Hilight_Mon"), True
'Hilights the cells for the KP and the Prep material required
Else: With Sheets("Products").range("Osgood_Table")
Set L8Rnge = .Find(what:=L8Product, _
after:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)
If Not L8Rnge Is Nothing Then
Hilight range("Osgood_Hilight_Mon"), True
'Hilights the cells for the Osgood and the Prep material required
End If
End With
End If
End With
Else: Hilight range("Line8_Hilight_Mon"), False
End If
End If
Hope the question is clear. Thank you in advance.
You can create a simple function to check for highlighting...
'use a constant to store the highlight color, since it's used
' in multiple places in your code
Const HIGHLIGHT_COLOR = 9894500 'RGB(100, 250, 150)
Sub AssignBided()
With Worksheets("Monday")
If IsHighlighted(.Range("B12")) Then
.Range("B12").Offset(0, 2).Value = "ABC" 'changed offset from -2...
End If
End With
End Sub
'Is a cell highlighted? EDIT: changed the function name to IsHighlighted
Function IsHighlighted(c As Range)
IsHighlighted = (c.Interior.Color = HIGHLIGHT_COLOR)
End Function
Sub Hilight(RNG As Range, Hilight As Boolean)
With RNG.Interior
If Hilight Then
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = HIGHLIGHT_COLOR '<< use contant here
.TintAndShade = 0
.PatternTintAndShade = 0
Else
.Pattern = xlNone
.PatternTintAndShade = 0
End If
End With
End Sub

Find the Difference between two rows and Highlight the difference then loop through all Rows with the Same Code

I Set up a Macro that Finds the differences between the two rows and then highlights them. I want the macro to Cycle through the next two rows and do the same thing and go on until there are no more rows of data(This Number varies all the time). So the Next selection would be Rows 4:5 and it would Select the differences and highlight them and so on. How is this possible? Any help is greatly appreciated. Thank you,
FindVariance Macro
Rows("2:3").Select
Range("A3").Activate
Selection.ColumnDifferences(ActiveCell).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("F16").Select
End Sub
Try:
FindVariance Macro
For j=2 to Range("A1").End(xlDown).Row-1
i=j+1
Rows(j & ":" & i).ColumnDifferences(Range("A" & i)).Offset(1,0).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
j=j+1
Next j
End Sub
Here's my sample
Option Explicit
Sub FindVariance()
Dim last As Integer, i As Integer, r As Boolean
last = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
For i = 2 To last
If i Mod 2 = 0 Then
Rows(i & ":" & i + 1).Select
r = Selection.ColumnDifferences(ActiveCell).Select
If r = True Then
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
End If
Next i
Range("F16").Select
End Sub
it's always a good habit to:
use objects reference and avoid the use of selections
which can be deceiving and slows down the code
use full reference for ranges, up to the workbook.
to avoid point to an unwanted active sheet or workbook!
so here's my code
Sub FindVariance()
Dim j As Long
Dim nRows As Long
With ActiveSheet
nRows = .Cells(.Rows.Count, 1).End(xlUp).Row
For j = 2 To nRows Step 2
With .Rows(j).Resize(2).ColumnDifferences(.Cells(j + 1, 1)).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Next j
End With
End Sub
and there's still some job to do in order to catch and properly treat exceptions (uneven number of rows, empty rows...)

Resources