计算列的每个部分中的项目数并执行公式,然后循环显示下一部分

时间:2014-05-28 17:07:51

标签: excel-vba vba excel

我已经集思广益了很长一段时间,并且正在努力寻找以下代码的良好起点。我正在处理的电子表格将分为几个部分,其中不同的发票明细以" Stock No / SKU"在电子表格示例中,我已在下面输入。我想创建一个宏来查看每个出现/部分:

  1. 计算" Stock No / SKU"之间的行数或项目数。和" SUBTOTAL"
  2. 如果行数#> 1,那么我想将税额除以小计金额{税额和小计金额将显示在列A中其字段旁边的列B中}
  3. 然后,我想通过将#2中计算的销售税%与多个列中的项目总数相乘(未在下面的电子表格示例中显示)为每个项目创建新的总计(未显示)
  4. 然后我希望宏进入下一节找到" Stock No / SKU"并重复这些步骤。如果该部分只包含一个项目,那么我希望它跳过这些步骤并找到下一个" Stock No / SKU"评估等等,直到找到最后一个。
  5. 我一直在玩这个代码试图让我到某个地方,但无论如何它不是成品。如果有人对从哪里开始有任何好的想法,我将非常感激。我将继续使用它,看看我能走多远,因为我总是试图扩展我的知识和创造性技术。

    Revised Code based off of answer:
    Sub stax()    
    Dim Wk4 As Worksheet
    Set Wk4 = Sheets("Open Invoice List")
    
    Dim r As Range
    Dim start As Integer
    Dim rws As Integer
    Dim i As Integer
    Dim isItem As Boolean
    
    
    Set r = Wk4.Range("A1")
    start = r.Row
    rws = Wk4.UsedRange.Rows.Count
    
    'loop
    For i = start To rws
        Select Case r
            Case "Stock No/SKU"
                isItem = True
                r.Offset(0, 1).End(xlDown).Offset(-2, 1).Formula = r.Offset(0, 1).End(xlDown).Offset(-2, 0) / r.Offset(0, 1).End(xlDown).Offset(-3, 0)
    
            Case "SUBTOTAL:"
                isItem = False
                r.Offset(1, 2).Font.Color = vbBlue
    
    
            Case Else
            'do something to the selection in between
                If isItem Then r.Offset(0, 9).Formula = Round(r.Offset(0, 5) * r.Offset(0, 1).End(xlDown).Offset(-2, 1), 2) + r.Offset(0, 5)
    
    
    
        End Select
    
        'Move to next row
        Set r = r.Offset(1, 0)
    
    Next i
    
    
    
    'Cleanup
    Set r = Nothing
    Set Wk4 = Nothing
        End Sub
    

    电子表格示例:

    Let's say starting at Row9 in Column A
    
    Stock No/SKU
    item1
    SUBTOTAL:
    TAX
    
    {several rows in between}
    
    Stock No/SKU
    item1
    item2
    item3
    item4
    SUBTOTAL:
    TAX
    
    {several rows in between}
    
    Stock No/SKU
    item1
    item2
    SUBTOTAL:
    TAX
    

2 个答案:

答案 0 :(得分:1)

Do-While循环仅运行到Break条件,在您的情况下找到了。你不能在这里使用它,因为你必须在这种情况下反复重启它。因此,这是一种错误的循环(或条件)。

您必须一直循环到列表的底部。您可以使用UsedRange获取工作表中使用的行数,并将其用作终止简单From To的点。

试试这个(未经测试):

Dim sh as Worksheet
Dim r as Range
Dim start as Integer, rws as Integer, i as Integer
Dim isItem as Boolean

'Presets
Set sh = ThisWorkbook.Sheets("Open Invoice List")
Set r = sh.Range("A9")    
start = r.Row
rws = sh.UsedRange.Rows.Count

'Loop
For i = start to rws

    Select Case r

        Case "Stock No/SKU"
            isItem = true

        Case "SUBTOTAL:" 
            isItem = false
            r.Offset(0,1).Font.Color = vbRed

        Case Else
            If isItem then r.Offset(0,1).Font.Color = vbBlue

    End Select

    'Move to next row
    Set r = r.Offset(1,0)

Next i

'Cleanup
Set r = Nothing
Set sh = Nothing

答案 1 :(得分:0)

我明白了。这是用于选择所有项目并将其粘贴到新工作表上的代码。让我知道你的想法。

Sub populatetemplate()

Dim Wk4 As Worksheet
Set Wk4 = Sheets("Open Invoice List")

Dim Wk1 As Worksheet
Set Wk1 = Sheets("Form")


Dim r As Range
Dim start As Integer
Dim rws As Integer
Dim i As Integer
Dim isItem As Boolean


Set r = Wk4.Range("A1")
start = r.Row
rws = Wk4.UsedRange.Rows.Count


Dim p As Integer
p = 17

'loop
For i = start To rws
    Select Case r
        Case "Stock No/SKU"
            isItem = True

        Case "SUBTOTAL:"
            isItem = False

        Case Else
        'do something to the selection in between
        If isItem Then
            With r.Offset(0, 1)
            Wk1.Cells(p, 9).Resize(.Rows.Count, .Columns.Count).Value = .Value
            p = p + 1
            End With
        End If


    End Select

    'Move to next row
    Set r = r.Offset(1, 0)

Next i



'Cleanup
Set r = Nothing
Set Wk4 = Nothing


End Sub