繁体   English   中英

如何增加行的单元格值,但在列中重复它们?

[英]How to increment cell values for rows, but to repeat them in columns?

我有一段代码效果很好,但我想按列递增这些值。 假设我在 AE 列中有 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。

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

Indeks = 0
For Each cell In RangeToAnonimize

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM