我收到此错误消息。运行时错误'1004'对象'_Global'的方法'范围'失败

时间:2018-04-18 12:57:19

标签: excel vba excel-vba error-handling

我正在尝试仅从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

1 个答案:

答案 0 :(得分:2)

我应该排吗?

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