Combo Box อยากให้ช่องพอดีกับข้อความที่แสดง
Posted: Sat Mar 01, 2014 9:55 pm
สวัสดีครับ
ผมได้ลองทำ Data Validation ให้ผู้ใช้ใส่ข้อมูลตัวเลข ตาม sheet เกณฑ์ โดยให้ใส่ตัวเลข แต่พบปัญหาว่าข้อมูลใน Combo Box ใน Design Mode มันไปกดทับตัวหนังสือไว้ ทำให้เห็นตัวหนังสือไม่หมด ผมไม่รู้ว่าต้องใช้วิธีใหน หรือต้องแก้ไขตรงใหนเพื่อให้มันพอกับตัวหนังสือครับ ผมใช้ Code ที่ว่าถ้าพบ Data Validation ให้มันกลายร่างเป็น Combo Box เพราะผมทราบมาว่า Data validation ตรงๆ ไม่สามารถทำได้
ผมได้ลองทำ Data Validation ให้ผู้ใช้ใส่ข้อมูลตัวเลข ตาม sheet เกณฑ์ โดยให้ใส่ตัวเลข แต่พบปัญหาว่าข้อมูลใน Combo Box ใน Design Mode มันไปกดทับตัวหนังสือไว้ ทำให้เห็นตัวหนังสือไม่หมด ผมไม่รู้ว่าต้องใช้วิธีใหน หรือต้องแก้ไขตรงใหนเพื่อให้มันพอกับตัวหนังสือครับ ผมใช้ Code ที่ว่าถ้าพบ Data Validation ให้มันกลายร่างเป็น Combo Box เพราะผมทราบมาว่า Data validation ตรงๆ ไม่สามารถทำได้
Code: Select all
Option Explicit
Private Sub TempCombo_KeyDown(ByVal _
KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
'Hide combo box and move to next cell on Enter and Tab
Select Case KeyCode
Case 9
ActiveCell.Offset(0, 1).Activate
Case 13
ActiveCell.Offset(1, 0).Activate
Case Else
'do nothing
End Select
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
On Error GoTo errHandler
If Target.Count > 1 Then GoTo exitHandler
Set cboTemp = ws.OLEObjects("TempCombo")
On Error Resume Next
If cboTemp.Visible = True Then
With cboTemp
.Top = 10
.Left = 10
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
End If
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
Application.EnableEvents = False
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
With cboTemp
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 15
.Height = Target.Height + 5
.ListFillRange = ws.Range(str).Address
.LinkedCell = Target.Address
End With
cboTemp.Activate
End If
exitHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
errHandler:
Resume exitHandler
End Sub