根据第n个字符删除不需要的行

时间:2016-05-21 14:21:55

标签: excel-vba vba excel

我有一份报告(下图),我只想要唯一的产品代码,并从左边第5位删除所有没有“*”的不需要的行,但同时我想保留行18作为标题。我该如何解决这个问题?提前谢谢。

enter image description here

我有代码:

Sub Remove_Unwanted_Cells()
    Dim Firstrow As Long
    Dim lastRow As Long
    Dim Lrow As Long
    Dim Cell As Range
    Dim CalcMode As Long
    Dim ViewMode As Long
    'Dim c As Range

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    With ActiveSheet
        .Select
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
        .DisplayPageBreaks = False

        'Firstrow = .UsedRange.Cells(1).Row
        Firstrow = 2
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

        For Lrow = lastRow To Firstrow Step -1
            With .Cells(Lrow, "A")
                For Each Cell In Range("A1:A" & Cells(Rows.Count, 6).End(xlUp).Row)
                If Mid(Cell, 5, 1) <> "*" Then Cell.EntireRow.Delete
                Next Cell
            End With
         Next Lrow
    End With


    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With


End Sub

2 个答案:

答案 0 :(得分:1)

将相应的行更改为:

1 to 17

修改 关于删除行 Rows("1:17").Delete 的观点,请插入一行

 End With
Rows("1:17").Delete

ActiveWindow.View = ViewMode
With Application
    .ScreenUpdating = True
    .Calculation = CalcMode
End With

在您的代码中,如下所示。

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class MainApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {

        StackPane myPane = new StackPane(new MyRegion());
        Scene myScene = new Scene(myPane);
        stage.setScene(myScene);
        stage.setWidth(500);
        stage.setHeight(500);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

End Sub

答案 1 :(得分:0)

尝试一下:

Sub Remove_Unwanted_Cells()
    Dim Firstrow As Long
    Dim lastRow As Long
    Dim Lrow As Long
    Dim Cell As Range
    Dim CalcMode As Long
    Dim ViewMode As Long

    With Application
         CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    ActiveSheet.DisplayPageBreaks = False

    Firstrow = 2
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row

    For Lrow = lastRow To Firstrow Step -1
            If Mid(Cells(Lrow, 1), 5, 1) <> "*" Then Cells(Lrow, 1).EntireRow.Delete
     Next Lrow


    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With


End Sub