:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser
🪷 คำแสดงเจตนา
ขอผลแห่งการให้ความรู้นี้ จงกลับไปยังผู้ที่เป็นเจ้าของเดิม แม้ข้าพเจ้าจะไม่รู้จักท่านก็ตาม ขอให้แสงแห่งปัญญาที่ท่านเคยจุดไว้ ได้กลับไปเติมเต็มชีวิตของท่านอีกครั้ง และขอให้เจตนาของข้าพเจ้าเป็นการคืนความดีอย่างสงบ

สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่

Post a reply


This question is a means of preventing automated form submissions by spambots.
Smilies
:D :thup: :cp: :flw: :rz: :sg: :tt: :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :ard: :arl: :aru: :| :mrgreen: :geek: :ugeek:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#18

by tigerwit » Wed Aug 24, 2022 8:08 pm

ขอบคุณครับผม
ปรับนิดหน่อยใช้งานได้แล้วครับผม

Code: Select all

Sub ClsOverScore2()
    Dim lastRow As Long
    Dim i As Long, r As Range, j As Long
    
    With Worksheets("ไทย")
        Set r = .Range("D6")
        Do While r.Offset(i, 0).Value <> ""
            i = i + 1
            j = r.Offset(i, 0).Row
        Loop
        lastRow = .Range("D" & .Rows.Count).End(xlUp).Row
        .Range("E" & j, .Range("R" & lastRow)).ClearContents
        .Range("T" & j, .Range("U" & lastRow)).ClearContents
        .Range("Y" & j, .Range("AL" & lastRow)).ClearContents
        .Range("AN" & j, .Range("AO" & lastRow)).ClearContents
End With
End Sub

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#17

by snasui » Wed Aug 24, 2022 7:44 pm

:D ตัวอย่างการปรับ Code ครับ

Code: Select all

Sub ClsOverScore2()
    Dim lastRow As Long
    Dim i As Long, r As Range, j As Long
    
    With Worksheets("ä·Â")
        Set r = .Range("D6")
        Do While r.Offset(i, 0).Value <> ""
            i = i + 1
            j = r.Offset(i, 0).Row
        Loop
        lastRow = .Range("e" & .Rows.Count).End(xlUp).Row
        .Range("d" & j, .Range("r" & lastRow)).ClearContents
        .Range("y" & j, .Range("al" & lastRow)).ClearContents
        .Range("an" & j, .Range("ao" & lastRow)).ClearContents
    End With
End Sub

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#16

by tigerwit » Wed Aug 24, 2022 6:16 pm

ขอบคุณครับผม
เรียนสอบถามต่อครับ
จากไฟลที่แนบมา
Code ด้านล่างใช้ลบคะแนนส่วนเกินมาในชีท ไทย
แต่มีปัญหา ไม่ทำงาน

Code: Select all

Sub ClsOverScore2()
    Dim lastRow As Long
    Dim i As Long, r As Range, j As Long
    
      With Worksheets("ไทย")
        Set r = .Range("D6")
        Do While r.Offset(i, 0).Value <> ""
            i = i + 1
        Loop
    .Range(r.Offset(i, 1), .Range("R" & .Rows.Count).End(xlUp)).ClearContents
        End With
        
      With Worksheets("ไทย")
        Set r = .Range("S6")
        Do While r.Offset(i, 0).Value <> ""
            i = i + 1
        Loop
        .Range(r.Offset(i, 1), .Range("U" & .Rows.Count).End(xlUp)).ClearContents
        End With
        
          With Worksheets("ไทย")
        Set r = .Range("X6")
        Do While r.Offset(i, 0).Value <> ""
            i = i + 1
        Loop
                        .Range(r.Offset(i, 1), .Range("AL" & .Rows.Count).End(xlUp)).ClearContents
        End With
        
        With Worksheets("ไทย")
        Set r = .Range("AM6")
        Do While r.Offset(i, 0).Value <> ""
            i = i + 1
        Loop
                        .Range(r.Offset(i, 1), .Range("AO" & .Rows.Count).End(xlUp)).ClearContents
        End With
        
End Sub
แต่โค้ด ลักษณะเดียวกันนี้

Code: Select all

