سؤال

I need to lock an ActiveX button to a specific column index. When clicked, the button runs VBA code which deletes three columns, and then when unchecked, the VBA code essentially undoes it's previous deletion, and inserts 3 new columns again.

The VBA code works fine. The issues is that the command button itself keeps sliding its way down my sheet, three columns at a time, until it is sitting over the top of my data.

I have tried locking/unlocking the buttons, but that doesn't seem to matter.

I would like to know if there is a way that I can lock this command button to a specific column index instead of just a specific column. So for example, I would like to lock it to column index 10, which is column J. That way, when the button is activated and rows are deleted/replaced, the button stays put in whatever column is at column index 10.

Here are before and after pictures of the issue: Before

After

Also here is the VBA code which is run on each press (not sure if it is relevant but I figured it couldn't hurt to include):

    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
هل كانت مفيدة؟

المحلول

You could try something like this. It moves the command button to a certain point every time the worksheet_change event is triggered:

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

Modify the number 216.75 to fit your needs.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top