如何将ActiveX按钮锁定到特定的列索引

时间:2014-04-28 13:46:41

标签: excel vba excel-vba activex excel-2007

我需要将ActiveX按钮锁定到特定的列索引。单击时,该按钮运行VBA代码,删除三列,然后取消选中时,VBA代码基本上撤消它之前的删除,并再次插入3个新列。

VBA代码运行正常。问题是命令按钮本身一直向下滑动,一次三列,直到它位于我的数据顶部。

我尝试过锁定/解锁按钮,但这似乎并不重要。

我想知道是否有一种方法可以将此命令按钮锁定到特定列索引而不仅仅是特定列。因此,例如,我想将其锁定到列索引10,即列J.这样,当按钮被激活并删除/替换行时,按钮保持在列索引10的任何列中。

以下是问题的前后图片: Before

After

此外还有VBA代码在每次按下时都会运行(不确定它是否相关,但我认为它不会受到影响):

    Private Sub CheckBox1_Click()
Dim Array1() As String 'creates a dynamic array
Dim Temp As Worksheet
Dim Alias_Adds As Worksheet
Dim LastRow As Long

Set Alias_Adds = Sheets("Alias_Adds")
Set Temp = Sheets("Temp")

'find last row in sheet
With Sheets("Alias_Adds")
    LastRow = .Range("B" & .Rows.Count).End(xlUp).Row
End With

'declares the length of Array1 to the value of LastRow
ReDim Preserve Array1(1 To LastRow) As String

'conditional statements start
If CheckBox1.Value = True Then

'stores the values from the cells in column F within Array1
For i = 1 To LastRow
Array1(i) = Alias_Adds.Cells(i, "F")
Next i

'prints the contents of Array1 to the Temp sheet
Temp.Columns("F").NumberFormat = "@"
For m = 1 To LastRow
Temp.Cells(m, "F") = Array1(m)
Next m


With Alias_Adds
        Alias_Adds.Range("A:A,F:F,G:G").Delete
        'Alias_Adds.Columns("E:F").EntireColumn.Delete 'set to E:F because after A is deleted in the above line, the columns shift to the left
    End With

End If 'end CheckBox1.Value = True boolean statement

If CheckBox1.Value = False Then
        'insert a new column to the left of current column A in Alias_Adds sheet
        Alias_Adds.Columns("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

        'for loop to print validation function in newly inserted column A
        Alias_Adds.Range("A:A,F:F,G:G").Interior.Color = RGB(192, 192, 192)
        Alias_Adds.Columns("A").NumberFormat = "General" 'set the NumberFormat of the column to General before the For loop to print functions
        For j = 1 To LastRow
        If (j > 1) Then
        Alias_Adds.Cells(j, "A") = "=B" & j & "&C" & j & "&D" & j
        Else: Alias_Adds.Cells(j, "A") = "Concatenate Function"
        End If
        Next j

        'for loop to print the stored the values from the Temp sheet to the cells in column F
        For k = 1 To LastRow
        Alias_Adds.Cells(k, "F") = Temp.Cells(k, "F")
        Next k

        'for loop to print validation function in column G
        Alias_Adds.Columns("G").NumberFormat = "General" 'set the NumberFormat of the column to General before the For loop to print functions
        For l = 1 To LastRow
        If (l > 1) Then
        Alias_Adds.Cells(l, "G") = "=COUNTIF(A:A,A" & l & ")"
        Else: Alias_Adds.Cells(l, "G") = "Duplicate Check?"
        End If
        Next l


End If 'end CheckBox1.Value = False boolean statement

End Sub

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情。每次触发worksheet_change事件时,它都会将命令按钮移动到某个点:

Private Sub worksheet_change(ByVal target As Range)
CommandButton1.Left = 216.75
End Sub

修改数字216.75以满足您的需求。