首页 > 解决方案 > 如何增加行的单元格值,但在列中重复它们?

问题描述

我有一段代码效果很好,但我想按列递增这些值。假设我在 AE 列中有 60 个单元格,所以我必须ID_1ID_60col A、B...E 中。我的代码的工作方式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。

标签: excelvbaincrement

解决方案


Indeks = 0应该在之前For Each cell

Indeks = 0
For Each cell In RangeToAnonimize

推荐阅读