如何基于单元格中的数字"n"插入行数"n" [英] How to insert 'n' number of rows based on 'n' being a number in a cell

查看:70
本文介绍了如何基于单元格中的数字"n"插入行数"n"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在a列中列出了公司列表.在b列中,我有一个数字.我需要创建一个列表,其中该公司在该列表中的出现次数与该数字的值一样多:

I have a list of companies in column a. In column b, I have a number. I need to create a list where the company appears as many times in the list as the value of the number:

Company A    4
Company B    2
Company C    3  

我想要:

Company A
Company A
Company A
Company A
Company B
Company B
Company C
Company C
Company C  

因此,它有点像创建(n)个重复项"功能.

So, it's sort of a 'Create (n) Duplicates' function.

推荐答案

尝试一下:

Sub KopyKat()
   Dim N As Long, i As Long, K As Long
   Dim v As String, kk As Long, m As Long
   N = Cells(Rows.Count, "B").End(xlUp).Row
   K = 1

   For i = 1 To N
      kk = Cells(i, "B").Value
      v = Cells(i, "A").Value
      For m = 1 To kk
         Cells(K, "C") = v
         K = K + 1
      Next m
   Next i
End Sub

EDIT#1:

宏非常易于安装和使用:

Macros are very easy to install and use:

  1. ALT-F11弹出VBE窗口
  2. ALT-IALT-M打开一个新模块
  3. 将内容粘贴并关闭VBE窗口

如果保存工作簿,则宏将随其一起保存.如果要在2003年以后使用Excel版本,则必须保存该文件为.xlsm而不是.xlsx

If you save the workbook, the macro will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

要删除宏,请执行以下操作:

To remove the macro:

  1. 如上调出VBE窗口
  2. 清除代码
  3. 关闭VBE窗口

要使用Excel中的宏,请执行以下操作:

To use the macro from Excel:

  1. ALT-F8
  2. 选择宏
  3. 触摸运行"

要全面了解有关宏的更多信息,请参见:

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

必须启用宏才能使其正常工作!

这篇关于如何基于单元格中的数字"n"插入行数"n"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