Page 1 of 1

การลบบรรทัดแบบมีข้อแม้

Posted: Wed Mar 11, 2015 1:52 am
by yodpao.b
การลบบรรทัดแบบมีข้อแม้

ผมใช้มาโครลบบรรทัด โดยต้องการใช้ 3 ข้อแม้

ข้อแม้แรก ข้อมูลมีหลายบรรทัด ลบข้อมูลทั้งหมด ผลที่ได้ ถูกต้อง
Capture1.PNG
ข้อแม้ที่ 2 ข้อมูลไม่มี ห้ามลบ ผลที่ได้ ถูกต้อง
Capture2.PNG
ข้อแม้ที่ 3 ข้อมูลมีบรรทัดเดียว ผลที่ได้ ไม่ถูกต้องเพราะมันลบข้อมูลในส่วนอื่นไปด้วย
ที่ถูกต้องคือลบบรรทัดเดียว
Capture3.PNG
code ที่ใช้อยู่

Code: Select all

    If Range("B30") <> "" Then
        Range("B30:S30").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Delete Shift:=xlUp
    End If

Re: การลบบรรทัดแบบมีข้อแม้

Posted: Wed Mar 11, 2015 1:53 am
by yodpao.b
ไฟล์แนบครับ

Re: การลบบรรทัดแบบมีข้อแม้

Posted: Wed Mar 11, 2015 7:34 pm
by snasui
:D ตัวอย่างการปรับ Code ครับ

Code: Select all

If Range("B31") <> "" Then
    Range("B30:S30").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
Else
    Range("B30:S30").ClearContents
End If

Re: การลบบรรทัดแบบมีข้อแม้

Posted: Fri Mar 13, 2015 4:54 pm
by yodpao.b
ผมแก้ code เป็น

Code: Select all

Sheets("Cool").Select
If Range("B30") <> "" And Range("B31") <> "" Then
    Range("B30:S30").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
End If
If Range("B30") <> "" And Range("B31") = "" Then
    Range("B30:S30").Select
    Selection.Delete Shift:=xlUp
End If
ใช้ได้แล้วครับ