Excel vba 重命名复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6097761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:17:00  来源:igfitidea点击:

Excel vba Renaming checkboxes

excelvbacheckboxrenaming

提问by Marc

I have a worksheet that contains many checkboxes. Excel names these automatically as CheckBox1, CheckBox2, etc... However, for my document, I need to rename them all as Rij11_1, Rij11_2, etc (Rij11 being row 11, _1 being the first checkbox in that row, and so on). It is important that the renaming starts on _1 for each row.

我有一个包含许多复选框的工作表。Excel 将这些自动命名为 CheckBox1、CheckBox2 等...但是,对于我的文档,我需要将它们全部重命名为 Rij11_1、Rij11_2 等(Rij11 是第 11 行,_1 是该行中的第一个复选框,依此类推) . 对于每一行,重命名从 _1 开始很重要。

Stackoverflow members Osknows and Dave DuPlantis helped me already on this one (THANKS A LOT FOR ALL YOUR HELP SO FAR), with this code:

Stackoverflow 成员 Osknows 和 Dave DuPlantis 已经帮助我解决了这个问题(非常感谢您到目前为止的所有帮助),代码如下:

Sub test()  
    Dim obj As OLEObject, ChkBoxRow as long  
    ChkBoxRow = 11   
    With Worksteets("Storia") 
    For Each obj In .OLEObjects     
    If TypeName(obj.Object) = "CheckBox" Then          
        if obj.TopLeftCell.Row = ChkBoxRow then            
            obj.Name = "Rij11_" & Right(obj.Name, 1)         
        end if      
    End If 
    Next obj
    End With
End Sub 

However, the first renamed checkbox on each row does not start on 1 (usually on 7, for some reason which escapes me), and if there are more than 10 checkboxes in a row, the numbering doesn't go beyond 10. After _9, I get _0 instead of _10 and then, after _0, it continues with _1 again, resulting in name duplicates in the row.

但是,每行上第一个重命名的复选框不是从 1 开始的(通常是 7,出于某种原因,这让我无法理解),如果一行中有 10 个以上的复选框,编号不会超过 10。在 _9 之后, 我得到 _0 而不是 _10 ,然后,在 _0 之后,它再次以 _1 继续,导致行中的名称重复。

Is there anyone who can help me out with this renumbering issue?

有没有人可以帮我解决这个重新编号的问题?

Thanks a lot!

非常感谢!

Kind regards, Marc

亲切的问候,马克

回答by Parag

Here's an easy way to get rid of these. Highlight the column or cells in which you want to remove the checkboxes and associated text. Then cut them using CTRL+C, after that move into a different sheet, paste them. (This method is if say you want to remove one column of checkboxes but keep another, or delete random checkboxes). Now hit F5, then Special, then Objects (on the sheet where you pasted the ones for deletion) they will all be selected, now just cut them, DONE! :)

这是摆脱这些的简单方法。突出显示要删除复选框和相关文本的列或单元格。然后使用 CTRL+C 剪切它们,然后移动到不同的工作表中,粘贴它们。(此方法是如果您想删除一列复选框但保留另一列,或删除随机复选框)。现在按 F5,然后是特殊,然后是对象(在您粘贴要删除的工作表上)它们都将被选中,现在只需剪切它们,完成!:)

回答by Excellll

The issue is with the Right(obj.Name, 1)in the inner If statement. The renaming only sticks the rightmost character of the old object name onto the end of the new name. Try replacing it with the following:

问题在于Right(obj.Name, 1)内部 If 语句中的 。重命名仅将旧对象名称的最右侧字符粘贴到新名称的末尾。尝试将其替换为以下内容:

Right(obj.Name, Len(obj.Name) - 8)

Where 8 is the length of "CheckBox".

其中 8 是“CheckBox”的长度。