在不重复信息VBA的情况下抓取数据

时间:2017-01-26 16:21:17

标签: vba excel-vba excel

我正在为下面的数据创建大量列标题,这些标题留空。

表格示例

Col 1 | Col 2 | Col3 | Col 4 |
blank | info | blank| info  |

我用来收集这些空白并将其粘贴到' debug.print'中的代码。显示需要信息的列..最终结果应为:
缺少的信息是:
Col1
Col3
它比我的例子大很多,请有人帮助我在哪里出错,因为它在我循环时重复了这些信息。

Do Until iCol = 34
    If Cells(iRow, iCol) = "" Then
        CellMissInfo = CellMissInfo & CurrTitleCol & ": " & vb_
        Debug.Print (CellMissInfo)
    End If
    iCol = iCol + 1
Loop

编辑: 还有一些代码可供帮助

iRow = 3
iCol = 22
lastRow = Application.WorksheetFunction.CountA _
(Range("B2", Range("B" & Rows.Count).End(xlUp)))

Do While iRow <= lastRow

    If CurrPipeC <> RGB(242, 220, 219) Then 'Pipeline Color'
        Do Until iCol = 34
'HERE is my problem'
            If Cells(iRow, iCol) = "" Then
                CellMissInfo = CellMissInfo & CurrTitleCol & ": " & vb_
            End If
            iCol = iCol + 1
        Loop

新想法:也许如果我将列创建为数组,然后通过LBound运行到Ubound以获取将名称串在一起的列名?

1 个答案:

答案 0 :(得分:0)

问题是你在循环遍历列时连接字符串,并且每次都打印字符串。

在我看来,你有两个选择:建立一个所有缺失数据的字符串并在最后打印它。或者在找到时打印出每个缺失的项目

选项1: 最后建立一个字符串并打印:

Do Until iCol = 34
    If Cells(iRow, iCol) = "" Then
        CellMissInfo = CellMissInfo & CurrTitleCol & ": " & vb_
    End If
    iCol = iCol + 1
Loop
Debug.Print (CellMissInfo)

选项2: 在找到时打印每个缺失的项目

Do Until iCol = 34
    If Cells(iRow, iCol) = "" Then
        CellMissInfo = CurrTitleCol & ": " & vb_
        Debug.Print (CellMissInfo)
    End If
    iCol = iCol + 1
Loop