Gán rowsoure cho combobox để tạo file add-in (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

thuongsykxps

Thành viên chính thức
Tham gia
3/6/08
Bài viết
67
Được thích
20
Chào cả nhà,

Mình tạo file này để chọn mã công tác (Bấm F6 sẽ hiện form mã công tác và chọn). Vấn đề ở chổ, Form chạy trên file này thì bình thường. Nhưng mình muốn tạo file này thành một add-in để chạy cho tất cả các file excel thì báo lỗi, (hình như lỗi ở rowsoure của combobox).

Mong mọi người giúp đỡ và tạo file add-in để chạy giúp mình.

Cảm ơn cả nhà.
 

File đính kèm

Chào cả nhà,

Mình tạo file này để chọn mã công tác (Bấm F6 sẽ hiện form mã công tác và chọn). Vấn đề ở chổ, Form chạy trên file này thì bình thường. Nhưng mình muốn tạo file này thành một add-in để chạy cho tất cả các file excel thì báo lỗi, (hình như lỗi ở rowsoure của combobox).

Mong mọi người giúp đỡ và tạo file add-in để chạy giúp mình.

Cảm ơn cả nhà.
- 2 cái OptionButtons thì nhập Caption vào cho nó để khi click vào tên cũng check được, chứ dò dò chuột bao giờ mới trúng cái nút tròn bé tí tẹo đấy.
Xóa vứt bỏ 2 cái Labels ở bên cạnh đi, căn chỉnh có thẳng với 2 cái OptionButtons đâu chứ.

- Muốn chuyển thành Addins thì không dùng Onkey, dùng command bar hoặc Button trên Ribbon.
(Mở file lên là thấy lỗi ngay chỗ Onkey rồi).

- Đã xài tới VBA rồi thì không dùng Name, chuyển dữ liệu cần gán vào Combobox vào mảng.

- Mong muốn dùng Addins thì phải có chỗ thiết lập lấy được vùng dữ liệu bất kỳ ở file bất kỳ để gán vào Combobox.

- Tham khảo cái này:
http://www.giaiphapexcel.com/dienda...t-hỗ-trợ-nhập-liệu-từ-danh-mục-có-sẵn.116711/
 
Upvote 0
Mã:
ComboBoxTS1.RowSource = ThisWorkbook.Names("MACT").RefersToRange.Address(, , xlA1, True)
Thử xem có được không, chạy bình thường thì ok, còn thành add in thì chưa thử được.
 
Upvote 0
- 2 cái OptionButtons thì nhập Caption vào cho nó để khi click vào tên cũng check được, chứ dò dò chuột bao giờ mới trúng cái nút tròn bé tí tẹo đấy.
Xóa vứt bỏ 2 cái Labels ở bên cạnh đi, căn chỉnh có thẳng với 2 cái OptionButtons đâu chứ.

- Muốn chuyển thành Addins thì không dùng Onkey, dùng command bar hoặc Button trên Ribbon.
(Mở file lên là thấy lỗi ngay chỗ Onkey rồi).

- Đã xài tới VBA rồi thì không dùng Name, chuyển dữ liệu cần gán vào Combobox vào mảng.

- Mong muốn dùng Addins thì phải có chỗ thiết lập lấy được vùng dữ liệu bất kỳ ở file bất kỳ để gán vào Combobox.

- Tham khảo cái này:
http://www.giaiphapexcel.com/diendan/threads/add-in-input-from-list-hỗ-trợ-nhập-liệu-từ-danh-mục-có-sẵn.116711/
Link này cũng thử rồi, nhưng bị lỗi trên office 64bit. Bỏ Onkey rồi nhưng vẫn bị lỗi, không thể dùng addins được. Bạn giúp mình tạo thành file add tý.
thanks!
 
Upvote 0
Bạn giúp mình tạo thành file add tý.
thanks!
Muốn tạo thành AddIn phải thêm nhiều công đoạn nữa. Giờ sửa trên file của bạn luôn, ta làm như sau:
1> Sửa code trên UserForm thành:
Mã:
Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
   If KeyCode = 27 Then Unload Me
End Sub
Private Sub CommandButtonTS1_Click()
  ActiveCell.Value = ComboBoxTS1.Value
  Unload Me
End Sub
Private Sub ButtonTS1_Click()
  If ButtonTS1.Value = True Then ComboBoxTS1.List = ThisWorkbook.Worksheets("MACT").Range("MACT").Value
End Sub
Private Sub ButtonTS2_Click()
  If ButtonTS2.Value = True Then ComboBoxTS1.List = ThisWorkbook.Worksheets("MACT").Range("MATT").Value
End Sub
Private Sub UserForm_Terminate()
 Application.Run "modCBar.HideForm"
End Sub
(riêng code sự kiện UserForm_KeyDown tôi cũng không biết bạn cần để làm gì nên cứ để yên đó tùy bạn xóa hoặc không)
2> Sửa code trên Thisworkbook thành:
Mã:
Private Sub Workbook_AddinInstall()
  Application.Run "BuildBar"
End Sub
Private Sub Workbook_AddinUninstall()
  Application.Run "DelBar"
End Sub
3> Sửa tên Module của bạn thành modCBar và code bên trong như sau:
Mã:
Private Sub Auto_Open()
  BuildBar
End Sub
Private Sub Auto_Close()
  DelBar
End Sub
Private Sub BuildBar()
  Dim cBar As CommandBar
  DelBar
  Set cBar = Application.CommandBars.Add("Create Combobox list")
  With cBar
    .Position = msoBarTop
    .Visible = True
     With .Controls.Add(msoControlButton)
       .Caption = "Show Form"
       .Style = msoButtonIconAndCaption
       .OnAction = "ShowForm"
       .FaceId = 156
     End With
  End With
End Sub
Private Sub DelBar()
  On Error Resume Next
  Application.CommandBars("Create Combobox list").Delete
End Sub
Private Sub ShowForm()
  On Error Resume Next
  Dim ctlBtt As CommandBarButton
  Set ctlBtt = Application.CommandBars("Create Combobox list").Controls("Show Form")
  With ctlBtt
    .Caption = "Hide Form"
    .OnAction = "HideForm"
    .FaceId = 189
  End With
  UserFormTS1.Show False
End Sub
Private Sub HideForm()
  On Error Resume Next
  Dim ctlBtt As CommandBarButton
  Set ctlBtt = Application.CommandBars("Create Combobox list").Controls("Hide Form")
  With ctlBtt
    .Caption = "Show Form"
    .OnAction = "ShowForm"
    .FaceId = 156
  End With
  Unload UserFormTS1
End Sub
Khi bạn lưu file thành AddIn rồi gọi AddIn này lên thì lập tức trên thanh Ribbon sẽ xuất hiện 1 nút cho bạn gọi form

Capture.JPG
 

File đính kèm

Upvote 0
Muốn tạo thành AddIn phải thêm nhiều công đoạn nữa. Giờ sửa trên file của bạn luôn, ta làm như sau:
1> Sửa code trên UserForm thành:
Mã:
Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
   If KeyCode = 27 Then Unload Me
End Sub
Private Sub CommandButtonTS1_Click()
  ActiveCell.Value = ComboBoxTS1.Value
  Unload Me
End Sub
Private Sub ButtonTS1_Click()
  If ButtonTS1.Value = True Then ComboBoxTS1.List = ThisWorkbook.Worksheets("MACT").Range("MACT").Value
End Sub
Private Sub ButtonTS2_Click()
  If ButtonTS2.Value = True Then ComboBoxTS1.List = ThisWorkbook.Worksheets("MACT").Range("MATT").Value
End Sub
Private Sub UserForm_Terminate()
 Application.Run "modCBar.HideForm"
End Sub
(riêng code sự kiện UserForm_KeyDown tôi cũng không biết bạn cần để làm gì nên cứ để yên đó tùy bạn xóa hoặc không)
2> Sửa code trên Thisworkbook thành:
Mã:
Private Sub Workbook_AddinInstall()
  Application.Run "BuildBar"
End Sub
Private Sub Workbook_AddinUninstall()
  Application.Run "DelBar"
End Sub
3> Sửa tên Module của bạn thành modCBar và code bên trong như sau:
Mã:
Private Sub Auto_Open()
  BuildBar
End Sub
Private Sub Auto_Close()
  DelBar
End Sub
Private Sub BuildBar()
  Dim cBar As CommandBar
  DelBar
  Set cBar = Application.CommandBars.Add("Create Combobox list")
  With cBar
    .Position = msoBarTop
    .Visible = True
     With .Controls.Add(msoControlButton)
       .Caption = "Show Form"
       .Style = msoButtonIconAndCaption
       .OnAction = "ShowForm"
       .FaceId = 156
     End With
  End With
End Sub
Private Sub DelBar()
  On Error Resume Next
  Application.CommandBars("Create Combobox list").Delete
End Sub
Private Sub ShowForm()
  On Error Resume Next
  Dim ctlBtt As CommandBarButton
  Set ctlBtt = Application.CommandBars("Create Combobox list").Controls("Show Form")
  With ctlBtt
    .Caption = "Hide Form"
    .OnAction = "HideForm"
    .FaceId = 189
  End With
  UserFormTS1.Show False
End Sub
Private Sub HideForm()
  On Error Resume Next
  Dim ctlBtt As CommandBarButton
  Set ctlBtt = Application.CommandBars("Create Combobox list").Controls("Hide Form")
  With ctlBtt
    .Caption = "Show Form"
    .OnAction = "ShowForm"
    .FaceId = 156
  End With
  Unload UserFormTS1
End Sub
Khi bạn lưu file thành AddIn rồi gọi AddIn này lên thì lập tức trên thanh Ribbon sẽ xuất hiện 1 nút cho bạn gọi form

View attachment 181452
Rất hay và tiện lợi, Cảm ơn Thầy Ndu nhiều !!!!!
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom