Page 2 of 2

Re: อยากใช้vba กำหนดค่าxและy ในscatter chartครับ

Posted: Sun Oct 20, 2013 12:03 am
by tuidento
Private Sub Chart_select(ByVal ElementID As Long, ByVal Arg1 As Long, ByVal Arg2 As Long)
Select Case ElementID
Case xlSeries
If Arg2 > 0 Then
x = ActiveChart.SeriesCollection(Arg1).XValues
y = ActiveChart.SeriesCollection(Arg1).Values

xi = x(Arg2)
yi = y(Arg2)
With Worksheets("Sheet2")
If .Range("c1") = "" Then
.Range("c1") = xi
.Range("d1") = yi
Else
.Range("c3").End(xlUp).Offset(1) = xi
.Range("d3").End(xlUp).Offset(1) = yi
End If
If .Range("c3") > 0 Then
MsgBox "stop"
End If

End With
End If
End Select

End Sub

ต้องคลิ๊กที่จุดserieครับ (ใส) แล้ว จุดที่ 1-3จะขึ้นมาตามลำดับในการคลิ๊กแต่ละครั้งที่จุดseries(ใส) แต่ปัญหาคือครั้งต่อไป จุดที่2จะเป็นตัวเปลี่ยนอย่างเดียว ผมอยากให้มันหยุดนะครับ

Re: อยากใช้vba กำหนดค่าxและy ในscatter chartครับ

Posted: Sun Oct 20, 2013 8:45 am
by snasui
:D การวาง Code VBA ควรให้แสดงเป็น Code เพื่อสะดวกในการอ่านและการ Copy ไปทดสอบ ดูวิธีการได้จาก Link นี้ครับ http://www.snasui.com/viewtopic.php?f=3&t=1187

สำหรับที่ถามมานั้นเป็นการบันทึกค่าจากการคลิกไปไว้ที่ Sheet2 และเมื่อบันทึกครบ 3 รอบแล้วต้องการให้หยุดบันทึก สามารถใช้ If เข้าไปดักก่อนที่จะบันทึกค่าได้ตามด้านล่าง คือเมื่อพบว่า C3 ใน Sheet2 มีค่ามากกว่า 0 แล้วให้แสดงข้อความ "Stop" แล้วออกจาก Procedure

Code: Select all

Private Sub Chart_select(ByVal ElementID As Long, ByVal Arg1 As Long, ByVal Arg2 As Long)
    Select Case ElementID
    Case xlSeries
        If Arg2 > 0 Then
            x = ActiveChart.SeriesCollection(Arg1).XValues
            y = ActiveChart.SeriesCollection(Arg1).Values
                     xi = x(Arg2)
                     yi = y(Arg2)
            With Worksheets("Sheet2")
                If .Range("c3") > 0 Then
                    MsgBox "Stop"
                    Exit Sub
                End If
                If .Range("c1") = "" Then
                    .Range("c1") = Round(xi, 2)
                    .Range("d1") = Round(yi, 2)
                Else
                    .Range("c3").End(xlUp).Offset(1, 0) = Round(xi, 2)
                    .Range("d3").End(xlUp).Offset(1, 0) = Round(yi, 2)
                End If
            End With
       End If
    End Select
End Sub

Re: อยากใช้vba กำหนดค่าxและy ในscatter chartครับ

Posted: Sun Oct 20, 2013 11:13 am
by tuidento
ขอบคุณครับ