Page 1 of 1

ต้องการให้ข้อมูลในกราฟเปลี่ยนตามข้อมูลที่กรอก

Posted: Tue Sep 01, 2020 9:04 am
by Chayanit.c
ต้องการให้ข้อมูลในกราฟเปลี่ยนตามข้อมูลที่กรอก

Code: Select all

 
Dim PlanChart As String
Dim Titulo As String
Dim AreaSheett As String
Dim AreaSheettt As String
PlanChart = Sheet2.Name

Dim Linha As Double
Linha = 4
With Planilhal
     Do
     Linha = Linha + 1
     Loop Until .Cells(Linha, 2).Value = ""
End With
Linha = Linha - 1

Dim Sheett As Chart
Set Sheett = Sheets(PlanChart).ChartObjects("Chart 1").Chart


        If ComboBox1.Value = "2561" Then
            Titulo = Sheet1.Range("A2").Value
             AreaSheettt = " = " & Sheet1.Name & " ! " & Sheet1.Range("A3:A & Linha").Address
             AreaSheett = " = " & Sheet1.Name & " ! " & Sheet1.Range("B3:B & Linha ").Address
           

       With Sheett
     .HasTitle = True
     .ChartTitle.Text = Titulo
     .SeriesCollection(1).XValues = AreaSheettt
     .SeriesCollection(1).Values = AreaSheett
      End With

End If

 If ComboBox1.Value = "2562" Then
            Titulo = Sheet1.Range("D2").Value
             AreaSheettt = " = " & Sheet1.Name & " ! " & Sheet1.Range("D3:D & Linha ").Address
             AreaSheett = " = " & Sheet1.Name & " ! " & Sheet1.Range("E3:E & Linha ").Address
           

       With Sheett
     .HasTitle = True
     .ChartTitle.Text = Titulo
     .SeriesCollection(1).XValues = AreaSheettt
     .SeriesCollection(1).Values = AreaSheett
      End With

End If
If ComboBox1.Value = "2563" Then
            Titulo = Sheet1.Range("G2").Value
             AreaSheettt = " = " & Sheet1.Name & " ! " & Sheet1.Range("G3:G & Linha ").Address
             AreaSheett = " = " & Sheet1.Name & " ! " & Sheet1.Range("H3:H& Linha ").Address
           

       With Sheett
     .HasTitle = True
     .ChartTitle.Text = Titulo
     .SeriesCollection(1).XValues = AreaSheettt
     .SeriesCollection(1).Values = AreaSheett
      End With

End If

Call Carreger_Chart

Exit Sub
Erro:
MsgBox " Erro! ", vbCritical, " ERRO "
End Sub



Private Sub UserForm_Initialize()
Call Carreger_Chart

ComboBox1.AddItem " 2561 "
ComboBox1.AddItem " 2562 "
ComboBox1.AddItem " 2563 "

End Sub

มันติดเหลือง Loop Until .Cells(Linha, 2).Value = "" บรรทัดนี้ และขึ้น Run time error 424 : Object Required

Re: ต้องการให้ข้อมูลในกราฟเปลี่ยนตามข้อมูลที่กรอก

Posted: Tue Sep 01, 2020 10:27 am
by snasui
:D With Statement ใน Procedure Private Sub ComboBox1_Change() เขียนไม่ถูกต้องครับ

การอ้างอิงจะต้องบอกโปรแกรมทราบว่าเป็นชีตไหน เช่น

Code: Select all

With Worksheets(PlanChart)
     Do
     Linha = Linha + 1
     Loop Until .Cells(Linha, 2).Value = ""
End With
ใน Procedure Private Sub UserForm_Initialize() Assign ค่าให้กับ Item ของ ComboBox ไม่ตรงกับที่จะใช้จริง การเขียนเป็น ComboBox1.AddItem " 2561 " คือให้มีวรรคครอบ เมื่อตอนตรวจสอบการเปลี่ยนแปลงค่าก็ต้องมีวรรคครอบด้วย เช่น If ComboBox1.Value = " 2561 " Then อย่างนี้ถึงจะตรวจสอบกันได้ครับ

Re: ต้องการให้ข้อมูลในกราฟเปลี่ยนตามข้อมูลที่กรอก

Posted: Tue Sep 01, 2020 11:27 am
by Chayanit.c
snasui wrote: Tue Sep 01, 2020 10:27 am :D With Statement ใน Procedure Private Sub ComboBox1_Change() เขียนไม่ถูกต้องครับ

การอ้างอิงจะต้องบอกโปรแกรมทราบว่าเป็นชีตไหน เช่น

Code: Select all

With Worksheets(PlanChart)
     Do
     Linha = Linha + 1
     Loop Until .Cells(Linha, 2).Value = ""
