我有一张包含数千个条目的Excel表格,如下表A所示。现在,因为 一些需求变化,我想为每个'Name'添加3个额外的'TAGS',如表B所示。例如,Name n1将有标签t1,t2,t3和t4并对应每个TAG,将有一个单独的评论。 (A)实现此目的的一种方法是向每列添加三个新行(可以有3个空行)(B)另一种方法是在同一文件中有3个工作表对应每个标记。 (C)第三种方法可能是为Tag列提供一些过滤器......
有人可以建议一种自动化方法(A)吗? (宏代码将不胜感激)如果有人可以共享代码来做(C)..或任何优雅的问题解决方案会更好。谢谢!
表A
Name id Tag Comment
n1 1 t1 my t1 comment for id 1
n2 2 t1 my t1 comment for id 2
n3 3 t1 my t1 comment for id 3
n4 4 t1 my t1 comment for id 4
n5 5 t1 my t1 comment for id 5
表B
Name id Tag Comment
n1 1 t1 my t1 comment for id 1*
n1 1 t2 my t2 comment for id 1
n1 1 t3 my t3 comment for id 1
n1 1 t4 my t4 comment for id 1
n2 2 t1 my t1 comment for id 2
n3 3 t1 my t1 comment for id 3
n4 4 t1 my t1 comment for id 4
n5 5 t1 my t1 comment for id 5
答案 0 :(得分:0)
在您的桌子上测试了这段代码,它应该可以正常工作
Sub NewRows()
Dim i As Long
Dim k As Long
Dim lastRow As Long
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Sheets("test")
lastRow = Range("B" & Rows.Count).End(xlUp).Row ' I assume that your unique ID's are in column B
For i = lastRow To 2 Step -1 '3 is the first row where you have data
For k = 4 To 2 Step -1 'Amount of new lines you want to insert
ws.Rows(i + 1).Insert shift:=xlShiftDown ' insert row
ws.Range("A" & i + 1).Value = ws.Range("A" & i).Value 'copy value in column A from original entry
ws.Range("b" & i + 1).Value = ws.Range("b" & i).Value 'copy value in column A from original entry
ws.Range("C" & i + 1).Value = "T" & k 'update tag info for each new line
ws.Range("d" & i + 1).Value = "this is " & ws.Range("C" & i + 1).Value & " comment for " & ws.Range("A" & i + 1).Value
Next k
Next i
End Sub
我不确定如何使用注释填充D列,但同样的原则适用。