【问题标题】:How to increment cell values for rows, but to repeat them in columns?如何增加行的单元格值,但在列中重复它们?
【发布时间】:2021-06-20 01:10:29
【问题描述】:

我有一段运行良好的代码,但我想按列递增这些值。假设我在 A-E 列中有 60 个单元格,所以我在 A、B...E 列中有 ID_1ID_60。我的代码工作方式B1ID_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。

【问题讨论】:

  • 谢谢,现在清楚多了。还有一个问题:你想要的输出是什么?
  • 正如我提到的,我每列有 57 行。我想匿名化我的数据,所以第一行得到 ID_1 等到 ID_57,除了 InStr = 0 函数覆盖的数组中的变量。我的问题是 B1 单元格是 ID_58,而不是 ID_1。因此,Indeks 的计数器在进入下一列时不会重置,依此类推
  • 我很清楚,您希望 A2:E2 为“ID_0”,A3:E3 为“ID_1”,等等。对吗?

标签: excel vba increment


【解决方案1】:

您的Indeks = 0 应该在For Each cell 之前

Indeks = 0
For Each cell In RangeToAnonimize

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 2014-06-02
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-08
    相关资源
    最近更新 更多