"Mark Up" 所有 non-blank 单元格(不包括 header 行)

"Mark Up" all non-blank cells (excluding header row)

我有一个 sheet(比如 Sheet2),里面有一个 table,里面有数据。

在下面的示例中(比实际数据小得多),它需要 select 从 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 循环?提前致谢!

如果您需要将增量更改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

如果电子表格中的“-”确实是“-”,请在下面替换

If cell.Offset(0, 2).Value = "" Then

 If cell.Offset(0, 2).Value = "-" Then

如果其他人需要相同的答案,以下内容对我有用:

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