Page 1 of 1
สอบถามเกี่ยวกับ Code VBA Drop down list (indirect(Substitute))
Posted: Sat Jun 24, 2017 5:26 pm
by jaturon
พอดีผมทำ Drop Down list ขึ้นมา 2 ชั้นครับ =indirect(A1) โดยสูตรนี้ใช้กับ Drop Down list ครับ แต่รายการเลือกของ Drop Down list มันเล็กเกินไปไม่สามารถปรับขนาดตัวอักษรได้ ผมเลยเอา Combo Box เข้ามาช่วยครับ โดยใช้ Code VBA ให้ Combobox ทับ Drop Down list ทุกอันและใช้ข้อมูลจาก Drop Down list ครับ (ซึ่ง Code VBA นี้ผมก็ไปเอามาจากเว็ปเว็ปหนึ่งครับ ลองไปมั่วๆก็ใช้ได้เลยลองใช้ดู)
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
'====================================
ปัญหา คือ
แต่เมื่อผมอยากให้ Drop Down list สามารถเว้นวรรคได้ เลยใช้ Substitute เข้ามาช่วย เมื่อมีการเว้นวรรคอยู่ด้วย =indirect(substitute(A1," ","_")) แต่ Code VBA ต้องมีการเปลี่ยนแปลงในเรื่องของ INDIRECT ที่ใช้ ผมลองเขียน VBA แบบเท่าที่เขียนได้ก็ไม่สามารถใช้ได้เลยครับ เลยต้องถามผู้รู้ให้ผู้รู้ช่วยทีครับ
Re: สอบถามเกี่ยวกับ Code VBA Drop down list (indirect(Substitute))
Posted: Sat Jun 24, 2017 5:32 pm
by snasui

กรุณาโพสต์ Code ให้แสดงเป็น Code ตามกฎการใช้บอร์ดข้อ 5 ด้านบนครับ

Re: สอบถามเกี่ยวกับ Code VBA Drop down list (indirect(Substitute))
Posted: Sat Jun 24, 2017 6:19 pm
by jaturon
snasui wrote:
กรุณาโพสต์ Code ให้แสดงเป็น Code ตามกฎการใช้บอร์ดข้อ 5 ด้านบนครับ

ขอบคุณมากๆนะครับ ที่ให้คำแนะนำ
Re: สอบถามเกี่ยวกับ Code VBA Drop down list (indirect(Substitute))
Posted: Sat Jun 24, 2017 8:01 pm
by snasui

ตัวอย่าง 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
Re: สอบถามเกี่ยวกับ Code VBA Drop down list (indirect(Substitute))
Posted: Sun Jun 25, 2017 1:47 am
by jaturon
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
ผมสามารถใช้ได้แล้วครับ ขอบคุณมากๆเลยนะครับ ขอบคุณจริงๆ ผมดีใจสุดๆไปเลยครับ น้ำตาแทบไหล หาวิธีมาตั้งนานครับ ขอบคุณอีกครั้งนะครับ
Re: สอบถามเกี่ยวกับ Code VBA Drop down list (indirect(Substitute))
Posted: Sun Jun 25, 2017 4:18 pm
by jaturon
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 ไปวางช่องอื่นก็ไม่สามารถใช้ได้เลยครับ ขอวิธีแนะนำหน่อยครับว่าผมต้องทำยังไง TT
Re: สอบถามเกี่ยวกับ Code VBA Drop down list (indirect(Substitute))
Posted: Sun Jun 25, 2017 4:38 pm
by snasui

