สวัสดีครับ
ผมกำลังทำฟอร์มชุดข้อมูล ที่แปลงจาก .csv และมีปัญหาเรื่องการทำชุดข้อมูลที่ได้รับจากไฟล์ดิบที่มีปัญหาเกี่ยวกับ Row ของข้อมูลเนื่องจากใน แต่ละ Row จะมีตัวอักษรอักขระขึ้นนำ และจะต้องเป็นตัวอักษรตามตัวอย่างที่แนบมา ซึ่งมีหลายพัน Row
เบื้องต้นผมได้ลองใช้ VBA+Macro เพื่อให้มีการ Cut copy paste และ Shift row up ขึ้นมาทีละบรรทัด หากต้องการ ให้ทำได้หลายๆบรรทัดจะต้องเขียน โค้ดในลักษณะนี้ หลายพันบรรทัดมาก และ ต้อง Call function จาก sub หลายๆรอบ ช่วยแนะนำหน่อยครับ ว่าควรตั้ง Loop อย่างไร
ผมได้แนบไฟล์ที่มีการเขียน Macro ไว้ และมีปุ่ม Set กับ Restart ไว้ลองโปรแกรมครับ
Code: Select all
Sub SETN()
''Set N for 1st row------------------------------------------------------------------------------------------------
If Range("A2") = "" Then
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Else
If Left(Range("A2"), 1) <> "N" Then
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Cut
Range("A1").Select
Range("A1").End(xlToRight).Offset(0, 1).Select
ActiveSheet.Paste
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Else
''Set N for 2nd row------------------------------------------------------------------------------------------------
If Range("A3") = "" Then
Rows("3:3").Select
Selection.Delete Shift:=xlUp
Else
If Left(Range("A3"), 1) <> "N" Then
Range("A3").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Cut
'Need next cell fomular here '
Range("A2").Select
Range("A2").End(xlToRight).Offset(0, 1).Select
'Need next cell fomular here '
ActiveSheet.Paste
Rows("3:3").Select
Selection.Delete Shift:=xlUp
Else
''Set N for 3rd row------------------------------------------------------------------------------------------------
If Range("A4") = "" Then
Rows("4:4").Select
Selection.Delete Shift:=xlUp
Else
If Left(Range("A4"), 1) <> "N" Then
Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Cut
'Need next cell fomular here '
Range("A3").Select
Range("A3").End(xlToRight).Offset(0, 1).Select
'Need next cell fomular here '
ActiveSheet.Paste
Rows("4:4").Select
Selection.Delete Shift:=xlUp
Else
''Set N for 4th row------------------------------------------------------------------------------------------------
If Range("A5") = "" Then
Rows("5:5").Select
Selection.Delete Shift:=xlUp
Else
If Left(Range("A5"), 1) <> "N" Then
Range("A5").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Cut
'Need next cell fomular here '
Range("A4").Select
Range("A4").End(xlToRight).Offset(0, 1).Select
'Need next cell fomular here '
ActiveSheet.Paste
Rows("5:5").Select
Selection.Delete Shift:=xlUp
End If
End If
End If
End If
End If
End If
End If
End If
End Sub