我有一张表(比如Sheet2),里面有一张表,其中包含数据。
在下面的示例中(远小于实际数据),需要从B2:O2中选择每个值,如果有值(忽略随机放置的没有数据的所有空白单元格),则增加数量为前一个细胞的15%。
范围来自B:O,但不是每个单元格都有值。 A列的ID值可以计算每列的长度。
因此,单元格B2输出为11.5,C2为1.15,如下所示:
在代码运行之前。
Row Column A Column B Column C Column D ... Column O
1 ID# Header1 Header2 Header3 'Row(1) header
2 ID1 10 1
3 ID2 2 5
4 ID3 2
代码运行后。
Row Column A Column B Column C Column D ... Column O
1 ID# Header1 Header2 Header3 'Row(1) header
2 ID1 11.5 1.15
3 ID2 2.3 5.75
4 ID3 2.3
我不知道如何使用For Each Loop?提前谢谢!
答案 0 :(得分:0)
如果您需要将增量更改Inc_By更改为您需要的小数。例如0.15是15%0.45是45% 以下假定" - "真的是""
Sub test()
Inc_By = 0.15
With ActiveSheet
lCol = .Range("A1").End(xlToRight).Column
For Each cell In .Range("A2:" & .Range("A2").End(xlDown).Address)
For i = 2 To lCol
If .Cells(cell.Row, i) <> "" Then
.Cells(cell.Row, i).Value = .Cells(cell.Row, i).Value + (.Cells(cell.Row, i).Value * Inc_By)
End If
Next
Next
End With
End Sub
如果&#34; - &#34;真是一个&#34; - &#34;在您的电子表格中替换
If cell.Offset(0, 2).Value = "" Then
与
If cell.Offset(0, 2).Value = "-" Then
答案 1 :(得分:0)
如果其他人需要相同的答案,以下内容适用于我:
Sub Test()
On Error GoTo ErrorMsg
Dim l As Integer, f As Integer, c As Integer, cf As Integer, cl As Integer
l = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row 'Total Rows, count from ID Column
f = 1 'First Row with Headings, is skipped with Offset
c = f 'Counter for Loop
cf = 2 'First Column to MarkUp
cl = 4 'Last Column to be MarkUp
Inc_By = 1.15 'Markup Amount
With ActiveSheet
Do While cf <= cl 'Loop count to check every Column
Cells(f, cf).Activate 'Select first cell of Column
For Each cell In .Range(Cells(f, cf).Address, Cells(l, cf).Address) 'cf needs to be a variable and cycle through to cl
If ActiveCell.Offset(1, 0).Value > 0 And Not IsEmpty(ActiveCell.Offset(1, 0).Value) Then 'Skips negatives and blanks
ActiveCell.Offset(1, 0).Activate
ActiveCell.Value = ActiveCell.Value * Inc_By
Else
ActiveCell.Offset(1, 0).Activate
End If
Next
cf = cf + 1 'Counter to start searching next Column
Loop
End With
MsgBox ("Done")
End Sub