英文:
Range("A1").Select generates error code 1004
问题
错误代码1004弹出。
代码行Range("A1").Select
被突出显示。
我尝试在代码周围使用activate,但没有用。我在立即窗口中测试了这行代码,它可以运行。
英文:
The error code 1004 pops up.
The line of code Range("A1").Select
is highlighted.
I tried using activate around the code but to no avail. I tested the line of code in the immediate window and it works.
答案1
得分: 1
不管您的宏录制代码如何,这在某些 Excel 版本和构建版本中有时会发生。有时,Worksheet 对象的 "Select" 方法与其他 Excel 版本的方式不完全相同,您需要使用 "Activate" 方法代替。
改为:
ThisWorkbook.Worksheets("FX rate").Activate
Range("A1").Select
英文:
Regardless of your macro recorded code, this sometimes happens in some versions and builds of Excel. Sometimes the "Select" method of Worksheet object doesn't work exactly the same way as in other Excel version and you have to use Activate method instead.
Instead of:
ThisWorkbook.Sheets("FX rate").Select
Range("A1").Select
try this:
ThisWorkbook.Worksheets("FX rate").Activate
Range("A1").Select
答案2
得分: 0
这可能是因为您的代码位于工作表对象而不是模块中。
最快的修复方法是将 Range("A1").Select
更改为 ThisWorkbook.Sheets("汇率").Range("A1").Select
,但您在代码的其余部分可能仍然会遇到更多问题,这部分代码您没有贴出来。
更好的修复方法是将您的代码移到一个模块中,使用 With
/ End With
,或者参考 @Ike 的链接来避免使用选择操作。
英文:
This can happen if your code is in a sheet object instead of a module.
The quickest fix is to change Range("A1").Select
to ThisWorkbook.Sheets("FX rate").Range"A1").Select
but you will still have more problems further down the rest of your code that you haven't posted.
A better fix is to move your code to a module, use With
/ End With
, or follow @Ike's link to avoid using select.
答案3
得分: 0
你真的应该尽量避免使用 Select
。重新编写宏将生成这段代码,但然后你应该修改代码以将其移除。即使执行得完美,用户在其运行时点击屏幕时也可能出现问题。
对于你示例的第一部分,你可以这样做:
With ThisWorkbook.Worksheets("Nostro bals - 5010")
.Range("A2:F1000").ClearContents
End With
对于第二部分,你的代码通过使用等同于 CTRL-Down
从 A1 开始找到数据的底部。这意味着如果数据中断,它只会清除到那一行。如果这是你的意图,那么可以使用以下方法:
With ThisWorkbook.Worksheets("FX rate")
ex_end_row = .Cells(1, 1).End(xlDown).Row
.Range("A3:B" & ex_end_row).ClearContents
End With
但是,如果你实际上希望它找到列 A 中的数据的最后一个单元格,而不考虑中断等情况,那么可以使用这个方法,它从列 A 的最底部开始,然后使用 CTRL-Up
:
With ThisWorkbook.Worksheets("FX rate")
ex_end_row = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("A3:B" & ex_end_row).ClearContents
End With
请注意,无论使用哪种方法,你都应该在前面声明你的 ex_end_row:
Dim ex_end_row As Long
英文:
You really should try to avoid using Select
entirely. Recoding a macro will produce this code but you should then alter the code to remove it. Even when done perfectly, it can go badly for instance if the user clicks the screen while it's running.
For the first part of your example, you could do the following:
With ThisWorkbook.Worksheets("Nostro bals - 5010")
.Range("A2:F1000").ClearContents
End With
For the second part, your code is finding the bottom of the data by using the equivalent of CTRL-Down
from A1. This means if there is a break in the data, it will only clear down to that row. If that is your intention, then you can use the following:
With ThisWorkbook.Worksheets("FX rate")
ex_end_row = .Cells(1, 1).End(xlDown).Row
.Range("A3:B" & ex_end_row).ClearContents
End With
However, if you actually want it to find the last cell in column A with data in it, regardless of breaks etc, then you could use this, which starts at the very bottom of column A and then uses CTRL-Up
:
With ThisWorkbook.Worksheets("FX rate")
ex_end_row = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("A3:B" & ex_end_row).ClearContents
End With
Note, with either of these, you should declare your ex_end_row beforehand:
Dim ex_end_row As Long
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论