运行时错误'1004':应用程序定义或对象定义的错误.Cells

时间:2019-08-21 10:30:07

标签: excel vba

我只是尝试编写宏以删除某些行,其中列A中的单元格等于特定文本,但是我收到一个1004错误,该错误突出显示了行'If .Cells(rowData,1) .Value =“ IN”然后'

在此之前,我遇到了一些问题,由于VBA的错误给出了问题的解释,因此我得以纠正了一些问题,但是我对此有些犹豫-抱歉,我的经验不足

Option Explicit

Sub Test()
Dim csvWorksheet As Worksheet
Dim rowData As Long
Dim rData As Range

    'DELETE ANY 'IN' ROWS
    With Worksheets("Movers_detail")
        If .Cells(rowData, 1).Value = "IN" Then
            .Rows(rowData).EntireRow.Delete
        End If
        End With


Set rData = csvWorksheet.Cells(1, 1).CurrentRegion
With rData
    .EntireColumn.AutoFit

    'draw border
    .Borders(xlDiagonalDown).LineStyle = xlNone
    .Borders(xlDiagonalUp).LineStyle = xlNone
    With .Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With .Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With .Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With .Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With .Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With .Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
End With

'formatting the cell alignment
Columns("A:C").Select
With Selection
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With

'formatting the page scaling for printing
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .PrintTitleRows = ""
    .PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .LeftHeader = ""
    .CenterHeader = ""
    .RightHeader = ""
    .LeftFooter = ""
    .CenterFooter = ""
    .RightFooter = ""
    .LeftMargin = Application.InchesToPoints(0.7)
    .RightMargin = Application.InchesToPoints(0.7)
    .TopMargin = Application.InchesToPoints(0.75)
    .BottomMargin = Application.InchesToPoints(0.75)
    .HeaderMargin = Application.InchesToPoints(0.3)
    .FooterMargin = Application.InchesToPoints(0.3)
    .PrintHeadings = False
    .PrintGridlines = False
    .PrintComments = xlPrintNoComments
    .PrintQuality = 600
    .CenterHorizontally = True
    .CenterVertically = False
    .Orientation = xlPortrait
    .Draft = False
    .PaperSize = xlPaperA4
    .FirstPageNumber = xlAutomatic
    .Order = xlDownThenOver
    .BlackAndWhite = False
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = 20
    .PrintErrors = xlPrintErrorsDisplayed
    .OddAndEvenPagesHeaderFooter = False
    .DifferentFirstPageHeaderFooter = False
    .ScaleWithDocHeaderFooter = True
    .AlignMarginsHeaderFooter = True
    .EvenPage.LeftHeader.Text = ""
    .EvenPage.CenterHeader.Text = ""
    .EvenPage.RightHeader.Text = ""
    .EvenPage.LeftFooter.Text = ""
    .EvenPage.CenterFooter.Text = ""
    .EvenPage.RightFooter.Text = ""
    .FirstPage.LeftHeader.Text = ""
    .FirstPage.CenterHeader.Text = ""
    .FirstPage.RightHeader.Text = ""
    .FirstPage.LeftFooter.Text = ""
    .FirstPage.CenterFooter.Text = ""
    .FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True


End Sub

1 个答案:

答案 0 :(得分:2)

问题:您没有为RowData分配任何值

尝试更改此循环

'DELETE ANY 'IN' ROWS
With Worksheets("Movers_detail")
    If .Cells(rowData, 1).Value = "IN" Then
        .Rows(rowData).EntireRow.Delete
    End If
    End With

使用方式:

With Worksheets("Movers_detail")
    For rowData = 1 To .Cells(.Rows.count, "A").End(xlUp).row
        If .Cells(rowData, 1).Value = "IN" Then
            .Rows(rowData).EntireRow.Delete
        End If
    Next
End With

现在,我们遍历A列并检查IN