从选定区域删除空行,并有条件地将值替换为上方单元格中的值

时间:2019-04-26 09:09:26

标签: excel vba

我需要清除许多工作表中提取的ERP数据,以便对其进行分析。

数据格式示例:

   
Material MvTS   Mat. Doc.   Item    Pstng Date  Time

1091769             Air Mover Type AM20-SN      
3   *711        4903961381      25.10.2018  11:29:03

3 *711      4903961381      25.10.2018  11:29:03

1598718             Filter      
3   711     4901254258      12.04.2018  10:52:06

1652333             O-Ring FKM 37.82x1.78 Black     
3   711     4904214978      14.11.2018  09:53:54

1653970             Gasket assembly EV344D 20 PA        
3   711     4903943685      24.10.2018  13:18:48

我要:

  1. 使用输入框确定需要清除的数据范围
  2. 如果Material = 3替换为上方单元格中的材料编号
  3. 如果MVt ==“”(空),请删除该行

Sub CleanVendorData()

    Dim r As Range
    Dim row As Long
    Dim col As Long
    Dim adr As Range
    Dim adr1 As Range
    Dim HeadRow As Double
    Dim LowRow As Double
    Dim num As Double
    Dim i As Double
    Dim dynamic As Long

    Dim rng As Range
    Set rng = Application.InputBox("Choose a random cell in the Vendor file that you want to clean", "Clean Vendor forecast file", Type:=8)
    Workbooks(rng.Worksheet.Parent.Name).Activate

    'Deleting empty rows.
    Range("A100000").Select
    Selection.End(xlUp).Select
    Set adr1 = ActiveCell
    LowRow = adr1.row

    'finding out how bit the area of data is before its deleted:)
    Range("A1").Select
    Selection.End(xlDown).Select
    Selection.End(xlDown).Select
    Set adr = ActiveCell
    dynamic = adr.row

    Set r = ActiveSheet.Range(Cells(dynamic, 1), Cells(LowRow + dynamic, 20))
    '"A10:s50000")
    row = r.Rows.Count
    For i = row To 1 Step (-1)
        If WorksheetFunction.CountA(r.Rows(i)) = 0 Then r.Rows(i).Delete
        If WorksheetFunction.CountA(r.Rows(i)) = 1 Then r.Rows(i).Delete
    Next

    Range("A1").Select
    Selection.End(xlDown).Select
    Selection.End(xlDown).Select
    Set adr = ActiveCell
    HeadRow = adr.row
    Rows(HeadRow).Clear
    num = Cells(HeadRow + 1, 1).CurrentRegion.Rows.Count - 2

    Next

    MsgBox ("Done")

End Sub

0 个答案:

没有答案