Sub ClsOverScore()
    Dim lastRow As Long
    Dim i As Long, r As Range, j As Long
    
    With Worksheets("EvaAttib")
        Set r = .Range("D6")
        Do While r.Offset(i, 0).Value <> ""
            i = i + 1
        Loop
    .Range(r.Offset(i, 1), .Range("H" & .Rows.Count).End(xlUp)).ClearContents
        End With
    With Worksheets("EvaAttib")
        Set r = .Range("I6")
        Do While r.Offset(i, 0).Value <> ""
            i = i + 1
        Loop
                        .Range(r.Offset(i, 1), .Range("M" & .Rows.Count).End(xlUp)).ClearContents
        End With
            With Worksheets("EvaAttib")
        Set r = .Range("N6")
        Do While r.Offset(i, 0).Value <> ""
            i = i + 1
        Loop
                        .Range(r.Offset(i, 1), .Range("O" & .Rows.Count).End(xlUp)).ClearContents
        End With
        
End Sub
ใช้งานได้กับชีท EvaAttib
ไม่ทราบว่าผิดตรงไหนครับ
Attachments
ลบส่วนเกิน2.xlsb
(84.09 KiB) Downloaded 2 times

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#15

by snasui » Wed Aug 24, 2022 5:25 pm

:D ปรับตรง For Each sh In Worksheets เป็น For Each sh In Worksheets(array("Sheet1","Sheet2","Sheet3")) เช่นนี้เป็นต้นครับ

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#14

by tigerwit » Wed Aug 24, 2022 4:24 pm

สูตรนี้

Code: Select all

Dim sh As Worksheet, r As Range, l As Integer
For Each sh In Worksheets
    If sh.Range("b1").MergeCells Then
        With sh
            l = Application.CountIfs(.Range("d6:d100"), "?*")
            For Each r In .Range("d" & l + 6, "as51")
                If Not r.Locked Then
                    r.ClearContents
                End If
            Next r
        End With
    End If
Next sh
เราสามารถใส่ชื่อชีทลงไปได้เลยหรือไม่ครับ (มี 12 ชีท)
เพื่อลดการการค้นหาชีทที่มีอยู่ในไฟล์ทั้งหมด (40 กว่าชีท) ที่เข้าเงื่อนไข (If sh.Range("b1").MergeCells Then)

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#13

by Bo_ry » Thu Apr 07, 2022 9:26 pm

Code: Select all

Sub cls()
Dim sh As Worksheet, l&
l = Application.Max(Sheets("Student").[B:B])
For Each sh In ThisWorkbook.Sheets
    If sh.[b6] = 1 Then
        sh.Unprotect
        sh.Range(Replace("F#:S58,U#:V58,Z#:AM58,AO#:AP58", "#", l + 6)).ClearContents
        sh.Protect
    End If
Next
End Sub

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#12

by snasui » Thu Apr 07, 2022 8:13 pm

:D ตัวอย่างการปรับ Code ครับ

Code: Select all

Dim sh As Worksheet, r As Range, l As Integer
For Each sh In Worksheets
    If sh.Range("b1").MergeCells Then
        With sh
            l = Application.CountIfs(.Range("d6:d100"), "?*")
            For Each r In .Range("d" & l + 6, "as51")
                If Not r.Locked Then
                    r.ClearContents
                End If
            Next r
        End With
    End If
Next sh

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#11

by tigerwit » Thu Apr 07, 2022 1:36 pm

เงื่อนไขไม่ต้องเพิ่มทีละนิดทีละหน่อย
เพิ่มมาทีเดียวให้ครบเลยนะครับ จะได้ตอบทีเดียว
ติดตรงนี้สุดท้ายแล้วครับ
ขอบคุณครับผม

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#10

by Bo_ry » Thu Apr 07, 2022 1:32 pm

เงื่อนไขไม่ต้องเพิ่มทีละนิดทีละหน่อย
เพิ่มมาทีเดียวให้ครบเลยนะครับ จะได้ตอบทีเดียว

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#9

by tigerwit » Thu Apr 07, 2022 10:13 am

Code: Select all

Sub cls()
Dim sh, l&
For Each sh In Array("Thai", "Eng", "Pysical", "Sci", "Social", "Techno")
    With Sheets(sh)
        l = .[D6].End(xlDown).Row + 1
        .Range(Replace("F#:S999,U#:V999,Z#:AM999,AO#:AP999", "#", l)).ClearContents
    End With
