简体   繁体   中英

I am getting this error message. Run Time error '1004' Method 'Range' of object'_Global' Failed

I am trying to copy text values only from column H and move them to E. I want to automate it so that everytime a text value comes to H from sheet1, it directly goes to E instead of H. Leaving H empty in that cell.

    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

Should i be row?

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

Which avoiding using row as a variable name and a few other tidies could be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM