我正在使用VBA脚本在15x15阵列中找到1。该脚本查找包含1的每个单元格的行,列地址,并根据映射表将其映射到新的(行,列)地址。以下是excel文件:
这是我用来执行此映射操作的脚本:
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
数组中的任何条目
如何创建一个数组来存储映射的单元格地址并检查每个循环?