简体   繁体   English

根据另一列的行数对列进行编号

[英]Number down a column based on amount of rows from another column

I am trying to number column A in increments by 1, based on how many rows are in column B Example of my Excel sheet我正在尝试根据 B 列中有多少行,以 1 为增量对 A 列进行编号我的 Excel 工作表示例

The code I currently have does this, but the top number does not end up being 1. I need to start with 1 at the top and count down.我目前拥有的代码可以做到这一点,但顶部的数字最终不会是 1。我需要从顶部的 1 开始并倒计时。

Sub SecondsNumbering()

     Dim ws As Worksheet
     Set ws = ThisWorkbook.Sheets("Data Formatted")
     Dim LastRow As Long
     Dim i As Long

     With ws
         LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

         For i = 6 To LastRow
             .Cells(i, 1).Value = i - 1
         Next
     End With

End Sub

With this, I am counting the number of rows in the column.有了这个,我正在计算列中的行数。

Edit: When I do the value 7 for i, so that it starts at 6 (which is where I want data to start) this is what I get.编辑:当我为 i 执行值 7 时,它从 6 开始(这是我希望数据开始的地方),就是我得到的。

How about...怎么样...

Option Explicit

Sub Test()

   Dim lCntr As Long

   lCntr = 6

   Do
      If (Cells(lCntr, 2) <> "") Then Cells(lCntr, 1) = lCntr - 5
      lCntr = lCntr + 1
   Loop Until Cells(lCntr, 2) = ""

End Sub

在此处输入图像描述

HTH HTH

暂无
暂无

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

相关问题 当范围基于另一列中的文本时,一列中的行数未知的小计公式 - Subtotal formula with unknown amount of rows in a column when the range is based on a text in another column 根据特定列上的单元格值将多行添加到另一个工作表 - Adding a number of rows to another worksheet based on cell value on a certain column 根据另一个表中的列中的值将特定数量的行插入到表中 - Insert a specific number of rows into a table based on values in a column in another table 根据 A 列向 J 列添加确切数量的数字 - Add an exact amount of number to the column J based on column A 在指定行下插入和下移“ X”列行数量 - Insert and Shift Down “X” Amount of Column Rows Under Specified Row Excel-根据另一分组列计算一列中的平均值。 每组的行数不是恒定的 - Excel - calculate average of values in one column based on another grouping column. The number of rows is not constant per group 根据唯一的“ID”列将行从一个工作表复制到另一个工作表 - Copy rows from one worksheet to another based on a unique "ID" column 根据另一列的选择,从一列的下拉列表中完成一项 - Completing an entry from a drop down list in one column, based on the choice from another column 如何根据 Excel 中另一个动态列的长度将值向下溢出 x 行? - How to spill a value down x rows based on length of another dynamic column in Excel? 根据单元格原始列的行数将内容从特定单元格复制到空列 - Copy contents from a specific cell to an empty column based on number of rows from cells original column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM