VBA宏删除一行

时间:2016-02-05 16:11:36

标签: excel vba

嘿,我刚刚创建了一个宏添加标题删除信息并获得了数据格式 但我注意到,当我为另一个文件运行它 它只是删除了我现在需要的那个确切单元格中的数据 照着做 但删除短语所在的行

想象我在文档的其他版本中有一个单元格a1,该短语可能位于a2中 我的宏只会删除A1中的最新内容

短语是ZFD 我需要宏来删除短语所在的整行 HELPPPPP

Sub UMR()
'
' UMR Macro
'

'
 Range("A1").Select
ActiveCell.FormulaR1C1 = "Transaction_Type"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Meter_Point_Ref"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Actual_Read_Date"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading_Source"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading_Reason"
Range("F1").Select
ActiveCell.FormulaR1C1 = "Meter_Serial_Number"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Meter_ROC_Count"
Range("I1").Select
ActiveCell.FormulaR1C1 = "Meter_Read_Verified"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Corrector_serialNumber"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Corrector_serial_Number"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Corrector_Uncorrected_Reading"
Range("L1").Select
ActiveCell.FormulaR1C1 = "Corrector_Corrected_Reading"
Range("M1").Select
ActiveCell.FormulaR1C1 = "Corrector_ROC_Count"
Range("N1").Select
ActiveCell.FormulaR1C1 = "Corrector_Usable_IND"
Range("O1").Select
ActiveCell.FormulaR1C1 = "Corrector_Read_Verified"
Range("A17").Select
Selection.ClearContents
Range("B17").Select
Selection.ClearContents
Columns("C:C").ColumnWidth = 8.29
Columns("C:C").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Columns("E:E").Select
Columns("D:D").EntireColumn.AutoFit
Columns("E:E").EntireColumn.AutoFit



Columns("F:F").EntireColumn.AutoFit
Columns("G:G").EntireColumn.AutoFit
Columns("H:H").EntireColumn.AutoFit
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
Columns("I:I").EntireColumn.AutoFit
Columns("J:J").EntireColumn.AutoFit
Range("Q1").Select
Columns("K:K").EntireColumn.AutoFit
Columns("L:L").EntireColumn.AutoFit
Range("R1").Select
Columns("M:M").EntireColumn.AutoFit
Columns("N:N").EntireColumn.AutoFit
Columns("O:O").EntireColumn.AutoFit
ActiveWindow.SmallScroll Down:=6
ActiveWindow.SmallScroll ToRight:=-9
ActiveWindow.SmallScroll Down:=-88
End Sub

1 个答案:

答案 0 :(得分:0)

正如我刚才那样,我有时间重新组织你的代码。请注意,这在stackoverflow上并不常见。下次:至少尝试编写一些代码,如果这不是问题,那就是我们可以提供帮助的地方。并且为了您的信息:我也是新手(到目前为止已经有3.5个月的vba),所以并不难。即使我的代码还没有完善,大多数时候我都能以某种方式让它工作......

尝试一次(首先阅读代码中的注释):

Sub UMR()

    Dim WS As worksheet
    Set WS = AcitveWorkbook.ActiveWorksheet 'be aware this will always be run on the activesheet

    Dim Values AS Variant
    Values = Array("Transaction_Type", "Meter_Point_Ref", "Actual_Read_Date", "Meter_Reading_Source", "Meter_Reading_Reason", "Meter_Serial_Number", "Meter_Reading", "Meter_ROC_Count", "Meter_Read_Verified", "Corrector_serialNumber", "Corrector_serial_Number", "Corrector_Uncorrected_Reading", "Corrector_Corrected_Reading", "Corrector_ROC_Count", "Corrector_Usable_IND", "Corrector_Read_Verified")
    Dim FindString As String
    FindString = "ZFD"

    Dim ZFDVal As Variant
    Dim IRow As Integer
    Dim ICol As Integer

    Set ZFDVal = Ws.Find(What:=FindString, _
                       After:=Ws.Cells(Ws.Cells.Count), _
                       LookIn:=xlValues, _
                       LookAt:=xlWhole, _   'If the value is only a part of a cell it would be xlPart instead of xlWhole
                       SearchOrder:=xlByRows, _
                       SearchDirection:=xlNext, _
                       MatchCase:=False) 'If you want it to Match the string exactly (regarding capital letters) you'll have to set this to true

    IRow = Range(ZFDVal.Adress).Row 'This is untested...

    For ICol = 1 To (UBound(Values)-LBound(Values))
        Ws.Cells(IRow, ICol) = Values(ICol-1)
    Next ICol

    Range("A17").Clear ' I believe this was unintendet and just recorded alongside so you can delete these two rows...
    Range("B17").Clear

    Columns("A:O").EntireColumn.AutoFit

End Sub

如果出现运行时错误,请按“debug”并注释哪一行标记为黄色。这样我们就可以帮助您纠正代码......