Excel如果单元格不为空,则将行复制到sheet2中

时间:2016-11-12 17:58:09

标签: excel vba excel-vba if-statement macros

我试图弄清楚如何在我的工作簿中查看单元格,如果单元格的值大于0,则将该行复制到sheet2中。然后它会查看列中的下一个单元格。

有谁知道??

我需要它查看单元格I10开始,如果I10> 0将数据从A10:K10复制到sheet2,则查看I11并重复,然后I12 ......直到所有750多行都被复制或不复制。 / p>

非常感谢所有的帮助!!!

2 个答案:

答案 0 :(得分:0)

Option Explicit

Sub Macro1()

Dim cell As Range
Dim lastRow As Long, i As Long

lastRow = Range("I" & Rows.Count).End(xlUp).Row
i = 10 ' change this to the wanted starting row in sheet2

For Each cell In Sheets(1).Range("I10:I" & lastRow)
    If cell.Value > 0 Then
        cell.EntireRow.Copy Sheets(2).Cells(i, 1)
        i = i + 1
    End If
Next

End Sub

答案 1 :(得分:0)

您可以使用Range Sub Main() With Worksheets("OriginWs") '<--| change "OriginWs" to your actual data worksheet name With .Range("I9", .Cells(.Rows.Count, "I").End(xlUp)) If IsEmpty(.Cells(1, 1)) Then .Cells(1, 1) = "dummyheader" .AutoFilter Field:=1, Criteria1:=">0" If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) > 1 Then .Offset(1, -8).Resize(.Rows.Count - 1, 11).SpecialCells(xlCellTypeVisible).Copy Worksheets("TargetWs").Cells(1, 1) '<--| change "TargetWs" to your actual destination worksheet name If .Cells(1, 1).Value = "dummyheader" Then .Cells(1, 1).ClearContents End With .AutoFilterMode = False End With End Sub 对象的方法,避免循环遍历单元格:

If IsEmpty(.Cells(1, 1)) Then .Cells(1, 1) = "dummyheader"
BTW,陈述:

If .Cells(1, 1).Value = "dummyheader" Then .Cells(1, 1).ClearContents

Stored procedure
如果您的单元格I9确实有一些文本

,则可以避免