Next
End Sub
ขอบคุณครับ...
เหลือติดอีกนิดหนึ่งครับ
กรณี เซลว่าง ว่างจริงๆ ไม่มีสูตรเชื่อมโยงข้อมูลมาจากชีทอื่น Code ใช้ได้ผล
แต่ ถ้าเซลว่าง เนื่องจากชีทต้นทางที่เชื่อมโยงมาว่าง กรณีนี้ Code ใช้ไม่ได้ผลครับ
ตามไฟล์ที่แนบมา
Attachments
ClsScore3.xlsb
(122.73 KiB) Downloaded 4 times

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#8

by Bo_ry » Thu Apr 07, 2022 12:42 am

Code: Select all

Sub cls()
Dim sh, l&
For Each sh In Array("Thai", "Eng", "Pysical", "Sci", "Social", "Techno")
    With Sheets(sh)
        l = .[D6].End(xlDown).Row + 1
        .Range(Replace("F#:S999,U#:V999,Z#:AM999,AO#:AP999", "#", l)).ClearContents
    End With
Next
End Sub

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#7

by tigerwit » Thu Apr 07, 2022 12:15 am

Code: Select all

Sub cls()
Dim sh As Worksheet, l&
For Each sh In ThisWorkbook.Sheets
   l = sh.[D6].End(xlDown).Row + 1
    sh.Range(Replace("F#:S50,U#:V50,Z#:AM50,AO#:AP50", "#", l)).ClearContents
Next
End Sub
ขอบคุณครับ
code สั้นมาก ยังมีปัญหาอยู่ครับ
แต่เนื่องจากว่า
1. ในไฟล์ยังมีชีทอื่นอีกจำนวนมาก ที่โครงสร้างชีทไม่เหมือนตัวอย่าง เมื่อรันโค๊ดแล้ว ทำให้ไปลบข้อมูลในชีทอื่นๆ ไปด้วย
2. กรณีนักเรียนครบแถวสุดท้ายที่ 45 คน รันโค๊ดแล้ว จะไปลบคะแนนคนที่ 45 ออก ซึ่งไม่ต้องการแบบนั้น

ต้องการใช้กับชีทที่มีโครงสร้างแบบตัวอย่าง (รายวิชาต่างๆ) เหมือนกัน มีทั้งหมด 15 ชีทเท่านั้นครับ
Attachments
TestClsScore02.xlsb
(104.93 KiB) Downloaded 4 times

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#6

by Bo_ry » Wed Apr 06, 2022 10:30 pm

Code: Select all

Sub cls()
Dim sh As Worksheet, l&
For Each sh In ThisWorkbook.Sheets
   l = sh.[D6].End(xlDown).Row + 1
    sh.Range(Replace("F#:S50,U#:V50,Z#:AM50,AO#:AP50", "#", l)).ClearContents
Next
End Sub
Attachments
TestClsScore.xlsb
(66.32 KiB) Downloaded 3 times

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#5

by tigerwit » Wed Apr 06, 2022 9:30 pm

สอบถามต่อครับ กรณีที่เราต้องการจะลบข้อมูลกับชีทอื่น ๆ ที่มีโครงสร้างแบบเดียวกัน ประมาณ 10-15 ชีท
Code ที่เขียนจะต้อง With Worksheets .................................End With ไปจนครบทุกชีทใช่หรือไม่ครับ
หรือสามารถย่อให้สั้นได้กว่านี้

Code: Select all

Sub clsScore()
    Dim lastRow As Long
    Dim i As Long, r As Range
    With Worksheets("thai")
        lastRow = .Cells(.Rows.Count, 44).End(xlUp).Row
    lastRow = .Cells(.Rows.Count, 17).End(xlUp).Row
    For i = 6 To lastRow
    If .Cells(i, 2) = "" Then
     .Cells(i, 6).Resize(, 14).ClearContents
     .Cells(i, 21).Resize(, 2).ClearContents
   .Cells(i, 26).Resize(, 14).ClearContents
      .Cells(i, 41).Resize(, 2).ClearContents
     End If
    Next i
    End With
    With Worksheets("Eng")
    lastRow = .Cells(.Rows.Count, 44).End(xlUp).Row
    lastRow = .Cells(.Rows.Count, 17).End(xlUp).Row
    For i = 6 To lastRow
    If .Cells(i, 2) = "" Then
     .Cells(i, 6).Resize(, 14).ClearContents
     .Cells(i, 21).Resize(, 2).ClearContents
   .Cells(i, 26).Resize(, 14).ClearContents
      .Cells(i, 41).Resize(, 2).ClearContents
     End If
    Next i
    End With
    With Worksheets("pysical")
