简体   繁体   中英

run-time error '1004' on range selecting

I get run-time error '1004' on this code
sh2 is a specific sheet on another file. I don't want to activate the second sheet,just use it and then closing the file. I get my error on the else condition

If sh2.Range(sh2.Cells(1, y + 4), sh2.Cells(1, y + 4)) <> "" Then
                        y = y + 1
Else: sh2.Range(sh2.Cells(1, 4), sh2.Cells(114, (y + 4) - 1)).Select
                        Selection.Copy

When I'm trying to debug: the y is 49 (as it had to be) and the cells of the range under the else get their own and right values. Where is my error?

PS. I'm using excel 2013 and the y is set to 0 before this code

尝试sh2.Range(sh2.Cells(1, 4), sh2.Cells(114, (y + 4) - 1)).Copy (不选择)我认为除非激活它,否则您无法选择工作表上的任何内容

Your original code worked for me - what happens if you drop the Select ?

Dim sh2 As worksheet
Set sh2 = Sheets(2)

y = 49
If sh2.Range(sh2.Cells(1, y + 4), sh2.Cells(1, y + 4)) <> "" Then
    y = y + 1
Else
    sh2.Range(sh2.Cells(1, 4), sh2.Cells(114, (y + 4) - 1)).Copy
End If

I think if you drop the sh2. from within the sh2.range it will work.

Change sh2.Range(sh2.Cells(1, y + 4), sh2.Cells(1, y + 4)) to

sh2.Range(Cells(1, y + 4), Cells(1, y + 4))

Untested though

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