【发布时间】:2013-10-31 21:07:46
【问题描述】:
我正在寻求在 VBA 中创建新行的帮助。 A:C 列是一般项目,D:F 列是 A:C 列中的 VBA 公式驱动值。 (基本上是 If Then 语句)
为了进行分析,我们的系统要求满足每个条件的单个订单项。第 1 行符合两个标准; “查询”和“高”。所以我需要在下面插入一个新行,从第 1 行 A:C 复制数据,然后在 D 列中输入“High”。这样,“Inq”和“High”就有一行数据。
将针对每一行重复该过程,不包括新添加的行。抱歉,这可能有点棘手,但我会尽我所能提供帮助。我是 Stackoverflow 的新手,所以我无法发布我的桌子的图片。
----下面是更新----
下面的代码非常适合第 19 列。它插入了行,在新行中插入了值,并将“Lead”放在最后一列。
Sub AddRow()
Dim RowIndex As Long
Dim Delta As Long
RowIndex = 2
Do While Sheets("WeeklyReport").Cells(RowIndex, 1).Value <> ""
Delta = 0
If Sheets("WeeklyReport").Cells(RowIndex, 19).Value = "Lead" Then
' Inserts new row
Sheets("WeeklyReport").Cells(RowIndex + Delta + 1, 1).EntireRow.Insert
' Takes cells value from row above and enters value in new row
Sheets("WeeklyReport").Range(Cells(RowIndex + 1, 1), Cells(RowIndex + 1, 17)).Value = Sheets("WeeklyReport").Range(Cells(RowIndex, 1), Cells(RowIndex, 17)).Value
' Puts rating value in last column
Sheets("WeeklyReport").Range(Cells(RowIndex + 1, 18), Cells(RowIndex + 1, 18)).Value = "Lead"
Delta = Delta + 1
End If
RowIndex = RowIndex + Delta + 1
Loop
End Sub
由于我在 RowIndex 中有多个潜在值,我假设我可以复制第一个 If 语句,为下一列修改它,一切都会正常工作(参见下面的代码)。当我运行它时,它插入了两行,只有一行向下复制,另一行空白。
问题似乎在于每个 RowIndex 是否有多个值。我将有可能为每个 RowIndex 生成多个值,我想在其中为每个值创建一个单独的行。请参见代码下方的示例。
这是我一直在使用的代码 子 AddRow()
Dim RowIndex As Long
Dim Delta As Long
RowIndex = 2
Do While Sheets("WeeklyReport").Cells(RowIndex, 1).Value <> ""
Delta = 0
If Sheets("WeeklyReport").Cells(RowIndex, 19).Value = "Lead" Then
' Inserts new row
Sheets("WeeklyReport").Cells(RowIndex + Delta + 1, 1).EntireRow.Insert
' Takes cells value from row above and enters value in new row
Sheets("WeeklyReport").Range(Cells(RowIndex + 1, 1), Cells(RowIndex + 1, 17)).Value = Sheets("WeeklyReport").Range(Cells(RowIndex, 1), Cells(RowIndex, 17)).Value
' Puts rating value in last column
Sheets("WeeklyReport").Range(Cells(RowIndex + 1, 18), Cells(RowIndex + 1, 18)).Value = "Lead"
Delta = Delta + 1
End If
If Sheets("WeeklyReport").Cells(RowIndex, 20).Value = "HP" Then
' Inserts new row
Sheets("WeeklyReport").Cells(RowIndex + Delta + 1, 1).EntireRow.Insert
' Takes cells value from row above and enters value in new row
Sheets("WeeklyReport").Range(Cells(RowIndex + 1, 1), Cells(RowIndex + 1, 17)).Value = Sheets("WeeklyReport").Range(Cells(RowIndex, 1), Cells(RowIndex, 17)).Value
' Puts rating value in last column
Sheets("WeeklyReport").Range(Cells(RowIndex + 1, 18), Cells(RowIndex + 1, 18)).Value = "HP"
Delta = Delta + 1
End If
RowIndex = RowIndex + Delta + 1
Loop
End Sub
示例值 - 下面不是代码,也没有在宏中使用,仅用于示例
Example: (RowIndex) A1-A17 Column 19 = "Lead", Column 20 = "HP", Column 21 = "QL"
Output: (RowIndex) A1-A17 Column 18 = "Lead"
(RowIndex) A1-A17 Column 18 = "HP"
(RowIndex) A1-A17 Column 18 = "QL"
【问题讨论】:
-
您是否尝试过录制宏并编辑生成的代码?通常,要求代码至少不显示某些现有代码的问题会在这里很快关闭。
-
The problem looks to be if the RowIndex has multiple values; for instance "Lead" in Column 19, and "HP" in Column 20 of the same row.在这种情况下,您希望添加多少行? 1 还是 2? -
@Sam Ward 你是正确的,“Lead”和“HP”将在同一行。在这种情况下,我想创建两个 RowIndex 副本(第 1-17 列),一个用于“Lead”,一个用于“HP”。另一方面,每个 RowIndex 可能有 6-8 个值,我想为每个值创建一个新行。我上面的代码显然只针对两个值,但为了清楚起见,我对其进行了修剪。
-
@bjk 我给你贴了一些代码,你应该能够适应。