End With
ใน Procedure Private Sub UserForm_Initialize() Assign ค่าให้กับ Item ของ ComboBox ไม่ตรงกับที่จะใช้จริง การเขียนเป็น ComboBox1.AddItem " 2561 " คือให้มีวรรคครอบ เมื่อตอนตรวจสอบการเปลี่ยนแปลงค่าก็ต้องมีวรรคครอบด้วย เช่น If ComboBox1.Value = " 2561 " Then อย่างนี้ถึงจะตรวจสอบกันได้ครับ

ลองเปลี่ยนตามที่แนะนำแล้วค่ะ พอ run ออกมามันขึ้น Error ที่ AreaSheettt = " = " & Sheet1.Name & " ! " & Sheet1.Range("D3:D & Linha ").Address มันขึ้นว่า run time error 1004 : Method ' Rangs ' of object_'worksheet' failed

Code: Select all

If ComboBox1.Value = " 2562 " Then
            Titulo = Sheet1.Range("D2").Value
             AreaSheettt = " = " & Sheet1.Name & " ! " & Sheet1.Range("D3:D & Linha ").Address
             AreaSheett = " = " & Sheet1.Name & " ! " & Sheet1.Range("E3:E & Linha ").Address

       With Sheett
     .HasTitle = True
     .ChartTitle.Text = Titulo
     .SeriesCollection(1).XValues = AreaSheettt
     .SeriesCollection(1).Values = AreaSheett
      End With

Re: ต้องการให้ข้อมูลในกราฟเปลี่ยนตามข้อมูลที่กรอก

Posted: Tue Sep 01, 2020 6:54 pm
by snasui
:D เขียน Statement ที่ใช้เชื่อมตัวแปรไม่ถูกต้องครับ

จาก .Range("D3:D & Linha ").Address ปรับเป็น .Range("D3:D" & Linha).Address

ซึ่งจะต้องแก้ในทุก ๆ ตำแหน่งที่เขียนไว้ลักษณะนี้ครับ

Re: ต้องการให้ข้อมูลในกราฟเปลี่ยนตามข้อมูลที่กรอก

Posted: Wed Sep 02, 2020 9:40 am
by Chayanit.c
snasui wrote: Tue Sep 01, 2020 6:54 pm :D เขียน Statement ที่ใช้เชื่อมตัวแปรไม่ถูกต้องครับ

จาก .Range("D3:D & Linha ").Address ปรับเป็น .Range("D3:D" & Linha).Address

ซึ่งจะต้องแก้ในทุก ๆ ตำแหน่งที่เขียนไว้ลักษณะนี้ครับ


เปลี่ยนทุกตำแหน่งตามที่แนะนำแล้วค่ะ แต่มันยังติด error อยู่เลยค่ะ ซึ่งหนูพยายามแก้เองแล้วแต่จนปัญญา

Code: Select all

 Private Sub ComboBox1_Change() 
มันติดเหลืองตัวนี้ค่ะ แล้วขึ้นว่า compile error : sub or Function not defined

Code: Select all

Private Sub ComboBox1_Change()
'On Error GoTo Erro

Dim PlanChart As String
Dim Titulo As String
Dim AreaSheett As String
Dim AreaSheettt As String
PlanChart = Sheet2.Name


Dim Linha As Double
Linha = 4
With Worksheet(PlanChart)
     Do
     Linha = Linha + 1
     Loop Until .Cells(Linha, 2).Value = ""
End With


Dim Sheett As Chart
Set Sheett = Sheets(PlanChart).ChartObjects(" Chart 1 ").Chart


        If ComboBox1.Value = " 2561 " Then
            Titulo = Sheet1.Range("A2").Value
             AreaSheettt = " = " & Sheet1.Name & " ! " & Sheet1.Range(" A3:A " & Linha).Address
             AreaSheett = " = " & Sheet1.Name & " ! " & Sheet1.Range(" B3:B " & Linha).Address
           

       With Sheett
     .HasTitle = True
     .ChartTitle.Text = Titulo
     .SeriesCollection(1).XValues = AreaSheettt
     .SeriesCollection(1).Values = AreaSheett
      End With

End If

 If ComboBox1.Value = " 2562 " Then
            Titulo = " 2562 "
             AreaSheettt = " = " & Sheet1.Name & " ! " & Sheet1.Range(" D3:D " & Linha).Address
             AreaSheett = " = " & Sheet1.Name & " ! " & Sheet1.Range(" E3:E " & Linha).Address
           

       With Sheett
     .HasTitle = True
     .ChartTitle.Text = Titulo
     .SeriesCollection(1).XValues = AreaSheettt
     .SeriesCollection(1).Values = AreaSheett
      End With

End If
If ComboBox1.Value = " 2563 " Then
            Titulo = Sheet1.Range("G2").Value
             AreaSheettt = " = " & Sheet1.Name & " ! " & Sheet1.Range(" G3:G " & Linha).Address
             AreaSheett = " = " & Sheet1.Name & " ! " & Sheet1.Range(" H3:H " & Linha).Address
           

       With Sheett
     .HasTitle = True
     .ChartTitle.Text = Titulo
     .SeriesCollection(1).XValues = AreaSheettt
     .SeriesCollection(1).Values = AreaSheett
      End With

