Need help with comparison vba code


"Excel VBA Help"

 


Play Friv Now - Play Friv Games
Go Back   The Code Cage Forums > Microsoft Topics > Microsoft Excel Forum
Our Info Pages:-
The Code Cage - Microsoft Office help

Notices

Microsoft Excel Forum Members Post questions in this forum if they are related to using Microsoft Excel, this includes Worksheet Functions, Excel VBA Programming, Simple Macro Code & Charting, Excel formula etc.

New Reply
Thread Tools Rate Thread Display Modes
  #1  
Unread 16th February 2012, 19:37
Audioboxer's Avatar
Audioboxer Audioboxer is offline
Junior Member
MS Office Version: Ms Office 2010
MS Office Skill Level: No VBA Skills
 
Join Date: Feb 2012
Location: Manchester, new Hampshire
Posts: 2
Posts Per Day: 0.00
My Top Tip Count: 0
Thanks to others: 0
Thanked 0 Times in 0 Posts
Reputation: 10 (3) Audioboxer is on a distinguished road
Default Need help with comparison vba code
     Excel: VBA   


------ Register to get rid of these "In Post" ads! ------

Category: Excel: VBA   

Hi everyone,

I found this code below to compare columns a and d. The second version I added (colorduplicates2) I added Two textboxes so that I can input the range i want to compare in the however It's not Working how I have it. Any ideas how to manipulate it so it will pull whats in the text boxes? Thank you!

VBA CODE:
Sub ColorDuplicates() 
     
    For i = 1 To Range("B65536").End(xlUp).Row 
        If Application.WorksheetFunction.CountIf(Range("A:A"), Range("D" & i)) = 1 Then 
            With Range("D" & i).Font 
                .ColorIndex = 3 
                .Bold = True 
            End With 
        End If 
    Next i 
    For i = 1 To Range("A65536").End(xlUp).Row 
        If Application.WorksheetFunction.CountIf(Range("D:D"), Range("A" & i)) = 1 Then 
            With Range("A" & i).Font 
                .ColorIndex = 3 
                .Bold = True 
            End With 
        End If 
    Next i 
     
     
End Sub 
 
 
 
Sub ColorDuplicates2() 
     
    For i = 1 To Range("B65536").End(xlUp).Row 
        If Application.WorksheetFunction.CountIf(Range("TextBox1.Value"), Range("LEFT(TextBox2,1)" & i)) = 1 Then 
            With Range("LEFT(TextBox2,1)" & i).Font 
                .ColorIndex = 3 
                .Bold = True 
            End With 
        End If 
    Next i 
    For i = 1 To Range("A65536").End(xlUp).Row 
        If Application.WorksheetFunction.CountIf(Range("TextBox2.Value"), Range("LEFT(TextBox1,1" & i)) = 1 Then 
            With Range("LEFT(TextBox1,1" & i).Font 
                .ColorIndex = 3 
                .Bold = True 
            End With 
        End If 
    Next i 
     
     
     
End Sub 


- via Tapatalk.


Last edited by RoyUK; 16th February 2012 at 19:43.
The Code Cage Advertisment
Advertisement
  #2  
Unread 16th February 2012, 19:45
RoyUK's Avatar
RoyUK RoyUK is offline
Administrator
Solution Centre Professional
MS Office Version: MS Office 2003, 2007, 2010
MS Office Skill Level: Proficient with MS Office
 
Join Date: Nov 2008
Location: Derbyshire, UK
Posts: 2,686
Posts Per Day: 1.20
My Top Tip Count: 4
Thanks to others: 2
Thanked 194 Times in 192 Posts
Reputation: 3305 (66) RoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond repute
4 Top Tips
England
Default Re: Need help with comparison vba code

------ Register to get rid of these "In Post" ads! ------


What type of textboxes have you used?

Also,

Code or Formula tags
help

Help

Guidline 3 Please read Guideline 3
It is preferable to use code tags around code or formula. Posting code without them makes your code hard to read and difficult to be copied for testing. It's as simple as clicking a button!, the code button is marked VBA and the formual one is marked E=MC2
If you need further help with using the code tags don't hesitate to ask a member of staff or even another member!



__________________
Hope that helps, RoyUK
For tips & examples visit free excel help
DatabaseForm
About me

  #3  
Unread 16th February 2012, 20:10
RoyUK's Avatar
RoyUK RoyUK is offline
Administrator
Solution Centre Professional
MS Office Version: MS Office 2003, 2007, 2010
MS Office Skill Level: Proficient with MS Office
 
Join Date: Nov 2008
Location: Derbyshire, UK
Posts: 2,686
Posts Per Day: 1.20
My Top Tip Count: 4
Thanks to others: 2
Thanked 194 Times in 192 Posts
Reputation: 3305 (66) RoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond repute
4 Top Tips
England
Default Re: Need help with comparison vba code

------ Register to get rid of these "In Post" ads! ------


Try this code instead
VBA CODE:
Option Explicit 
 
 
Sub FindDupes() 
    Dim rDupe As Range 
    Dim rData As Range 
    Dim rCl As Range 
    Dim myRange As Range 
     
     'Use InputBox to get first cell of column to check
    Set myRange = Application.InputBox(Prompt:="Please select cell to search from", _ 
    Title:="Find Dupes", Type:=8) 
     'if no selection then quit
    If myRange Is Nothing Or myRange.Cells.Count > 1 Then Goto exit_proc 
     'get the range to check
    Set rData = Range(myRange, myRange.End(xlDown)) 
     'loop to find duplicates
    For Each rCl In rData 
        If WorksheetFunction.CountIf(rData, rCl.Value) > 1 Then 
             'build a range of duplicates
            If rDupe Is Nothing Then 
                Set rDupe = rCl 
            Else: Set rDupe = Union(rDupe, rCl) 
            End If 
        End If 
    Next rCl 
     'colour the duplicates
    rDupe.Interior.ColorIndex = 3 
    Exit Sub 
