2014-04-28 2 views
0

ActiveX 단추를 특정 열 인덱스에 고정해야합니다. 이 버튼을 클릭하면 버튼이 3 개의 열을 삭제하는 VBA 코드를 실행 한 다음 선택을 해제하면 VBA 코드가 이전 삭제를 원래대로 되돌리고 3 개의 새 열을 다시 삽입합니다.ActiveX 단추를 특정 열 인덱스에 고정하는 방법

VBA 코드는 정상적으로 작동합니다. 문제는 명령 단추 자체가 내 데이터 맨 위에 앉아있을 때까지 한 번에 3 개 열씩 내 시트 아래로 계속 이동한다는 것입니다.

버튼 잠금/잠금 해제를 시도했지만 문제가되지 않습니다.

난 그냥 특정 열 대신 특정 열 인덱스이 명령 단추를 잠글 수있는 방법이 있는지 알고 싶습니다

. 예를 들어, 열 J 인 열 인덱스 10에이 열을 고정시키고 자합니다. 그러면 단추가 활성화되고 행이 삭제되거나 대체 될 때 열은 열 인덱스 10에있는 열에 넣어집니다.

여기

이전과 문제의 사진을 쫓고 : 그것은 관련이 있는지 확실하지 않습니다 (각 키를 누릅니다에 VBA 코드를 실행하는 여기 또한 Before

After

하지만 난 그것을 해치지 않을 수 생각 포함) :

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 

답변

0

다음과 같이 시도해 볼 수 있습니다.

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

사용자의 요구에 맞게 숫자 216.75를 수정 : 그것은 특정 지점에 worksheet_change 이벤트가 트리거 될 때마다 명령 단추를 이동합니다.

+0

굉장히 좋을 것입니다. 감사! – FluffyKittens

+0

np, 내가 도울 수있어서 다행 :) – Pedrumj