简体   繁体   English

在Excel中先行再行

[英]Loop through rows then columns in Excel

I have a range of data in excel which is uniform, Column A has a description, Column B has a unique ID and column C is blank. 我在excel中有一系列数据是统一的,A列具有描述,B列具有唯一ID,C列为空白。 Then the next 3 columns have the same set of data. 然后,接下来的3列具有相同的数据集。 I am trying to get my vba loop to go down the two sets of data and compare the unique ID's, if there are differences it copies it to the range of data to a new sheet. 我正在尝试让我的vba循环查看两组数据并比较唯一ID,如果存在差异,它将把它复制到数据范围中并复制到新的工作表中。

The issue is it is not just 6 columns of data its about a couple of hundred, so once the final row has been reached after checking the first range of data, the loop needs to move over 6 columns to begin the process again. 问题在于,不仅有大约200列的6列数据,所以一旦在检查了第一个数据范围之后到达了最后一行,则循环需要移到6列以上才能再次开始该过程。

I am having some difficulty getting the loop to move across 6 columns once the finalrow has been reached 一旦到达最终行,我很难让循环在6列上移动

Sub finddata()

Dim s As Worksheet
Dim uniqueId As String
Dim finalrow As Long
Dim i As Long
Dim c As Long
Dim rngSearch As Range
Dim rngFound As Range
Dim finalcolumn As Long
Dim offset As Integer

Application.ScreenUpdating = True

uniqueId = Sheets("Data").Range("B2").Value
finalrow = Sheets("Data").Range("G100000").End(xlUp).Row
finalcolumn = Sheets("Data").Range("XFD1").End(xlToLeft).Column
offset = 3

Set s = Sheets("Data")
Set rngSearch = s.Range(s.Cells(2, 5), s.Cells(finalrow, 5))

Sheets("DataValidation").Range("A1:C100000").ClearContents

If i = finalrow GoTo 'guessing this is how to being to loop to move over columns
    For i = 2 To finalrow
        uniqueId = s.Cells(i, 2).Value
        Set rngFound = rngSearch.Find(What:=uniqueId, LookIn:=xlValues, LookAt:=xlWhole)
        If rngFound Is Nothing Then
            s.Range(Cells(i, 1), Cells(i, 6)).Copy
            Sheets("DataValidation").Range("A1048575").End(xlUp).offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
            End If
    Next i

MsgBox "Done"
End Sub

Try that one 试试那个

For j = 1 to finalcolumn step 6
    Set rngSearch = s.Range(s.Cells(2, j - 1 + 5), s.Cells(finalrow, j - 1 + 5))
    For i = 2 To finalrow
        uniqueId = s.Cells(i, 2 + j - 1).Value
        Set rngFound = rngSearch.Find(What:=uniqueId, LookIn:=xlValues, LookAt:=xlWhole)
            If rngFound Is Nothing Then
                s.Range(Cells(i, 1 + j - 1), Cells(i, 6 + j - 1)).Copy
                Sheets("DataValidation").Range("A1048575").End(xlUp). _
                               offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
            End If
    Next i
Next j

Hope it helps 希望能帮助到你

Here is a short example of how you can iterate on uniform ranges of 3 columns, and moving right by offset of 6. You said your ranges are uniform so I hard-coded the initial value of rngSet. 这是一个简短的示例,说明如何在3列的统一范围内进行迭代,并向右偏移6个偏移量。您说您的范围是统一的,因此我对rngSet的初始值进行了硬编码。

In the For Each I'm printing the address of the cells but you can do your checks there. 在“每个我都在”中打印单元格的地址,但是您可以在此处进行检查。 The While loop will end when it reaches an empty cell. While循环到达空单元格时将结束。

Dim rngSet As Range, rngRow As Range

Set rngSet = Range("A1:B6")

While Not IsEmpty(rngSet.Cells(1, 1).Value)

    For Each rngRow In rngSet.Rows
        Debug.Print rngRow.Cells(1, 1).Address
    Next rngRow

    Set rngSet = rngSet.Offset(0, 6)
Wend

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM