EXCEL TOOLS
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
[code]
และปิดด้วย [/code]
ตัวอย่างเช่น [code]dim r as range[/code]
เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)Code: Select all
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, _
Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim WS As Worksheet
Set WS = ActiveSheet
Set cboTemp = WS.OLEObjects("TempCombo")
On Error Resume Next
With cboTemp
'clear and hide the combo box
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
'if the cell contains
'a data validation list
Cancel = True
Application.EnableEvents = False
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
'for simple INDIRECT function (English)
' e.g. =INDIRECT
'will create dependent list of items
If Left(str, 8) = "INDIRECT)" Then
lSplit = InStr(1, str, "(")
str = Right(str, Len(str) - lSplit)
str = Left(str, Len(str) - 1)
str = Range(str).Value
End If
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = str
.LinkedCell = Target.Address
End With
cboTemp.Activate
'open the drop down list automatically
Me.tempcombo.DropDown
End If
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub
'=========================================
Private Sub TempCombo_LostFocus()
With Me.tempcombo
.Top = 10
.Left = 10
.Width = 0
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
End Sub
'====================================
ขอบคุณมากๆนะครับ ที่ให้คำแนะนำsnasui wrote: กรุณาโพสต์ Code ให้แสดงเป็น Code ตามกฎการใช้บอร์ดข้อ 5 ด้านบนครับ
Code: Select all
'...other code
Cancel = True
Application.EnableEvents = False
If InStr(Target.Offset(0, -1).Value, " ") Then
str = Replace(Target.Offset(0, -1).Value, " ", "_")
Else
str = Target.Offset(0, -1).Value
End If
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = str
.LinkedCell = Target.Address
End With
cboTemp.Activate
'open the drop down list automatically
Me.tempcombo.DropDown
End If
'...other code
snasui wrote: ตัวอย่าง Code ครับ
Code: Select all
'...other code Cancel = True Application.EnableEvents = False If InStr(Target.Offset(0, -1).Value, " ") Then str = Replace(Target.Offset(0, -1).Value, " ", "_") Else str = Target.Offset(0, -1).Value End If With cboTemp 'show the combobox with the list .Visible = True .Left = Target.Left .Top = Target.Top .Width = Target.Width + 5 .Height = Target.Height + 5 .ListFillRange = str .LinkedCell = Target.Address End With cboTemp.Activate 'open the drop down list automatically Me.tempcombo.DropDown End If '...other code
ขอโทษนะครับ Code นี้ผมลองใช้แล้วครับ แต่มันสามารถใช้ได้แค่เฉพาะช่อง B11 ช่องเดียวเองครับ ผมลอง Copy ไปวางช่องอื่นก็ไม่สามารถใช้ได้เลยครับ ขอวิธีแนะนำหน่อยครับว่าผมต้องทำยังไง TTsnasui wrote: ตัวอย่าง Code ครับ
Code: Select all
'...other code Cancel = True Application.EnableEvents = False If InStr(Target.Offset(0, -1).Value, " ") Then str = Replace(Target.Offset(0, -1).Value, " ", "_") Else str = Target.Offset(0, -1).Value End If With cboTemp 'show the combobox with the list .Visible = True .Left = Target.Left .Top = Target.Top .Width = Target.Width + 5 .Height = Target.Height + 5 .ListFillRange = str .LinkedCell = Target.Address End With cboTemp.Activate 'open the drop down list automatically Me.tempcombo.DropDown End If '...other code
ขอโทษนะครับ ที่ใช้สีตัวอักษรเปลืองเกินไปsnasui wrote: Code นั้นสามารถใช้ได้กับทุกเซลล์ที่มีสภาพเหมือนกับตัวอย่างที่ทำมา
กรณีนำไปใช้กับเซลล์อื่นให้แนบไฟล์พร้อม Code ที่ปรับปรุงแล้วมาใหม่ ชี้ให้เห็นว่าต้องการใช้กับเซลล์ใด การใช้สีตัวอักษร ให้เน้นสีเท่าที่จำเป็น อย่าใช้สีเดียวกันหมดทุกข้อความจะสะดวกในการอ่านมากกว่าครับ
Code: Select all
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, _
Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim WS As Worksheet
Set WS = ActiveSheet
Set cboTemp = WS.OLEObjects("TempCombo")
On Error Resume Next
With cboTemp
'clear and hide the combo box
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
'if the cell contains
'a data validation list
'...other code
Cancel = True
Application.EnableEvents = False
If InStr(Target.Offset(0, -1).Value, " ") Then
str = Replace(Target.Offset(0, -1).Value, " ", "_")
Else
str = Target.Offset(0, -1).Value
End If
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = str
.LinkedCell = Target.Address
End With
cboTemp.Activate
'open the drop down list automatically
Me.tempcombo.DropDown
End If
'...other code
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub
'=========================================
Private Sub TempCombo_LostFocus()
With Me.tempcombo
.Top = 10
.Left = 10
.Width = 0
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
End Sub
'====================================
ขอโทษนะครับ ตอนนี้ผมรู้เหตุผลแล้วว่าทำไมถึงใช้ Combo Box ที่ซ้อนทับ Drop Down list ไม่ได้ เนื่องจากผมต้องมีหัวข้อที่จะเลือกอยู่ด้านซ้ายมือก่อนหน้า Cell ที่ต้องการจะทำการ Drop down list ถ้าผมไม่ต้องการ Show ให้เห็นก็แค่ hide Column นั้น แต่ถ้าอยากให้สมบูรณ์จริงๆเลย โดยที่ผมไม่ต้องใส่หัวข้อที่จะเลือกเลยผมจะต้องเพิ่ม Code ยังไงครับ ขอบคุณมากๆนะครับsnasui wrote: Code นั้นสามารถใช้ได้กับทุกเซลล์ที่มีสภาพเหมือนกับตัวอย่างที่ทำมา
กรณีนำไปใช้กับเซลล์อื่นให้แนบไฟล์พร้อม Code ที่ปรับปรุงแล้วมาใหม่ ชี้ให้เห็นว่าต้องการใช้กับเซลล์ใด การใช้สีตัวอักษร ให้เน้นสีเท่าที่จำเป็น อย่าใช้สีเดียวกันหมดทุกข้อความจะสะดวกในการอ่านมากกว่าครับ
ขอบคุณมากๆนะครับ สำหรับการช่วยเหลือในการพัฒนา Code VBA แค่นี้ก็ถือว่าช่วยผมได้เยอะมากๆ เลยครับ แต่ผมเขียน Code ไม่เป็นเลยครับ ก็แค่ปรับ Code เท่าที่มีให้ใช้งานได้ครับ แต่ถ้าเป็นไปได้ก็อยากให้สามารถใช้ได้แบบสมบูรณ์เลยครับ เพราะผมต้องใช้ Drop Down List ทั้ง Sheet เลยครับ ขอบคุณอีกครั้งนะครับsnasui wrote: Code นี้จะต้องมีเซลล์หลักในการใช้ตัดสินใจเพื่อที่จะแสดง Drop down หากว่าใช้งานได้ก็ไม่จำเป็นต้องหาวิธีอื่น
การปรับ Code ในทุกความต้องการจำเป็นต้องเขียนมาเองก่อนเสมอ ติดปัญหาแล้วค่อยมาถามกันครับ