Page 1 of 1

การแก้ไข code VBA

Posted: Wed Oct 17, 2012 12:00 pm
by tutape
พอดีผมลงบันทึกแมโคร

Code: Select all

Sub Macro1()
    Range("B3").Select
    ActiveCell.FormulaR1C1 ="สสสสส"
    Sheets("Sheet2").Select
    Range("B4:E5").Select
    Selection.ClearContents
End Sub
ที่ต้องการคือ ที่เซลล์ B3 ไม่ว่าจะคีย์ข้ดความใดๆ ในเครื่องหมาย " " จะปรับอย่างไรครับ ถึงจะทำให้ ข้อมูลใน sheet2 ช่วง B4 : B5 ลบออกทุกครั้งที่ข้อมูล B3 มีการเปลี่ยนแปลง และนำไปใช้อย่างไร จะต้องสร้างปุ่มขึ้นมาหรือเปล่าครับ

Re: การแก้ไข code VBA

Posted: Wed Oct 17, 2012 2:18 pm
by snasui
:D ในหน้าต่าง VBE ดับเบิ้ลคลิกที่ Sheet2 แล้วนำ Code ด้านล่างไปวางครับ

Code: Select all

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$3" Then
        Range("B4:E5").ClearContents
    End If
End Sub

Re: การแก้ไข code VBA

Posted: Wed Oct 17, 2012 2:32 pm
by tutape
ได้แล้วครับ แต่ผิดกับที่ต้องการนิดนึง เพราะผมถามผิดประเด็นเอง คือ B3 ที่ว่าอยู่ใน sheet1 ครับ ผมจะลองประยุกต์ก่อนถ้าไม่ได้ยังไงค่อยถามต่อครับ ขอบคุณครับ

Re: การแก้ไข code VBA

Posted: Wed Oct 17, 2012 3:11 pm
by tutape
ผมลองปรับโค้ดแล้วครับ ดังนี้

Code: Select all

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$3" Then
        Sheets("Sheet2").Select
        Selects("B4:E5").ClearContents
    End If
End Sub
แต่ปรับยังไงก็ไม่ได้ สงสัยประยุกต์ผิดที่ครับ ขอคำชี้แนะจาก อ. ด้วยครับ

Re: การแก้ไข code VBA

Posted: Wed Oct 17, 2012 3:14 pm
by snasui
tutape wrote:เพราะผมถามผิดประเด็นเอง คือ B3 ที่ว่าอยู่ใน sheet1
:lol: ให้นำ Code ไปวางใน Sheet1 แล้วปรับเป็นด้านล่างครับ

Code: Select all

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$3" Then
        Sheets("Sheet2").Range("B4:E5").ClearContents
    End If
End Sub

Re: การแก้ไข code VBA

Posted: Wed Oct 17, 2012 3:44 pm
by tutape
ขอคุณครับ

Re: การแก้ไข code VBA

Posted: Sat Oct 20, 2012 10:16 pm
by tutape
snasui wrote:
tutape wrote:เพราะผมถามผิดประเด็นเอง คือ B3 ที่ว่าอยู่ใน sheet1
:lol: ให้นำ Code ไปวางใน Sheet1 แล้วปรับเป็นด้านล่างครับ

Code: Select all

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$3" Then
        Sheets("Sheet2").Range("B4:E5").ClearContents
    End If
End Sub
ผมขอถามต่ออีกนิดครับ ว่าถ้าหากต้องการกำหนดว่าเมื่อเซลล์ใดเซลล์หนึ่งเปลี่ยนแปลงนอกจากเซลล์ B3 จะเขียนเซลล์อื่นๆ ต่อจาก B3 อย่างไร ผมลองใส่แบบทั่วไปใน excel ก็ไม่ได้ครับ ผมลองใส่เป็น If Target.Address = "$B$3,$E$5$,$F$2" Then รบกวนช่วยแนะนำด้วยครับ

Re: การแก้ไข code VBA

Posted: Sun Oct 21, 2012 5:43 pm
by snasui
:D สามารถใฃ้ Or เข้ามาช่วยครับ เช่น

Code: Select all

If Target.Address = "$B$3" Or Target.Address = "$E$5" Or Target.Address = "$F$2" Then

Re: การแก้ไข code VBA

Posted: Tue Mar 12, 2013 12:28 am
by tutape
ครับ ผมเข้าใจแล้วครับ ขอบคุณมากครับ