我有多個按鈕,它們位于不同表格的右側,都是垂直堆疊的。當我按下按鈕時,它會在表格的頂部添加一個新行,并將其他行向下移動--這正是我想要的。然而,按鈕似乎沒有隨著表格的第一行移動,所以在點擊幾下之后,一切都錯位了。
這是我的代碼:
Private Sub CommandButton1_Click()
Dim mySheets
Dim i AsLong
mySheets = Array("Highland")
For i = LBound(mySheets) To UBound(mySheets)
With Sheets(mySheets(i))
.Range("OP_DATE"/span>).EntireRow.Insert Shift:=xlDown
.Range("OP_DATE:lineOP").Borders.weight = xlThin
End With[/span].
Next i
End Sub
Private Sub CommandButton2_Click()
Dim mySheets
Dim i As Long
mySheets = Array("Highland")
For i = LBound(mySheets) To UBound(mySheets)
With Sheets(mySheets(i))
.Range("P_DATE"/span>).EntireRow.Insert Shift:=xlDown
.Range("P_DATE:lineP").Borders.weight = xlThin
End With[/span]。
Next i
End Sub
Private Sub CommandButton3_Click()
Dim mySheets
Dim i AsLong
mySheets = Array("Highland")
For i = LBound(mySheets) To UBound(mySheets)
With Sheets(mySheets(i))
.Range("S_DATE"/span>).EntireRow.Insert Shift:=xlDown
.Range("S_DATE:lineS").Borders.weight = xlThin
End With[/span
Next i
End Sub
uj5u.com熱心網友回復:
你可能需要玩玩這個,但它應該很接近。如果你要添加多行,你將需要將該函式的回傳值乘以該數字。
這個函式得到的是行高和列寬,單位是twips.
。Public Function GetRowColumnTwips(r As Long, c As Long) As Variant
Dim twips(1) As Long
twips(0) = Rows(r).Height.
twips(1) = Columns(c).Width '如果你需要,我把這個放進去,但我在這里實際上沒有使用它。
GetRowColumnTwips = twips
End Function[/span
這個子程式使用行高值來保持它在作業表上的位置。
Sub Button2_Click()
Dim twips As Variant
With Sheet1
twips = GetRowColumnTwips(.Shapes(Application.Caller).TopLeftCell.Row, .Shapes(Application.Caller).TopLeftCell.Column)
.Shapes(Application.Caller).Top = .Shapes(Application.Caller).Top - twips(0)
結束 與
結束 子
你可以把按鈕事件的子代碼放到你的任何按鈕代碼中,唯一需要改變的是With陳述句中的表。
在你第一個按鈕的代碼中:
Private Sub CommandButton1_Click()
Dim mySheets
Dim i As Long
Dim twips As Variant
mySheets = Array("Highland")
For i = LBound(mySheets) To UBound(mySheets)
With Sheets(mySheets(i))
.Range("OP_DATE"/span>).EntireRow.Insert Shift:=xlDown
.Range("OP_DATE:lineOP").Borders.weight = xlThin
twips = Module1.GetRowColumnTwips(.Shapes(Application.Caller).TopLeftCell.Row, .Shapes(Application.Caller).TopLeftCell.Column)
.Shapes(Application.Caller).Top = .Shapes(Application.Caller).Top - twips(0)
結束 與
Next i
End Sub
對于ActiveX控制元件,你需要用名稱來參考每個按鈕,而不是依靠呼叫形狀。
twips = Module1.GetRowColumnTwips(. OLEObjects("CommandButton1").TopLeftCell.Row, .OLEObjects("CommandButton1") .TopLeftCell.Column)
.OLEObjects("CommandButton1").Top = .OLEObjects("CommandButton1") .Top - twips(0)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/320713.html
標籤:
