【问题标题】:Last Row code to the Select case code Ranges选择案例代码范围的最后一行代码
【发布时间】:2021-01-29 14:26:53
【问题描述】:

我已添加最后一行以选择案例代码,请参见下文。 错误说

Method Range of Object _Worksheet Failed

不太清楚如何处理错误? 是不是可以t find the range on the worksheet? Ive把颜色程序放在下面给你看 代码工作正常,除了范围内的最后一行

Option Explicit

Private Sub Add_Break_Lines_Click()

    Dim Com As ComboBox
    Dim ws As Worksheet

    Set ws = Application.Workbooks("Automated ardworker.xlsm").Worksheets("Job Card Master")
    Set Com = Me.Add_Break_Lines
    LastRow = ws.Range("C299").End(xlUp).row
    
    With ws
        Select Case Com.Value
        Case "Break Lines 1 Page Job Card"
            Color .Range("A13:Q & LastRow")
        End Select
    End With

End Sub

Function Color(rng As Range)

    Dim row As Range
    Dim EmptyRowNum As Integer
    
    For i = 1 To rng.Rows.Count
        Set row = rng.Rows(i)
        If WorksheetFunction.CountA(row) = 0 Then
            EmptyRowNum = EmptyRowNum + 1
        End If
        If EmptyRowNum = 2 Then
            EmptyRowNum = 0
            row.Interior.ColorIndex = 6
        End If
    Next i

End Function

【问题讨论】:

  • Color .Range("A13: & LastRow") 没有多大意义。你想做什么?范围必须类似于"A13:A" & LastRow
  • 你能发布Color过程吗?
  • 还要检查这个Automated ardworker.xlsm,它可能拼写错误。看起来可能是CardWorker
  • 很抱歉现在说 Cardworker 但仍然无法正常工作
  • 我已经在上面的代码中添加了颜色程序

标签: excel vba named-ranges


【解决方案1】:

组合框更改

  • 始终使用Option Explicit

  • 使用与VBA 中使用的关键字、属性、方法的名称不同的变量名称。

  • 你可以考虑使用

    If WorksheetFunction.CountBlank(rrg) = rrg.Columns.Count Then 
    

    由于 Tom Brunberg 在 cmets 中指出的原因,在函数中,即如果您的公式在范围内评估为 "",则永远不会有 empty 行,只有 blank 行。也许在循环之前使用一个变量:

    cCount = rng.Columns.Count
    

表格模块,例如Sheet1(在 VBA 中,括号中的名称)

Option Explicit

Private Sub Add_Break_Lines_Change()

    Dim cmb As ComboBox
    Dim ws As Worksheet
    Dim LastRow As Long

    Set ws = ThisWorkbook.Worksheets("Job Card Master")
    Set cmb = Me.Add_Break_Lines
    cmb.AddItem "Break Lines 1 Page Job Card"
    cmb.AddItem "Dummy"
    LastRow = ws.Range("C" & ws.Rows.Count).End(xlUp).row
    
    Select Case cmb.Value
        Case "Break Lines 1 Page Job Card"
            colorAbove ws.Range("A13:Q" & LastRow)
    End Select

End Sub

标准模块,例如Module1(可选在同一工作表模块中)

Option Explicit

Sub colorAbove(rng As Range)
    
    Dim brg As Range
    Dim rrg As Range
    Dim EmptyRowNum As Long
    Dim i As Long
    
    For i = 1 To rng.Rows.Count
        Set rrg = rng.Rows(i)
        If WorksheetFunction.CountA(rrg) = 0 Then
            EmptyRowNum = EmptyRowNum + 1
        End If
        If EmptyRowNum = 2 Then
            EmptyRowNum = 0
            If brg Is Nothing Then
                Set brg = rrg
            Else
                Set brg = Union(brg, rrg)
            End If
        End If
    Next i
    
    If Not brg Is Nothing Then
        brg.Interior.ColorIndex = 6
    End If

End Sub

【讨论】:

  • Ive done what you said but its 仍然说错误“工作表的方法范围失败”在这部分代码中,ColorAbove ws.Range("A13:Q & LastRow")
  • 报错时LastRow的值是多少?只需将鼠标悬停在它上面。
  • 说 LastRow= 0
  • 你有它。模块顶部是否有Option Explicit(这很重要)?
  • LastRow 现在正在工作,我还添加了 Option Explicit。但它仍然在同一行失败。
猜你喜欢
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 2016-10-10
  • 1970-01-01
  • 2015-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多