Excel VBA Duplicates in one columns unique in another

时间:2015-06-30 19:22:17

标签: excel vba excel-vba

I have been attempting to solve this for a couple of days and have been able not come up with or find a similar solution. I am trying to highlight duplicates in a single column where their are distinct values in another.

For example occasionally in column G there are duplicate names but they only need to be marked(highlighted) when the value column D is unique. So using the example below the end result should only highlight Elizabeth Moore.

Column D    Column G
116023339   Alan Fluder
116023339   Alan Fluder
116023347   Elizabeth Moore
116025757   Elizabeth Moore
116025048   A. Lavoie

If it helps below is the code I used as my starting point.

Sub test()

Dim cel As Variant
Dim myCell As Variant
Dim myrng As Range
Dim myRange As Range
Dim CellValue As Long

Set myrng = Range("G2:G" & Range("G" & Rows.Count).End(xlUp).Row)
Set myRange = Range("D2:D" & Range("D" & Rows.Count).End(xlUp).Row)

For Each cel In myrng
    If Application.WorksheetFunction.CountIf(myrng, cel) > 1 Then
        For Each myCell In myRange
            If Application.WorksheetFunction.CountIf(myRange, myCell) = 1        Then
                myCell.Offset(0, 3).Interior.ColorIndex = 6
            End If
        Next myCell
    End If
Next cel

End Sub

I should add that my current solution now is to separate the loops where one highlights all the column G dups then the second loop unhighlights where column D is duped.

1 个答案:

答案 0 :(得分:0)

如果你可以使用公式@Tim的建议效果很好。修复VBA:

import UIKit

class ViewController: UIViewController, NSURLConnectionDelegate {

lazy var data = NSMutableData()

@IBOutlet weak var searchField: UITextField!

@IBOutlet weak var name: UILabel!

@IBOutlet weak var summonerLevel: UILabel!

@IBOutlet weak var profileIconId: UILabel!

@IBAction func enterButton(sender: AnyObject) {

    startConnection()

}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func startConnection(){
    let urlPath: String = "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/soon2challenger?api_key=(removed my private api key for obvious reasons)"
    var url: NSURL = NSURL(string: urlPath)!
    var request: NSURLRequest = NSURLRequest(URL: url)
    var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
    connection.start()
}

func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
    self.data.appendData(data)
}

func buttonAction(sender: UIButton!){
    startConnection()
}


func connectionDidFinishLoading(connection: NSURLConnection!) {
    var err: NSError
    // throwing an error on the line below (can't figure out where the error message is)
    var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary

    let include: AnyObject = jsonResult.objectForKey(searchField.text)!

    var name1: AnyObject = include.objectForKey("name")!
    var summLevel: AnyObject = include.objectForKey("summonerLevel")!
    var profIconId: AnyObject = include.objectForKey("profileIconId")!

    name.text = name1 as? String
    profileIconId.text = profIconId as? String
    summonerLevel.text = summLevel as? String

    println(name1)
    println(summLevel)
    println(profIconId)

  }
}

如果列已同步,则不需要内部For

我使用ROW_OFFSET常量来强调列之间的对齐

Dupes and Uniques