End If

Call Carreger_Chart

Exit Sub
Erro:
MsgBox " Erro! ", vbCritical, " ERRO "
End Sub
[/coed]

Re: ต้องการให้ข้อมูลในกราฟเปลี่ยนตามข้อมูลที่กรอก

Posted: Wed Sep 02, 2020 9:45 am
by Chayanit.c
snasui wrote: Tue Sep 01, 2020 6:54 pm :D เขียน Statement ที่ใช้เชื่อมตัวแปรไม่ถูกต้องครับ

จาก .Range("D3:D & Linha ").Address ปรับเป็น .Range("D3:D" & Linha).Address

ซึ่งจะต้องแก้ในทุก ๆ ตำแหน่งที่เขียนไว้ลักษณะนี้ครับ


เปลี่ยนทุกตำแหน่งตามที่แนะนำแล้วค่ะ แต่มันยังติด error อยู่เลยค่ะ ซึ่งหนูพยายามแก้เองแล้วแต่จนปัญญา

Code: Select all

 Private Sub ComboBox1_Change() 
มันติดเหลืองตัวนี้ค่ะ แล้วขึ้นว่า compile error : sub or Function not defined

Code: Select all

Private Sub ComboBox1_Change()
'On Error GoTo Erro

Dim PlanChart As String
Dim Titulo As String
Dim AreaSheett As String
Dim AreaSheettt As String
PlanChart = Sheet2.Name


Dim Linha As Double
Linha = 4
With Worksheet(PlanChart)
     Do
     Linha = Linha + 1
     Loop Until .Cells(Linha, 2).Value = ""
End With


Dim Sheett As Chart
Set Sheett = Sheets(PlanChart).ChartObjects(" Chart 1 ").Chart


        If ComboBox1.Value = " 2561 " Then
            Titulo = Sheet1.Range("A2").Value
             AreaSheettt = " = " & Sheet1.Name & " ! " & Sheet1.Range(" A3:A " & Linha).Address
             AreaSheett = " = " & Sheet1.Name & " ! " & Sheet1.Range(" B3:B " & Linha).Address
           

       With Sheett
     .HasTitle = True
     .ChartTitle.Text = Titulo
     .SeriesCollection(1).XValues = AreaSheettt
     .SeriesCollection(1).Values = AreaSheett
      End With

End If

 If ComboBox1.Value = " 2562 " Then
            Titulo = " 2562 "
             AreaSheettt = " = " & Sheet1.Name & " ! " & Sheet1.Range(" D3:D " & Linha).Address
             AreaSheett = " = " & Sheet1.Name & " ! " & Sheet1.Range(" E3:E " & Linha).Address
           

       With Sheett
     .HasTitle = True
     .ChartTitle.Text = Titulo
     .SeriesCollection(1).XValues = AreaSheettt
     .SeriesCollection(1).Values = AreaSheett
      End With

End If
If ComboBox1.Value = " 2563 " Then
            Titulo = Sheet1.Range("G2").Value
             AreaSheettt = " = " & Sheet1.Name & " ! " & Sheet1.Range(" G3:G " & Linha).Address
             AreaSheett = " = " & Sheet1.Name & " ! " & Sheet1.Range(" H3:H " & Linha).Address
           

       With Sheett
     .HasTitle = True
     .ChartTitle.Text = Titulo
     .SeriesCollection(1).XValues = AreaSheettt
     .SeriesCollection(1).Values = AreaSheett
      End With

End If

Call Carreger_Chart

Exit Sub
Erro:
MsgBox " Erro! ", vbCritical, " ERRO "
End Sub
[/coed]

Re: ต้องการให้ข้อมูลในกราฟเปลี่ยนตามข้อมูลที่กรอก

Posted: Wed Sep 02, 2020 8:04 pm
by snasui
:D แก้คำว่า Worksheet ให้เป็น Worksheets ครับ

นอกจากนั้น แก้พวกค่าที่ครอบด้วยเครื่องหมายฟันหนู ค่าใดก็ตามที่ไปเพิ่มเครื่องหมายวรรคเข้าไปทีหลัง ให้ลบวรรคออกทั้งหมด

โพสต์ #4 ผมหมายถึงการครอบตัวแปรด้วยฟันหนูที่เขียนมานั้นไม่ถูกต้อง Statement ใดที่ครอบตัวแปรด้วยฟันหนูให้แก้ตามที่ผมบอก ต้องเข้าใจว่าสิ่งไหนเรียกว่าตัวแปร สิ่งไหนไม่ใช่ตัวแปร ซึ่งถ้าเขียน Code ข้างต้นมาเองควรจะต้องทราบสิ่งเหล่านี้ครับ