Page 1 of 1

สอบถามปัญหา CodeVB ร่วมกับ Combobox สร้างเมนูเพื่อลิ้งค์ไปชีทอื่น

Posted: Sat Apr 02, 2022 10:50 am
by tigerwit
จากไฟล์แนบ
ผมใช้ Combobox ทำเป็นเมนู เพื่อให้เลือกรายการแล้วโดดไปยังหน้าที่เลือก
เมื่อทดสอบการใช้งานแล้วปกติ
แต่พอปิดไฟล์โดยวิธีการปิดจากกากะบาทมุมขวาสุดจะมีปัญหาแจ้ง Debug โค๊ด
แต่พอเลือกวิธีการปิดจากเมนูแฟ้ม ---> ปิด วิธีการนี้ไม่พบปัญหาใดๆ
เรียนถามว่าเกิดจากสาเหตุใด และต้องแก้ไขอย่างไรครับ

Code: Select all

Sub Menu()
Dim r As Range
Set r = Worksheets("page1").Range("A1")

If r = 1 Then
        Sheet1.Select
End If
        
If r = 2 Then
        Sheet2.Select
End If
            
If r = 3 Then
        Sheet3.Select
End If
                
If r = 4 Then
        Sheet4.Select
End If
                    
If r = 5 Then
        Sheet6.Select
End If
If r = 6 Then
        Sheet7.Select
End If

If r = 7 Then
        Sheet8.Select
        End If
        
If r = 8 Then
        Sheet9.Select
        End If
        
If r = 9 Then
        Sheet10.Select
        End If
        
If r = 10 Then
        Sheet11.Select
        End If
        
If r = 11 Then
        Sheet12.Select
        End If
        
If r = 12 Then
        Sheet13.Select
        End If
        
If r = 13 Then
        Sheet14.Select
        End If
        
If r = 14 Then
        Sheet15.Select
        End If
        
If r = 15 Then
        Sheet16.Select
End If

End Sub

Code: Select all

Private Sub ComboBox1_Change()
Call Menu
End Sub

Code: Select all

Private Sub Worksheet_Activate()
Range("A1").Select
ActiveCell.FormulaR1C1 = "1"
End Sub

Re: สอบถามปัญหา CodeVB ร่วมกับ Combobox สร้างเมนูเพื่อลิ้งค์ไปชีทอื่น

Posted: Sat Apr 02, 2022 7:57 pm
by snasui
:D เกิดปัญหาจากการ Link ComboBox ไว้กับเซลล์ ซึ่งปัจจุบันใช้ A1 ครับ

ในตอนเปิดและปิดจะมีการคำนวณใหม่ทำให้กระทบกับ Event Change ของ ComboBox ทุกตัวเป็นรายชีต ในบางจึงหวะมันไม่สามารถเข้าถึงชีตนั้น ๆ ได้ก็จะเกิด Error ครับ

Re: สอบถามปัญหา CodeVB ร่วมกับ Combobox สร้างเมนูเพื่อลิ้งค์ไปชีทอื่น

Posted: Sat Apr 02, 2022 9:19 pm
by tigerwit
ขอบคุณครับ
หมายความว่าไม่สามารถใช้ Event Change ของ ComboBox เพื่อสั่งให้ไปยังชีทอื่นด้วยวิธีการนี้ได้

Re: สอบถามปัญหา CodeVB ร่วมกับ Combobox สร้างเมนูเพื่อลิ้งค์ไปชีทอื่น

Posted: Sun Apr 03, 2022 9:12 am
by snasui
:D สามารถใช้การดัก Error เข้ามาช่วยได้ครับ

ตัวอย่าง Code

Code: Select all

Sub Menu()
    Dim r As Range
    On Error Resume Next
    Set r = Worksheets("page1").Range("A1")
    If r Is Nothing Then
        On Error GoTo 0
        Exit Sub
    End If
'Other code