exit_proc: 
    MsgBox "You didn't select a cell to search from. Cancelling", vbCritical, "Input required" 
End Sub 
__________________
Hope that helps, RoyUK
For tips & examples visit free excel help
DatabaseForm
About me

The Code Cage Advertisment
Advertisement
  #4  
Unread 17th February 2012, 20:13
Audioboxer's Avatar
Audioboxer Audioboxer is offline
Junior Member
MS Office Version: Ms Office 2010
MS Office Skill Level: No VBA Skills
 
Join Date: Feb 2012
Location: Manchester, new Hampshire
Posts: 2
Posts Per Day: 0.00
My Top Tip Count: 0
Thanks to others: 0
Thanked 0 Times in 0 Posts
Reputation: 10 (3) Audioboxer is on a distinguished road
Default Re: Need help with comparison vba code

------ Register to get rid of these "In Post" ads! ------


Quote:
Originally Posted by RoyUK View Post

Try this code instead
VBA CODE:
Option Explicit 
 
 
Sub FindDupes() 
    Dim rDupe As Range 
    Dim rData As Range 
    Dim rCl As Range 
    Dim myRange As Range 
     
     'Use InputBox to get first cell of column to check
    Set myRange = Application.InputBox(Prompt:="Please select cell to search from", _ 
    Title:="Find Dupes", Type:=8) 
     'if no selection then quit
    If myRange Is Nothing Or myRange.Cells.Count > 1 Then Goto exit_proc 
     'get the range to check
    Set rData = Range(myRange, myRange.End(xlDown)) 
     'loop to find duplicates
    For Each rCl In rData 
        If WorksheetFunction.CountIf(rData, rCl.Value) > 1 Then 
             'build a range of duplicates
            If rDupe Is Nothing Then 
                Set rDupe = rCl 
            Else: Set rDupe = Union(rDupe, rCl) 
            End If 
        End If 
    Next rCl 
     'colour the duplicates
    rDupe.Interior.ColorIndex = 3 
    Exit Sub 
exit_proc: 
    MsgBox "You didn't select a cell to search from. Cancelling", vbCritical, "Input required" 
End Sub 

Thank you Roy, However in your code above it seems to only compare a single column or data, I am trying to compare one column of data to another. Is it easy enough to add that option? I'm going to try and tinker with it in the mean time. Thanks again!

  #5  
Unread 18th February 2012, 09:11
RoyUK's Avatar
RoyUK RoyUK is offline
Administrator
Solution Centre Professional
MS Office Version: MS Office 2003, 2007, 2010
MS Office Skill Level: Proficient with MS Office
 
Join Date: Nov 2008
Location: Derbyshire, UK
Posts: 2,686
Posts Per Day: 1.20
My Top Tip Count: 4
Thanks to others: 2
Thanked 194 Times in 192 Posts
Reputation: 3305 (66) RoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond reputeRoyUK has a reputation beyond repute
4 Top Tips
England
Default Re: Need help with comparison vba code

------ Register to get rid of these "In Post" ads! ------


The code can compare one column to another with a sloght change
VBA CODE:
Option Explicit 
Sub FindDupes() 
    Dim rDupe As Range 
    Dim rData As Range 
    Dim rData2 As Range 
    Dim rCl As Range 
    Dim myRange As Range 
    Dim myRange2 As Range 
     
     'Use InputBox to get first cell of column to check
    Set myRange = Application.InputBox(Prompt:="Please select cell to search from", _ 
    Title:="Find Dupes", Type:=8) 
     'if no selection then quit
    If myRange Is Nothing Or myRange.Cells.Count > 1 Then Goto exit_proc 
     'Use InputBox to get first cell of column to check
    Set myRange2 = Application.InputBox(Prompt:="Please select cell to search from", _ 
    Title:="Find Dupes", Type:=8) 
     'if no selection then quit
    If myRange2 Is Nothing Or myRange2.Cells.Count > 1 Then Goto exit_proc 
     'get the range to check
    Set rData = Range(myRange, myRange.End(xlDown)) 
    Set rData2 = Range(myRange2, myRange2.End(xlDown)) 
     'loop to find duplicates
    For Each rCl In rData 
        If WorksheetFunction.CountIf(rData2, rCl.Value) > 1 Then 
             'build a range of duplicates
            If rDupe Is Nothing Then 
                Set rDupe = rCl 
            Else: Set rDupe = Union(rDupe, rCl) 
            End If 
        End If 
    Next rCl 
     'colour the duplicates
    rDupe.Font.ColorIndex = 3 
    Exit Sub 
exit_proc: 
    MsgBox "You didn't select a cell to search from. Cancelling", vbCritical, "Input required" 
End Sub 
__________________
Hope that helps, RoyUK
For tips & examples visit free excel help
DatabaseForm
About me

The Code Cage Advertisment
Advertisement

To stop seeing these ads and get other benefits check This page!

New Reply
The Code Cage at Facebookhe Code Cage at Twitter


Tags
code, comparison, vba

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

The Code Cage Affilliates



http://www.thecodecage.com/forumz tested by McAfee Security http://www.thecodecage.com/forumz tested by McAfee Security

All times are GMT +1. The time now is 01:52.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Template-Modifications by TMS
No part of this board, functionality or creatives may be copied or reproduced either in part or full without the express permission of The Code Cage Team.
Copyright ©2008 - , Simon lloyd.
We are not associated with nor employed by Microsoft in any way, we simply provide resources!
All MS office icons are registered trademarks of the application they represent and are used with permission from Microsoft
UA-8135493-1
Cultural Forum | Study at Malaysian University