Link to home
Start Free TrialLog in
Avatar of Seamus2626
Seamus2626Flag for Ireland

asked on

Adding to code

Hi,

I have the below piece of code and i want to start adding more named ranges like "Today" and "Yesterday"

How can i add on to that code

e.g. my next named ranges would be "MeritToday" & "Merit Yesterday"

Thanks
Seamus
Option Explicit

Public Sub CompareData()
  'compares Today and Yesterday named ranges
  Dim rngToday As Range
  Dim rngYesterday As Range
  Dim rngTcell As Range
  Dim boolFirstTime As Boolean
  
  Set rngToday = ActiveSheet.Range("Today")
  Set rngYesterday = ActiveSheet.Range("Yesterday")
  
  If (rngToday.Columns.Count = rngYesterday.Columns.Count) _
    And (rngToday.Rows.Count = rngYesterday.Rows.Count) Then
  Else
    MsgBox "Sophis Fiscal are Identical!", vbCritical, "Range size difference"
    Exit Sub
  End If
  
  
  boolFirstTime = True
  For Each rngTcell In rngToday
    If rngTcell.Value <> rngYesterday.Cells(rngTcell.Row - rngToday.Row + 1, rngTcell.Column - rngToday.Column + 1).Value Then
    Else
      If boolFirstTime Then
        boolFirstTime = False   'skip header cell
      Else
        Exit Sub
      End If
    End If
  Next
  MsgBox "Data Identical", vbCritical, "Range Data Identical"
End Sub

Open in new window

Avatar of patrickab
patrickab
Flag of United Kingdom of Great Britain and Northern Ireland image

Seamus2626,

Try the code below to add the range names. Change the ranges to which each of them refer to suit your requirements.

Patrick
ActiveWorkbook.Names.Add Name:="MeritToday", RefersToR1C1:="=Sheet1!R1C1:R10C1"
    Range("B1:B10").Select
    ActiveWorkbook.Names.Add Name:="Merit Yesterday", RefersToR1C1:="=Sheet1!R1C2:R10C2"

Open in new window

Avatar of Seamus2626

ASKER

Hi Patrick, is not possible for me to simply name a range like the above code as opposed to having to reference each area?

Thanks
Seamus
ASKER CERTIFIED SOLUTION
Avatar of patrickab
patrickab
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Patrick

Seamus
Seamus - Thanks for the grade - Patrick