【问题标题】:Run-time error '1004': Application-defined or object-defined error .Cells运行时错误“1004”:应用程序定义或对象定义的错误 .Cells
【发布时间】:2019-08-21 10:30:07
【问题描述】:

我只是想编写一个宏来删除 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

【问题讨论】:

  • 您没有为RowData 分配任何值。它有什么价值?

标签: excel vba


【解决方案1】:

问题:您没有为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

【讨论】:

    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2016-01-14
    • 1970-01-01
    相关资源
    最近更新 更多