Compare values in worksheets

addey

New Member
Joined
Jun 30, 2014
Messages
9
Hi,

I have figured out some of the stuff I want to do below, but would love to have it all in one VB macro, since it is an action I will have to perform somewhat regularly on rather large excel files, so I'm hoping for a bit of help. I haven't done a lot of VB, but enough to see that it should be pretty easy to do, but I haven't programmed in years, and am very far from being fluent in VB... (can read it rather easily though)

Worksheet 1 has an item-specific number (in the format xx.xx.xxx if that matters) in column G, and item quantity in column K
Worksheet 2 has the items split over several rows (in various places in the worksheet), but keeps the item-specific number (only this time in column D). It has item quantity in column F
Both worksheets have several lines without items (headers, info etc) that don't need to be taken into account. For these lines, the item-specific number cell is empty.

What I would like is the following: For each item in the first worksheet, sum up total quantity of all appearances of same item (given by the item-specific number) in the second worksheet. If this total matches the quantity in worksheet 1, mark the cell green, if it does not match, mark the cell red.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
addey,

Welcome to the MrExcel forum.

Are you using a PC or a Mac?


Can you post screenshots of the two actual raw data worksheets?

And, can you post screenshots of the worksheet results (manually formatted by you) that you are looking for?

To post your data, you can download and install one of the following two programs:
Excel Jeanie
Download

MrExcel HTMLMaker20101230
https://onedrive.live.com/?cid=8cffdec0ce27e813&sc=documents&id=8CFFDEC0CE27E813!189

Installation instructions here:
http://www.mrexcel.com/forum/board-announcements/515787-forum-posting-guidelines.html#post2545970


If you are not able to give us screenshots:
You can upload your workbook to Box Net,
sensitive data changed
mark the workbook for sharing
and provide us with a link to your workbook.
 
Upvote 0
Hiker95, thanks for the reply. I'm sorry, but I'm not able to do that... I am not allowed to install any programs on my PC (i.e. not a Mac), and security regulations don't allow me to upload any data either. The best I can do is try to provide an example in text here, just hope the columns line up...:

Worksheet 1
A ... F G H ... K
1 3.10.10 Item1 10
2 3.10.20 Item2 15
.
.
N 4.10.50 ItemN 43

Worksheet 2
A B C D E G
1 3.10.10 Item1 3
2 3.10.20 Item2 10
3 3.10.10 Item1 3
4 3.10.10 Item1 3
5 3.10.10 Item2 5
.
.
N-1 4.10.50 ItemN 22
N 4.10.50 ItemN 21

Output:
A ... F G H ... K
1 3.10.10 Item1 10 (red)
2 3.10.20 Item2 15 (green)
.
.
N 4.10.50 ItemN 43 (green)


Edit: That obviously didn't work... I really don't have any possibility to upload the real data, very sorry... I thought the description was rather clear, so I still hope someone can help me.
 
Last edited:
Upvote 0
addey,

I am not able to tell what cells, rows, columns, your text data is in.


Click on the Reply to Thread button, and just put the word BUMP in the thread. Then, click on the Post Quick Reply button, and someone else will assist you.
 
Upvote 0
Have fiddled a bit with it myself, and this seems to do the trick. More or less... since it's the first VBA code I've ever written myself, I guess there are a lot of ways this could be done better, but this seems to work (number of rows limited because I've just been testing on a small subset)

Code:
Sub CheckQuantities()
Dim ItemID As String, ItemIDcheck As String
Dim ItemQuantity As Long
Dim ItemCount As Integer, ItemAdd As Integer
Dim i As Integer
Dim j As Integer

Worksheets("Sheet1").Activate
For i = 5 To 10 Step 1
  If Not IsEmpty(Cells(i, "D")) Then
    ItemID = Cells(i, "D").Value
    ItemQuantity = Cells(i, "F").Value
  End If
  
  ItemCount = 0
  ItemAdd = 0
  
    For j = 5 To 11 Step 1
     If Worksheets("Sheet2").Cells(j, "E").Value = ItemID Then
      ItemAdd = Worksheets("Sheet2").Cells(j, "G").Value
      ItemCount = ItemCount + ItemAdd
     End If
    Next j
   If ItemCount = ItemQuantity Then
     Cells(i, "F").Interior.Color = RGB(0, 256, 0)
    Else
     Cells(i, "F").Interior.Color = RGB(256, 0, 0)
   End If
Next i
End Sub
 
Upvote 0
addey,

Nicely done.

number of rows limited because I've just been testing on a small subset

Without seeing your raw data, the only suggestion I have is to change the Integer variables to Long.


since it's the first VBA code I've ever written myself

See if anything in my most up to date list, in the below links will help you:

Training / Books / Sites as of 6/26/2014

What Is VBA?
VBA is an acronym for Visual Basic for Applications. VBA should not be confused with VB, which is standard Visual Basic. Visual Basic for Applications is a programming feature designed by Microsoft for use with their Microsoft Office

MrExcel's Products: Books, CDs, Podcasts Discuss topics related to Holy Macro! Products: Learn Excel from MrExcel, VBA and Macros for Microsoft Excel,Holy Macro! It's 2500 VBA Examples CD, Guerilla Data Analysis Using Microsoft Excel and Excel Knowledge Base CD and the MrExcel Podcasts.
MrExcel's Products: Books, CDs, Podcasts

There are over 1800 Excel videos/tutorials here:
ExcelIsFun - YouTube

Getting Started with VBA.
http://www.datapigtechnologies.com/ExcelMain.htm

If you are serious about learning VBA try
Macros Made Easy for Microsoft Excel

Excel Tutorials and Tips - VBA - macros - training
Excel Tutorial | Excel Tips | Excel Articles

Free VBA Course
Free VBA Course

Excel 2007 VBA materials to learn here:
VBA for Excel 2007 tutorial-VBA programming?

Here's a good primer on the scope of variables.
Understanding Scope

Using Variables in Excel VBA Macro Code
Excel VBA Variables. Using Variables in Excel VBA Macro Code

See David McRitchie's site if you just started with VBA
Getting Started with Macros and User Defined Functions

What is a VBA Module and How is a VBA Module Used?
What Is A VBA Module and how is a VBA Module Used?

Events And Event Procedures In VBA
Events In Excel VBA

Here is a good introductory tutorial using a VBA Class:
Classes In VBA

There's a chapter on classes in VBA Developer's Handbook which is also good.
VBA Developer's Handbook, 2nd Edition: Ken Getz, Mike Gilbert: 0025211229781: Amazon.com: Books

Ron's Excel Tips
Excel for Windows Tips

Ron de Bruin's Mail from Excel and make/mail PDF files (Windows)
Mail from Excel and make/mail PDF files (Windows)

Anthony's Excel VBA Page - Excel Application - Excel Consultant - Excel Consulting (see 3 tutorials in Excel VBA Basic Tutorial Series)

BET: Microsoft Excel Visual Basic

Debugging VBA
Debug Excel VBA Macro Code
TechBookReport - Debugging Excel VBA Code

Start at the beginning...

Creating An XLA Add-In For Excel, Writing User Defined Functions In VBA
Creating An XLA Add In

Build an Excel Add-In
VBA Tips: Build an Excel Add-In

Creating custom functions
Creating custom functions - Excel

Writing Your First VBA Function in Excel
Writing Your First VBA Function in Excel | Custom Functions

VBA for Excel (Macros)
Excel Macros (VBA) Tutorial

Excel Macros Tutorial
Excel Macros (VBA) Tutorial

Excel Macros & Programming
EXCEL Macros Tutorial

VBA Lesson 11: VBA Code General Tips and General Vocabulary
VBA for Excel macros language

Excel VBA -- Adding Code to a Workbook
http://www.contextures.com/xlvba01.html

Beyond Excel's recorder

Helpful as a simple concise reference for the basics, and, using the macro recorder is endlessly powerful.
MS Excel: Cells

Learn to debug:
Debugging VBA

How To: Assign a Macro to a Button or Shape
How To: Assign a Macro to a Button or Shape - Peltier Tech Blog

User Form Creation
http://www.contextures.com/xlUserForm01.html

Build a UserForm for Excel
http://www.fontstuff.com/ebooks/free/fsuserforms.pdf

When To Use a UserForm & What to Use a UserForm For
When to use Userform & What To Use Them For. Excel Training VBA 2 lesson 2

Excel Tutorials / Video Tutorials - Functions
http://www.contextures.com/xlFunctions02.html

How to insert Buttons, radio buttons and check boxes in Excel
How to insert Buttons, radio buttons and check boxes in Excel - Bing Videos

INDEX MATCH - Excel Index Function and Excel Match Function
http://www.contextures.com/xlFunctions03.html

Multi or two way vlook up and index match tutorial
Get Digital Help - Excel formulas, array formulas, vba, charts, pivot tables, templates and much more.

Excel Data Validation
http://www.contextures.com/xlDataVal08.html#Larger
http://www.contextures.com/excel-data-validation-add.html

Excel -- Data Validation -- Create Dependent Lists
http://www.contextures.com/xlDataVal02.html

Your Quick Reference to Microsoft Excel Solutions
XL-CENTRAL.COM : For your Microsoft Excel Solutions

New! Excel Recorded Webinars
http://www.datapigtechnologies.com/ExcelMain.htm

Fuzzy Matching - new version plus explanation

Programming The VBA Editor - Created by Chip Pearson at Pearson Software Consulting LLC
This page describes how to write code that modifies or reads other VBA code.
Programming In The VBA Editor

VBA and Macros for Microsoft Excel, by Bill Jelen "Mr.Excel" and Tracy Syrstad

Excel Hacks 100 Industrial-Strength Tips & Tools, by David & Traina Hawley

VBA and Macros for Microsoft Excel 2007, by Bill Jelen "Mr.Excel" and Tracy Syrstad

John Walkenbach's power programming with Excel books.

Excel 2010 Power Programming with VBA, Mr. Spreadsheet's Bookshelf

by Stephen/ Bovey, Rob/ Green, John Bullen (Paperback - Feb 11, 2005)
Professional Excel Development

by Rob Bovey, Stephen Bullen, John Green, and Robert Rosenberg (Paperback - Sep 26, 2001)
Excel 2002 VBA: Programmers Reference

Professional Excel Development by Rob Bovey, Dennis Wallentin, Stephen Bullen, & John Green

DonkeyOte: My Recommended Reading, Volatility
Volatile Excel Functions -Decision Models

A list of Reference Books
AJP Excel Information

Sumproduct
http://www.xldynamic.com/source/xld.SUMPRODUCT.html

Arrays
VBA Array Basics
VBA Arrays
Excel: Introduction to Array Formulas - Xtreme Visual Basic Talk
Excel VBA Chpter 21: Array in Excel VBA

Array Dimensions in Visual Basic - Working with Dimensions (code and graphics)
Array Dimensions in Visual Basic

Shortcut Keys in Excel 2000 through 2007

Pivot Intro
Using Pivot Tables and Pivot Charts in Microsoft Excel
Office 2010 Class #36: Excel PivotTables Pivot Tables 15 examples (Data Analysis) - YouTube
Office 2010 Class #36: Excel PivotTables Pivot Tables 15 examples (Data Analysis) - YouTube
Getting Started with Pivot Tables
http://www.contextures.com/xlPivot01.html#Start
Overview of PivotTable and PivotChart reports
Overview of PivotTable and PivotChart reports - Excel
Build a Pivot Table in Excel VBA
Excel VBA - Build A Pivot Table In Excel VBA Tutorials

Conditional Formatting
Use a formula to apply conditional formatting - Excel

Email from XL - VBA
Mail from Excel example pages

Outlook VBA
Writing VBA code for Microsoft Outlook

Excel Function Dictionary by Peter Noneley
http://www.xlfdic.com/
www.xlfdic.com Excel Function Dictionary 150 example functions and formula.

Function Translations
Excel 2007 function name translations - Dictionary Chart Front Page

Dynamic Named Ranges
http://www.contextures.com/xlNames01.html

How to create Excel Dashboards
http://www.contextures.com/excel-dashboards.html
Excel Dashboards - Templates, Tutorials, Downloads and Examples | Chandoo.org - Learn Microsoft Excel Online
Excel Dashboards - Templates, Tutorials, Downloads and Examples | Chandoo.org - Learn Microsoft Excel Online
Free Microsoft Excel Dashboard Widgets to Download
AJP Excel Information - Gauge

Excel Dashboard / Scorecard Ebook
Excel Dashboards and Scorecards Ebook | How to Create Dashboards in Excel

Mike Alexander from Data Pig Technologies
Amazon.com: Excel 2007 Dashboards and Reports For Dummies (9780470228142): Michael Alexander: Books

Templates
CPearson.com Topic Index
http://www.contextures.com/excel-template-golf-scores.html
Free Microsoft Excel Template Links & Search Engine

Microsoft Excel Cascading Listboxes Tutorial
Microsoft Excel Cascading Listboxes Tutorial - YouTube

Date & Time stamping:
McGimpsey & Associates : Excel : Time and date stamps

Get Formula / Formats thru custom functions:
Show FORMULA or FORMAT of another cell

A nice informative MS article "Improving Performance in Excel 2007"
Improving Performance in Excel 2007

Progress Meters
AJP Excel Information - Progress meters
xcelfiles.com

How to convert text to numbers in Excel
How to convert text to numbers in Excel

How to parse data from the web - Brett Fret has a video in YouTube
Excel VBA - Easily parse XML data from a file or website using XML Maps - YouTube

Excel VBA MAC shortcuts
http://www.mrexcel.com/forum/excel-questions/465614-mac-shortcuts-excel.html

Macintosh Keyboard Shortcuts to use in Microsoft Excel
Excel keyboard shortcuts - mac excel

Excel shortcut and function keys - Excel

And, as your skills increase, try answering threads on sites like:
MrExcel.com | Excel Resources | Excel Seminars | Excel Products
Excel Help Forum
Excel Templates | Excel Add-ins and Excel Help with formulas and VBA Macros
Expert Microsoft Help, with all of the Microsoft Applications from Excel programming, Access training to our free Microsoft VBA Forums
Excel, Access, PowerPoint and Word VBA Macro Automation Help

If you are willing to spend money for the training, then something here should work for you...
Amazon.com: excel tutorial dvd

Advanced Excel Training - Online Excel Course
Advanced Excel Training - Online Excel Course

Excel VBA Programmer Interview - Excel Test for interview
Excel VBA Programmer Interview
 
Last edited:
Upvote 0
What would be the advantage of changing from Integer to Long? I have now added a check of number of used rows in both sheets, and use this to limit the for loops. Thanks for the links, that's a whole lot of information to browse through!!
 
Upvote 0
addey,

What would be the advantage of changing from Integer to Long?

It would depend on how may rows your raw data uses, but, just to be safe, use Long.

Integer variables are stored as 16-bit (2-byte) numbers ranging in value from -32,768 to 32,767.

Long (long integer) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647.


Thanks for the links, that's a whole lot of information to browse through!!

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top