'    With Worksheets.Sheet3
    lastRow = .Cells(.Rows.Count, 44).End(xlUp).Row
    lastRow = .Cells(.Rows.Count, 17).End(xlUp).Row
    For i = 6 To lastRow
    If .Cells(i, 2) = "" Then
     .Cells(i, 6).Resize(, 14).ClearContents
     .Cells(i, 21).Resize(, 2).ClearContents
   .Cells(i, 26).Resize(, 14).ClearContents
      .Cells(i, 41).Resize(, 2).ClearContents
     End If
    Next i
    End With
      With Worksheets("sci")
'    With Worksheets.Sheet3
    lastRow = .Cells(.Rows.Count, 44).End(xlUp).Row
    lastRow = .Cells(.Rows.Count, 17).End(xlUp).Row
    For i = 6 To lastRow
    If .Cells(i, 2) = "" Then
     .Cells(i, 6).Resize(, 14).ClearContents
     .Cells(i, 21).Resize(, 2).ClearContents
   .Cells(i, 26).Resize(, 14).ClearContents
      .Cells(i, 41).Resize(, 2).ClearContents
     End If
    Next i
    End With
End Sub

Attachments
TestClsScore.xlsb
(64.61 KiB) Downloaded 3 times

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#4

by puriwutpokin » Mon Apr 04, 2022 9:44 pm

tigerwit wrote: Mon Apr 04, 2022 9:27 pm ขอบคุณครับมีข้อสงสัยครับ
บรรทัดนี้
lastRow = .Cells(.Rows.Count, 17).End(xlUp).Row
เลข 17 หมายถึงอะไรครับ
เลขคอลัมน์​ครับที่หาค่าสุดท้าย​ครับ

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#3

by tigerwit » Mon Apr 04, 2022 9:27 pm

ขอบคุณครับมีข้อสงสัยครับ
บรรทัดนี้
lastRow = .Cells(.Rows.Count, 17).End(xlUp).Row
เลข 17 หมายถึงอะไรครับ

Re: สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#2

by puriwutpokin » Mon Apr 04, 2022 8:22 pm

ลองปรับเป็น

Code: Select all

Sub clsScore()
    Dim lastRow As Long
    Dim i As Long, r As Range
    With Worksheets("Page1")
    lastRow = .Cells(.Rows.Count, 17).End(xlUp).Row
    For i = 2 To lastRow
    If .Cells(i, 1) = "" Then
     .Cells(i, 2).Resize(, 15).ClearContents
     End If
    Next i
    End With
End Sub

สอบถามปัญหา CodeVB ลบข้อมูลใน Cell ที่ไม่ต้องการ

#1

by tigerwit » Mon Apr 04, 2022 2:13 pm

จากไฟล์ที่แนบ
ต้องการลบข้อมูลที่เป็นคะแนนตั้งแต่ คลอลัมน์ C ถึง P โดยมีเงื่อนไขว่า
ถ้าคลอลัมน์ A แถวไหนไม่มีข้อมูล ให้ลบข้อมูลที่มีในคลอลัมน์ C ถึง P

Code: Select all

Sub clsScore()
    Dim lastRow As Long
    Dim i As Long, r As Range
    With Worksheets("Page1")
    Range("A2").Select
        Set r = .Range("A2")
        Do While r.Offset(i, 0).Value <> ""
            i = i + 1
        Loop
          lastRow = r.Offset(i - 1, 0).Row
        If Selection.Row <= lastRow Then
'        Range(lastRow).Select
         Range("C9:P12").Select
'         Selection.ClearContents
        End If
        End With
End Sub
จะต้องเขียน Code เพิ่มอย่างไรครับ
Attachments
testClsScore.xlsb
(15.56 KiB) Downloaded 8 times

Top