查找值从正变为负的行并插入空行

时间:2017-01-18 16:55:29

标签: excel excel-vba vba

我有下面的代码,它取一个数据表,删除大部分列和C列中的值为零的任何行,最后按列C排序。

我接下来需要做的是找到C列中的值从正变为负的行,并插入5个空白行。

我可以找到其他代码示例,它们遍历这些值以执行许多任务,但我只需要插入行,其中值从正变为负。

Sub FormatData()

Dim wsData As Worksheet
Dim FirstRow As Integer
Dim LastRow As Integer
Dim LastRow2 As Integer
Dim lrow As Integer

Set wsData = Worksheets("Data")

FirstRow = wsData.Range("C2").Row
LastRow = wsData.Range("A" & Rows.Count).End(xlUp).Row

Columns("C:Y").Select
Selection.Delete Shift:=xlToLeft
Columns("D:F").Select
Selection.Delete Shift:=xlToLeft

wsData.Range("C2:C" & LastRow).Select

For lrow = LastRow To FirstRow Step -1
Set workrange = Cells(lrow, 3)
If workrange.Value = "0" _
    Then workrange.EntireRow.Delete

Next lrow

LastRow2 = wsData.Range("C" & Rows.Count).End(xlUp).Row

Range("A2:C" & LastRow2).Sort Key1:=Range("C2:C" & LastRow2), Order1:=xlDescending, Header:=xlNo

End Sub

1 个答案:

答案 0 :(得分:2)

您可以在排序代码之后将以下循环添加到sub的末尾:

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
ndp.unmarshalDefaultNumbering();
AlternativeFormatInputPart inputPart = new AlternativeFormatInputPart(AltChunkType.Xhtml);
inputPart.setContentType(new ContentType("text/html"));
inputPart.setBinaryData(html.getBytes());
Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(inputPart);
// .. the bit in document body
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId());
wordMLPackage.getMainDocumentPart().addObject(ac);
// .. content type
wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html");
//Saving the Document
wordMLPackage.save(file);