如何增加行的单元格值,但在列中重复它们?
How to increment cell values for rows, but to repeat them in columns?
我有一段代码运行良好,但我想按列递增这些值。假设我在 A-E 列中有 60 个单元格,所以我在 A、B...E 列中有 ID_1
到 ID_60
。我的代码的工作方式 B1
是 ID_61
有什么解决方法的建议吗?
编辑:更改变量名称的代码:
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Dim HeaderName As String
HeaderName = Join(Array("Country", "City", "X", "Y"))
Dim LastUsedCell As Range
Set LastUsedCell = Nothing
Set LastUsedCell = ws.UsedRange.Find("*", , , , xlByRows, xlPrevious)
If Not LastUsedCell Is Nothing Then
Dim HeaderRow As Range
Set HeaderRow = ws.Range(ws.Cells(1, 1), ws.Cells(1, ws.Cells.Columns.Count).End(xlToLeft))
Indeks = 0
For Each Header In HeaderRow
If InStr(1, HeaderName, Header.Value, vbTextCompare) = 0 Then
Dim RangeToAnonimize As Range
Set RangeToAnonimize = ws.Range(ws.Cells(2, Header.Column), ws.Cells(LastUsedCell.Row, Header.Column))
For Each cell In RangeToAnonimize
cell.Value2 = "ID_" & Indeks
Indeks = Indeks + 1
Next cell
End If
Next Header
End If
Next ws
End Sub
代码的作用:
然而,目的是每一行应该有相同的 ID。
您的 Indeks = 0
应该在 For Each cell
之前
Indeks = 0
For Each cell In RangeToAnonimize
我有一段代码运行良好,但我想按列递增这些值。假设我在 A-E 列中有 60 个单元格,所以我在 A、B...E 列中有 ID_1
到 ID_60
。我的代码的工作方式 B1
是 ID_61
有什么解决方法的建议吗?
编辑:更改变量名称的代码:
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Dim HeaderName As String
HeaderName = Join(Array("Country", "City", "X", "Y"))
Dim LastUsedCell As Range
Set LastUsedCell = Nothing
Set LastUsedCell = ws.UsedRange.Find("*", , , , xlByRows, xlPrevious)
If Not LastUsedCell Is Nothing Then
Dim HeaderRow As Range
Set HeaderRow = ws.Range(ws.Cells(1, 1), ws.Cells(1, ws.Cells.Columns.Count).End(xlToLeft))
Indeks = 0
For Each Header In HeaderRow
If InStr(1, HeaderName, Header.Value, vbTextCompare) = 0 Then
Dim RangeToAnonimize As Range
Set RangeToAnonimize = ws.Range(ws.Cells(2, Header.Column), ws.Cells(LastUsedCell.Row, Header.Column))
For Each cell In RangeToAnonimize
cell.Value2 = "ID_" & Indeks
Indeks = Indeks + 1
Next cell
End If
Next Header
End If
Next ws
End Sub
代码的作用:
然而,目的是每一行应该有相同的 ID。
您的 Indeks = 0
应该在 For Each cell
Indeks = 0
For Each cell In RangeToAnonimize