Page 2 of 2
Re: การใช้ Data Validation กับ combo box(Active X)
Posted: Mon Nov 21, 2016 3:44 pm
by sakajohn
snasui wrote:
กรณี B2 หรือ B4 สามารถใช้ Or เข้ามาช่วยได้ครับ การอ้างอิงตามลักษณะที่ถามมานั้นเหมือนกันครับ
If Target.Address <> [b2].Address or Target.Address <> [b4].Address Then
Application.EnableEvents = True
Exit Sub
มันไม่ขึ้นเลยครับ
ผมลองเขียน If Target.Address = ("b2") Then
Application.EnableEvents = True
Exit Sub
กลายเป็นว่า ทุกช่องขึ้น combo boxหมด ยกเว้น cell B2 ครับ หรือผมต้องใช้ <> ไม่เท่ากับ เหมือนกันครับ
Re: การใช้ Data Validation กับ combo box(Active X)
Posted: Mon Nov 21, 2016 4:06 pm
by DhitiBank
ลองปรับจาก or เป็น and ดูครับ
If Target.Address <> [b2].Address And Target.Address <> [b4].Address Then
Re: การใช้ Data Validation กับ combo box(Active X)
Posted: Mon Nov 21, 2016 5:07 pm
by sakajohn
ผมแนบไฟล์มาให้ดูเลยครับ ไฟล์นี้เป็นไฟล์ต้นฉบับครับ ที่Sheet 1 จะให้ combo box ขึ้นที่ cell C7 และะ C12 ตาม code ที่แนะนำมา ผมก็ใช้ and ปรากฏว่าไม่ได้ครับ มันจะโชว์แต่ที่ Cell C12 แต่นำcodeนี้ไปลองกลับไฟล์อื่น ทำได้ครับ งงมาก
Code: Select all
If Target.Address <> [C7].Address And Target.Address <> [C12].Address Then
Application.EnableEvents = True
Exit Sub
ต้องปรับยังไงดีครับ
Re: การใช้ Data Validation กับ combo box(Active X)
Posted: Mon Nov 21, 2016 7:13 pm
by snasui
DhitiBank wrote:2016-11-21 15_33_40-Microsoft Visual Basic for Applications - combo box.xlsm [break] - [Sheet1 (Code.png
อาจารย์ครับ range.Validation.Type เป็นการเช็คอะไรครับ ทำไมผมลองคีย์ใน immediate window เพื่อดูค่ากลับแสดงค่าผิดพลาดว่า "Application-defined of Object-defined error" ครับ

เดิมที Code นั้นใช้ร่วมกับ Validation ก็เลยเป็นการตรวจสอบว่าเป็น Validation Type ใด เช่น ตัวเลข วันที่ สูตร เป็นต้น ดูเพิ่มเติมที่นี่ครับ
Validation.Type
สามารถทดสอบโดยการสร้าง Data Validation ไว้ที่เซลล์ใด ๆ แล้วใช้ Immediate Windows เช็คเพื่อดูผลลัพธ์ครับ
Re: การใช้ Data Validation กับ combo box(Active X)
Posted: Mon Nov 21, 2016 8:43 pm
by snasui

ลองปรับ Code เป็นด้านล่างครับ
Code: Select all
Private Sub Worksheet_Selectionchange(ByVal Target As Range)
Dim xStr As String
Dim xCombox As OLEObject
Application.EnableEvents = False
Set xCombox = ActiveSheet.OLEObjects("TempCombo")
With xCombox
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Top = Target.Top
End With
If InStr("$C$7$C$12", Target.Address) = 0 Then
Application.EnableEvents = True
Exit Sub
End If
xStr = "Sheet1!J2:J51"
With xCombox
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = xStr
.LinkedCell = Target.Address
End With
xCombox.Activate
Me.TempCombo.DropDown
Application.EnableEvents = True
End Sub
Re: การใช้ Data Validation กับ combo box(Active X)
Posted: Mon Nov 21, 2016 9:40 pm
by sakajohn
อาจารย์ครับผมลองปรับ code เพื่อให้เลือกค่าในแต่ละตารางครับ โดย Cell C7 เลือกค่าใน Cell H2:H3 , Cell C12 เลือกค่าใน Cell J2:J51 และ Cell C17 เลือกค่าใน G2:G10
Code: Select all
"Other code"
If InStr("$C$7$C$12$C$17", Target.Address) = 0 Then
Application.EnableEvents = True
Exit Sub
End If
xStr = "Sheet1!J2:J51"
With xCombox
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = xStr
.LinkedCell = Target.Address
End With
xCombox.Activate
Me.TempCombo.DropDown
Application.EnableEvents = True
If InStr("$C$7", Target.Address) = 0 Then
Application.EnableEvents = True
Exit Sub
End If
xStr = "Sheet1!H2:H3"
With xCombox
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = xStr
.LinkedCell = Target.Address
End With
xCombox.Activate
Me.TempCombo.DropDown
Application.EnableEvents = True
If InStr("$C$17", Target.Address) = 0 Then
Application.EnableEvents = True
Exit Sub
End If
xStr = "Sheet1!G2:G10"
With xCombox
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = xStr
.LinkedCell = Target.Address
End With
xCombox.Activate
Me.TempCombo.DropDown
Application.EnableEvents = True
End Sub
Cell C7 กับ Cell C12 ไม่มีปัญหาได้ตามที่ต้องการ แต่ Cell C17 ไม่ได้ตามต้องการครับ
Re: การใช้ Data Validation กับ combo box(Active X)
Posted: Mon Nov 21, 2016 10:15 pm
by snasui

พยายามทำความเข้าใจตัวแปร การเขียน Code ลักษณะนั้นให้เลือกใช้ตัวแปรแทนที่จะเขียนเข้าไปทุกเงื่อนไข
จาก Statement นี้
xStr = "Sheet1!J2:J51"
ให้นำการรตัดสินใจเข้ามาช่วย ยกตัวอย่างเช่นด้านล่างครับ
Code: Select all
If target.address = "$C$7" then
xStr = "Sheet1!H2:H13"
elseif target.address = "$C$12" then
xStr = "Sheet1!J2:J51"
elseif target.address = "$C$17" then
xStr = "Sheet1!G2:G10"
end if
Re: การใช้ Data Validation กับ combo box(Active X)
Posted: Mon Nov 21, 2016 10:58 pm
by sakajohn
ได้แล้วครับอาจารย์ขอบคุณมากครับ ตรง $C$12" กับ $C$17" ต้องเพิ่ม " เข้าไปข้างหน้าด้วยใช่ไหมครับ
Code: Select all
If target.address = "$C$7" then
xStr = "Sheet1!H2:H13"
elseif target.address = "$C$12" then
xStr = "Sheet1!J2:J51"
elseif target.address = "$C$17" then
xStr = "Sheet1!G2:G10"
end if
Re: การใช้ Data Validation กับ combo box(Active X)
Posted: Mon Nov 21, 2016 11:14 pm
by sakajohn
อาจารย์ครับ พอเราเลือก Share workbook มันขึ้น error "ไม่สามารถใช้ TOP ของคลาส OLEObject ได้" พอClick End ที่ทำมาก็ใช้ไม่ได้เลยครับ
Re: การใช้ Data Validation กับ combo box(Active X)
Posted: Mon Nov 21, 2016 11:42 pm
by snasui

ไฟล์ที่มี VBA มันแชร์ไม่ได้ VBA จะไม่สามารถทำงานได้บนไฟล์แชร์ครับ
Re: การใช้ Data Validation กับ combo box(Active X)
Posted: Mon Nov 21, 2016 11:49 pm
by snasui
sakajohn wrote:ตรง $C$12" กับ $C$17" ต้องเพิ่ม " เข้าไปข้างหน้าด้วยใช่ไหมครับ

ใช่ครับ ผมคีย์ตกไปครับ
Re: การใช้ Data Validation กับ combo box(Active X)
Posted: Wed Nov 23, 2016 1:42 pm
by DhitiBank
snasui wrote:DhitiBank wrote:2016-11-21 15_33_40-Microsoft Visual Basic for Applications - combo box.xlsm [break] - [Sheet1 (Code.png
อาจารย์ครับ range.Validation.Type เป็นการเช็คอะไรครับ ทำไมผมลองคีย์ใน immediate window เพื่อดูค่ากลับแสดงค่าผิดพลาดว่า "Application-defined of Object-defined error" ครับ

เดิมที Code นั้นใช้ร่วมกับ Validation ก็เลยเป็นการตรวจสอบว่าเป็น Validation Type ใด เช่น ตัวเลข วันที่ สูตร เป็นต้น ดูเพิ่มเติมที่นี่ครับ
Validation.Type
สามารถทดสอบโดยการสร้าง Data Validation ไว้ที่เซลล์ใด ๆ แล้วใช้ Immediate Windows เช็คเพื่อดูผลลัพธ์ครับ
ขอบคุณอาจารย์มากครับ
