动态检查单元格值是否存在多次

时间:2016-09-28 20:02:50

标签: excel vba excel-vba

我有一行带标题的数据,相同的标题可以显示function barCodeBuilder(stockName) { // Setting Ranges from prices table var sheet = SpreadsheetApp.getActive(); var listNameRange = sheet.getRangeByName('MeterailsPricing!ITEM NAME'); var listSkuRange = sheet.getRangeByName('MeterailsPricing!INVENTORY SKU'); var listPriceRange = sheet.getRangeByName('MeterailsPricing!PRICE'); var listSupRange = sheet.getRangeByName('MeterailsPricing!SUPPLIER CODE'); //Errors begin here = TypeError: Cannot read property "length" from null for(listCounter = 0; listCounter < listNameRange.length; listCounter++){ if(stockName == listNameRange[listCounter][0]){ var barCode = { sku:listSkuRange[listCounter][0], supCode:listSupRange[listCounter][0], price:listPriceRange[listCounter][0]}; } } //final bar code assembly var finalBarCode = barCode.sku + "-" + barCode.supCode + "-" + barCode.price; return finalBarCode; } 次。我需要在VBA中循环遍历标头,检查每个标题中是否存在多个标题,如果是,则在每个标题的末尾添加一个字符串。

n

,结果应该在循环之后(如:

HEADER 1 HEADER 2 HEADER 3 HEADER 1 HEADER 2 HEADER 3

我知道我需要循环遍历细胞,但逻辑逃脱了我

HEADER 1 OLD HEADER 2 OLD HEADER 3 OLD HEADER 1 NEW HEADER 2 NEW HEADER 3 NEW

for循环之后的逻辑让我失望

1 个答案:

答案 0 :(得分:1)

可能不是最好的方法,但你可以在列表中循环两次并进行比较。如果你有很多标题,这可能会很慢。

For i = 1 to lastCol - 1
    numDuplicates = 0
    for j = i + 1 to lastCol
        if ws.Cells(1,i) = ws.Cells(1,j) then
            numDuplicates = numDuplicates + 1
            ws.Cells(1,j) = ws.Cells(1,j) & " " & numDuplicates
        end if
    next j
next i

那样的东西?这将保留原始标题,然后使用1,2,...

标记重复项