简体   繁体   English

选择范围时出现运行时错误'1004'

[英]run-time error '1004' on range selecting

I get run-time error '1004' on this code 我在此代码上收到运行时错误“ 1004”
sh2 is a specific sheet on another file. sh2是另一个文件上的特定工作表。 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. 当我尝试调试时:y为49(必须如此),else下范围的单元格将获得自己正确的值。 Where is my error? 我的错误在哪里?

PS. PS。 I'm using excel 2013 and the y is set to 0 before this code 我正在使用excel 2013并且在此代码之前y设置为0

尝试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 ? 您的原始代码对我有用-如果删除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. 我认为如果您放弃sh2. from within the sh2.range it will work. sh2.range内它将起作用。

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

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

Untested though 虽然未经测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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