Code นั้นสามารถใช้ได้กับทุกเซลล์ที่มีสภาพเหมือนกับตัวอย่างที่ทำมา
กรณีนำไปใช้กับเซลล์อื่นให้แนบไฟล์พร้อม Code ที่ปรับปรุงแล้วมาใหม่ ชี้ให้เห็นว่าต้องการใช้กับเซลล์ใด การใช้สีตัวอักษร ให้เน้นสีเท่าที่จำเป็น อย่าใช้สีเดียวกันหมดทุกข้อความจะสะดวกในการอ่านมากกว่าครับ
Re: สอบถามเกี่ยวกับ Code VBA Drop down list (indirect(Substitute))
Posted: Sun Jun 25, 2017 5:22 pm
by jaturon
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
'====================================
Re: สอบถามเกี่ยวกับ Code VBA Drop down list (indirect(Substitute))
Posted: Sun Jun 25, 2017 5:33 pm
by jaturon
snasui wrote:
Code นั้นสามารถใช้ได้กับทุกเซลล์ที่มีสภาพเหมือนกับตัวอย่างที่ทำมา
กรณีนำไปใช้กับเซลล์อื่นให้แนบไฟล์พร้อม Code ที่ปรับปรุงแล้วมาใหม่ ชี้ให้เห็นว่าต้องการใช้กับเซลล์ใด การใช้สีตัวอักษร ให้เน้นสีเท่าที่จำเป็น อย่าใช้สีเดียวกันหมดทุกข้อความจะสะดวกในการอ่านมากกว่าครับ
ขอโทษนะครับ ตอนนี้ผมรู้เหตุผลแล้วว่าทำไมถึงใช้ Combo Box ที่ซ้อนทับ Drop Down list ไม่ได้ เนื่องจากผมต้องมีหัวข้อที่จะเลือกอยู่ด้านซ้ายมือก่อนหน้า Cell ที่ต้องการจะทำการ Drop down list ถ้าผมไม่ต้องการ Show ให้เห็นก็แค่ hide Column นั้น
แต่ถ้าอยากให้สมบูรณ์จริงๆเลย โดยที่ผมไม่ต้องใส่หัวข้อที่จะเลือกเลยผมจะต้องเพิ่ม Code ยังไงครับ ขอบคุณมากๆนะครับ
Re: สอบถามเกี่ยวกับ Code VBA Drop down list (indirect(Substitute))
Posted: Sun Jun 25, 2017 5:45 pm
by snasui

Code นี้จะต้องมีเซลล์หลักในการใช้ตัดสินใจเพื่อที่จะแสดง Drop down หากว่าใช้งานได้ก็ไม่จำเป็นต้องหาวิธีอื่น
การปรับ Code ในทุกความต้องการจำเป็นต้องเขียนมาเองก่อนเสมอ ติดปัญหาแล้วค่อยมาถามกันครับ
Re: สอบถามเกี่ยวกับ Code VBA Drop down list (indirect(Substitute))
Posted: Sun Jun 25, 2017 5:57 pm
by jaturon
snasui wrote:
Code นี้จะต้องมีเซลล์หลักในการใช้ตัดสินใจเพื่อที่จะแสดง Drop down หากว่าใช้งานได้ก็ไม่จำเป็นต้องหาวิธีอื่น
การปรับ Code ในทุกความต้องการจำเป็นต้องเขียนมาเองก่อนเสมอ ติดปัญหาแล้วค่อยมาถามกันครับ
ขอบคุณมากๆนะครับ สำหรับการช่วยเหลือในการพัฒนา Code VBA แค่นี้ก็ถือว่าช่วยผมได้เยอะมากๆ เลยครับ แต่ผมเขียน Code ไม่เป็นเลยครับ ก็แค่ปรับ Code เท่าที่มีให้ใช้งานได้ครับ แต่ถ้าเป็นไปได้ก็อยากให้สามารถใช้ได้แบบสมบูรณ์เลยครับ เพราะผมต้องใช้ Drop Down List ทั้ง Sheet เลยครับ ขอบคุณอีกครั้งนะครับ
Re: สอบถามเกี่ยวกับ Code VBA Drop down list (indirect(Substitute))
Posted: Sun Jun 25, 2017 6:16 pm
by snasui

การช่วยปรับ Code จะเป็นไปได้ก็ต่อเมื่อมีการปรับมาเองก่อนเท่านั้น เป็นกฎข้อ 5 ของฟอรั่มที่ผมเขียนขึ้นมาเอง จะละเมิดเสียเองไม่ได้ครับ
