创建一个数组来存储单元格地址

时间:2018-02-02 11:23:47

标签: arrays excel vba

我正在使用VBA脚本在15x15阵列中找到1。该脚本查找包含1的每个单元格的行,列地址,并根据映射表将其映射到新的(行,列)地址。以下是excel文件:

enter image description here

这是我用来执行此映射操作的脚本:

Option Explicit

Sub findvalues()
Dim OldRow As Long, OldCol As Long, NewCol As Long, NewRow As Long, OldRowMapped As Long, OldColMapped As Long
Dim oldmappingrow As Variant, oldmappingcol As Variant, c As Range, firstAddress As String, cellAddress As String

With Worksheets(1).Range("a1:o15")
    Set c = .Find(1, LookIn:=xlValues)
    If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            cellAddress = c.Address
            OldRow = Range(cellAddress).Row
            OldCol = Range(cellAddress).Column

            oldmappingrow = Application.Match(OldRow, Worksheets(1).Range("r3:r16"), 0)
            If Not IsError(oldmappingrow) Then
                OldRowMapped = Worksheets(1).Range("r3:r16").Cells(oldmappingrow).Offset(, 1).Value
            End If

            oldmappingcol = Application.Match(OldCol, Worksheets(1).Range("r3:r16"), 0)
            If Not IsError(oldmappingcol) Then
                OldColMapped = Worksheets(1).Range("r3:r16").Cells(oldmappingcol).Offset(, 1).Value
            End If

            If OldCol > OldRow Then
                NewCol = WorksheetFunction.Max(OldRowMapped, OldColMapped)
                NewRow = WorksheetFunction.Min(OldRowMapped, OldColMapped)
            Else
                NewRow = WorksheetFunction.Max(OldRowMapped, OldColMapped)
                NewCol = WorksheetFunction.Min(OldRowMapped, OldColMapped)
            End If

            .Cells(NewRow, NewCol) = .Cells(OldRow, OldCol).Value
            .Cells(OldRow, OldCol).Value = "0"

            Set c = .FindNext(c)
            MsgBox (OldRow & OldCol & " moved to " & NewRow & NewCol)
        Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
End With

End Sub

我需要阻止.FindNext()映射之前已映射的值。

我认为一个好主意是将每个NewRow, New Col值存储在一个数组中,然后每个循环检查OldRow, OldCol不等于NewRow, NewCol数组中的任何条目

如何创建一个数组来存储映射的单元格地址并检查每个循环?

0 个答案:

没有答案