Excel VBA问题:输入框添加想法+点赞按钮,解决按钮重复生成问题
Hey there! Let’s sort out this button placement issue. The root problem with your current code is that you’re using fixed pixel coordinates when adding the button—so it keeps landing in the exact same spot every time. Instead, we need to tie the button’s position directly to the row where you just added the new idea, so it automatically shows up in the next available row.
Here’s the revised code that fixes this:
Sub VBA_Input_Idea_inputbox() Dim MyInp As String Dim NextRow As Long Dim targetCell As Range Dim newButton As Button MyInp = VBA.Interaction.InputBox("Please input idea", "LEARNING REQUEST") If MyInp = "" Then Exit Sub NextRow = Cells(Rows.Count, 3).End(xlUp).Row + 1 ' Add formatted idea to column C Range("C" & NextRow).Value = Excel.WorksheetFunction.Proper(MyInp) ' Target column A of the new row for the button Set targetCell = Range("A" & NextRow) ' Add button positioned exactly over the target cell Set newButton = ActiveSheet.Buttons.Add( _ Left:=targetCell.Left, _ Top:=targetCell.Top, _ Width:=targetCell.Width, _ Height:=targetCell.Height _ ) ' Configure button behavior and appearance newButton.OnAction = "Addcount" newButton.Caption = "👍" ' Replace with "Like" if you prefer plain text End Sub
Key Changes Explained:
- Removed fixed coordinates: Instead of hardcoding
0.75, 145.5, 42, 24.75, we use theLeft,Top,Width, andHeightproperties of the target cell. This ensures the button fits perfectly in the cell and moves with it if you resize rows/columns later. - Used an object variable for the button: Declaring
newButton As Buttonlets us set its properties directly without relying onSelect—a practice that’s more reliable and faster in VBA. - Added a clear caption: I used a thumbs-up emoji for a visual "like" button, but you can swap that with
"Like"or any text you want.
Now every time you add a new idea, the like button will automatically appear in column A of the same row—no more overlapping buttons!
内容的提问来源于stack exchange,提问作者JMSC
相关产品推荐
相关产品推荐

