【问题标题】:I am getting this error message. Run Time error '1004' Method 'Range' of object'_Global' Failed我收到此错误消息。运行时错误“1004”对象“_Global”的方法“范围”失败
【发布时间】:2018-04-18 12:57:18
【问题描述】:

我试图仅从 H 列复制文本值并将它们移动到 E。我想自动化它,以便每次文本值从 sheet1 到达 H 时,它直接转到 E 而不是 H。将 H 留空那个细胞。

    Sheets("102Tk").Select
    Dim row As Long
    For row = 17 To 1000
        ' Check if "textvalue" appears in the value anywhere.
        If WorksheetFunction.IsText(Range("H" & i)) Then
            ' Copy the value and then blank the source.
            Range("E" & i).value = Range("H" & i).value
            Range("H" & i).value = ""
        End If
    Next
End Sub

【问题讨论】:

  • 你没有声明i

标签: vba excel error-handling


【解决方案1】:

我应该排吗?

Option Explicit
Sub n()

    Sheets("102Tk").Select 
    Dim row As Long

    For row = 17 To 1000
        ' Check if "save" appears in the value anywhere.
        If Not IsNumeric(Range("H" & row)) Then
            ' Copy the value and then blank the source.
            Range("E" & row).Value = Range("H" & row).Value
            Range("H" & row).Value = ""
        End If
    Next
End Sub

避免使用 row 作为变量名和其他一些整洁的方法可能是:

Option Explicit
Public Sub SetValues()
    Dim currentRow As Long
    With ThisWorkbook.Worksheets("102Tk")  'taking note of the comments and using worksheet collection to avoid Chart sheets
        For currentRow = 17 To 1000
            ' Check if "save" appears in the value anywhere.
            If Not IsNumeric(.Range("H" & currentRow)) Then
                ' Copy the value and then blank the source.
                .Range("E" & currentRow) = .Range("H" & currentRow)
                .Range("H" & currentRow) = vbNullString
            End If
        Next currentRow
    End With
End Sub

【讨论】:

  • 您的代码有效,但它复制了所有值。我只需要传输文本值而不是数字。你知道我可以改变吗?
  • 这在“If Not IsNumeric(.Range("H" & i)) Then”这一行给出了不同的错误消息,错误消息是“应用程序定义的或对象定义的错误”
  • 错误在哪一行?如果第一次有效,第二次也应该有效,因为我只更改了测试。
  • @ManishShiwlani 你是复制了整个代码,还是只复制了一个部分 - 如果你仍然有 Dim row as Long 而不是 Dim currentRow as Long,你会得到那个错误
  • @QHarr 可能行错误的一种方式是如果ThisWorkbook.Sheets("102Tk") 是图表而不是工作表 - 但那将是Run-time error '438': Object doesn't support this proerty or method
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多