hainam121991
Thành viên mới

- Tham gia
- 1/7/15
- Bài viết
- 7
- Được thích
- 0
Mình làm hoài không được mong các bạn giúp đỡ. Mục đích mình muốn tạo 1 menu trên menu bar, menu này không bị mất khi mở file excel khác (Mình mới làm được menu chỉ hiện trong file excel được lưu), tương tự như 1 phần menu chức năng của excel.
Phần code nó đây mong các bạn xem giúp:
Phần chức năng mình: Xóa hàng khi tại cells đó có ký tự nằm trong giá trị của cells. mình cho lặp nhưng vẫn sót 1 giá trị, mình chưa tìm ra nguyên nhân.
Phần code nó đây mong các bạn xem giúp:
Mã:
Sub CreateMenu()Dim cmbBar As CommandBar
Dim cmbControl As CommandBarControl
Set cmbBar = Application.CommandBars("Worksheet Menu Bar")
Set cmbControl = cmbBar.Controls.Add(Type:=msoControlPopup, temporary:=True) 'adds a menu item to the Menu Bar
With cmbControl
.Caption = "&My Macros" 'names the menu item
With .Controls.Add(Type:=msoControlButton) 'adds a dropdown button to the menu item
.Caption = "Delete character" 'adds a description to the menu item
.OnAction = "DeleteCharacter" 'runs the specified macro
.FaceId = 1098 'assigns an icon to the dropdown
End With
With .Controls.Add(Type:=msoControlButton)
.Caption = "My Macro No 2"
.OnAction = "RunMyMacro2"
.FaceId = 108
End With
End With
End Sub
Private Sub Workbook_Open()
Call CreateMenu
MsgBox “A new menu (MyMenu) was created.”, vbInformation
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next 'in case the menu item has already been deleted
Application.CommandBars("Worksheet Menu Bar").Controls("My Macros").Delete 'delete the menu item
End Sub
Sub DeleteCharacter()
Dim k As Integer
For i = 1 To 8
k = i
If InStr(Cells(i, 2), "1") > 0 Or InStr(Cells(i, 2), "2") > 0 Or InStr(Cells(i, 2), "3") > 0 Then k = i - 1
If InStr(Cells(i, 2), "1") > 0 Or InStr(Cells(i, 2), "2") > 0 Or InStr(Cells(i, 2), "3") > 0 Then Rows(i).Delete
If k = 0 Then k = 1
i = k
Next i
End Sub
Phần chức năng mình: Xóa hàng khi tại cells đó có ký tự nằm trong giá trị của cells. mình cho lặp nhưng vẫn sót 1 giá trị, mình chưa tìm ra nguyên nhân.