宏检查值列,然后将项目呈现到不同的工作表中

时间:2014-03-18 16:28:49

标签: excel vba excel-vba

我有一张看起来像的照片:

 ITEM      QTY
======     ===
"ITEM1"     2
"ITEM2"     0
"ITEM3"     1
"ITEM4"     0

我还有另一张sheet2,我希望检查此表中的项目是否有大于零的数量,如果是,请写入表2中的新表:

 ITEM      QTY
======     ===
"ITEM1"     2
"ITEM3"     1

我知道如何使用其他语言,比如ruby,但我并不是很了解VBA。希望有人可以帮助我。我想,在伪代码中,它看起来像

SheetCounter=1
SelectSheet1
  for(x=1, x < column-height, x++)
    if(item-x.qty > 0)
      copy item.x.title to sheet2, cell $A$SheetCounter
      copy item.x.qty to sheet2, cell $B$SheetCounter
      SheetCounter++
      x++
    else
      x++
  end

1 个答案:

答案 0 :(得分:1)

未经测试,但类似:

Sub Tester()

    Dim rng As Range, c As Range, cdest As Range

    With Worksheets("Sheet1")
        Set rng = .Range(.Range("A2"), .Cells(Rows.Count, 1).End(xlUp))
    End With
    Set cdest = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp)

    For Each c In rng.Cells
        If c.Offset(0, 1).Value > 0 Then
            c.Resize(1, 2).Copy cdest
            Set cdest = cdest.Offset(1, 0)
        End If
    Next c

End Sub