简体   繁体   中英

Run-time Error '1004' in excel vba

For some reason I keep getting a range global runtime error when I try to run the following script. Any help would be greatly appreciated.

Sub crossUpdate()
Dim rng As Range

Set rng = Sheet1.Cells.Range("A2").End(xlDown)

Range(rng).Select



End Sub

Try changing your code into:

Sub crossUpdate()
   Dim rng As Range
   ActiveWorkbook.Sheets("Sheet1").Select
   Set rng = ActiveWorkbook.Sheets("Sheet1").Range("A2").End(xlDown)
   rng.Select
End Sub

Whenever you select a range, you must have already Select ed the proper sheet. Also fix the Select command Try:

Sub crossUpdate()
    Dim rng As Range
    Set rng = Sheet1.Cells.Range("A2").End(xlDown)
    Sheet1.Select
    rng.Select
End Sub

EDIT#1

This version will Select the block of cells:

Sub crossUpdate()
    Dim rng As Range, N As Long
    Sheet1.Select
    N = Cells(Rows.Count, "A").End(xlUp).Row
    Set rng = Cells.Range("A2:A" & N)
    rng.Select
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