Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding to code

Posted on 2011-02-25
5
Medium Priority
?
191 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:Seamus2626
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 45

Expert Comment

by:patrickab
ID: 34981667
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

0
 

Author Comment

by:Seamus2626
ID: 34981715
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
0
 
LVL 45

Accepted Solution

by:
patrickab earned 2000 total points
ID: 34983088
>is not possible for me to simply name a range like the above code as opposed to having to reference each area?

I don't understand the point you are making. Do please explain. The code  I gave can and will work.
0
 

Author Closing Comment

by:Seamus2626
ID: 34996155
Thanks Patrick

Seamus
0
 
LVL 45

Expert Comment

by:patrickab
ID: 34996205
Seamus - Thanks for the grade - Patrick
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question