本文介绍了ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row返回错误的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个小宏在受保护的工作表中添加和删除行.为此,每行都有一个删除"按钮.添加新行时,将复制最后一个现有行(包括删除"按钮),然后清除其内容.

I'm using a small macro to add and delete rows in a protected worksheet. For this each line has a "Delete" button. When a new line is added, the last existing line is copied (including the "Delete" button) and then its content is cleared.

要删除行,我需要交出一些参数,包括删除"按钮的行(以及项目的行).为此,我使用:

For deleting the rows I hand over some parameters including the "Delete" button's (and thus the item's) row. For this I use:

RowToDelete = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row

此代码对于现有行工作正常,但对于自上次打开工作簿以来添加的所有行(最后一个除外),它将失败.对于其他所有内容,它将删除添加的下一行,而不是真实"一行.

This code works fine for existing lines, but it will fail for all lines added since the workbook was last opened (except for the last one). For all other it will delete the next line that was added instead of the "real" one.

我猜这是一个缓存问题,因为"Application.Caller"包含正确的字符串,但返回的复制形状行不再包含该名称.

I guess that this is a cache problem, as the "Application.Caller" contains the right string, but returns the row of the copied shape that no longer carries that name.

是否可以清除相关缓存或强制Shapes()不使用缓存?

Is there a way to clear the relevant cache or to force Shapes() to not use the cache?

非常感谢您的帮助.

推荐答案

我遇到了同样的问题.这些按钮是由其他按钮(例如您的按钮)创建的.但是,只有在创建按钮的代码中开始为它添加名称时,它才开始这样做.因此,另一种不太令人满意的处理方法是仅没有"shp.name ="行.当我取出来时,它停止了.我的猜测是,以一种自然的方式来命名按钮,它自然可以避免这种情况.就像拥有工作表,删除工作表并添加其他工作表一样,它仍会记住旧工作表并将新工作表命名为更高的数字.删除缓存的代码会很好.我敢肯定,如果问题的提出方式有所不同,那么有人会知道该怎么做.搜索得到:Application.Restart()我还没有尝试过.

I had the same problem. The buttons were created by other buttons like yours. However, it only started doing it when in the code that creates the buttons, I started adding a name to it. So another not so satisfying way of dealing with it is to just not have the "shp.name= " line. When I took that out it stopped doing it. My guess is that in the natural way of naming buttons it naturally avoids that. Just like when you have a sheet, delete it, and add another sheet, it still remembers the old sheet and names the new sheet a higher number.A code to delete the cache would be nice. I am sure somebody knows how to do that if the question was asked differently. Searching for that gives:Application.Restart()I haven't tried it though.

这篇关于ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row返回错误的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 07:14