snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
อาจารย์ครับ รบกวนอีกครั้งครับ ผมได้นำ Code Delete sheet ที่อาจารย์ แก้ไข ให้ทดสอบใช้แล้วครับ
ถ้าไฟล์ Test 1 , Test 2 และ Test 3 มีชื่อ Sheet Name = Sheet 1 ก็จะไม่ถูกลบ ซึ่ง Code ของอาจารย์ใช้งานได้ดีเลยครับ
แต่ผมมีความอยากรู้เพิ่มเติมครับ คือ ถ้าไฟล์ Test 1, Test 2 และ Test 3 มีชื่อ sheet Name ที่ไม่ต้องการลบออก ไม่เหมือนกัน
เช่น Test 1 >> Sheet Name = AA
Test 2 >> Sheet Name = BB
Test 3 >> Sheet Name = CC
ความต้องการ คือ เลือกทั้ง 3 ไฟล์พร้อมกัน และให้ลบ Sheet อื่นที่ไม่เกี่ยวข้องออก จากไฟล์ โดยแต่ละไฟล์จะต้องเหลือ
Test 1 >> เหลือ Sheet Name = AA
Test 2 >> เหลือ Sheet Name = BB
Test 3 >> เหลือ Sheet Name = CC
Sub RoundedRectangle2_Click()
'Sub DeleteSheet()
Dim i As Integer, strThisbook As Variant
strThisbook = Application.GetOpenFilename(Filefilter:= _
"All File (*.*), *.*", Title:="Please select source file(s).", MultiSelect:=True)
If TypeName(strThisbook) = "Boolean" Then
'MsgBox "Please select file(s)."
Exit Sub
End If
Application.DisplayAlerts = False
For i = 1 To UBound(strThisbook)
Set thisBook = Workbooks.Open(strThisbook(i))
Application.ScreenUpdating = False
For Each sh In thisBook.Worksheets
If sh.Name <> "AA" Then
sh.Delete
Else
If sh.Name <> "BB" Then
sh.Delete
Else
If sh.Name <> "CC" Then
sh.Delete
End If
Next sh
thisBook.Close True
Next i
Application.DisplayAlerts = True
End Sub
Sub RoundedRectangle2_Click()
'Sub DeleteSheet()
Dim i As Integer, strThisbook As Variant
strThisbook = Application.GetOpenFilename(Filefilter:= _
"All File (*.*), *.*", Title:="Please select source file(s).", MultiSelect:=True)
If TypeName(strThisbook) = "Boolean" Then
'MsgBox "Please select file(s)."
Exit Sub
End If
Application.DisplayAlerts = False
For i = 1 To UBound(strThisbook)
Set thisBook = Workbooks.Open(strThisbook(i))
Application.ScreenUpdating = False
For Each sh In thisBook.Worksheets
If sh.Name <> "AA" Or "BB" Or "CC" Then
sh.Delete
End If
Next sh
thisBook.Close True
Next i
Application.DisplayAlerts = True
End Sub
'--Other code
For Each sh In thisBook.Worksheets
if thisBook.Name = "Test 1.XLSX" Then
If sh.Name <> "AA" Then
sh.Delete
End If
Elseif thisBook.Name = "Test 2.XLSX" Then
If sh.Name <> "BB" Then
sh.Delete
Else
ElstIf thisBook.Name = "Test 3.XLSX" Then
If sh.Name <> "CC" Then
sh.Delete
End If
End If
Next sh
'--Other code
Sub RoundedRectangle2_Click()
'Sub DeleteSheet()
Dim i As Integer, strThisbook As Variant
Dim sh As Worksheet, thisBook As Workbook
Dim ob As Workbook
strThisbook = Application.GetOpenFilename(Filefilter:= _
"All File (*.*), *.*", Title:="Please select source file(s).", MultiSelect:=True)
If TypeName(strThisbook) = "Boolean" Then
'MsgBox "Please select file(s)."
Exit Sub
End If
Application.DisplayAlerts = False
For i = 1 To UBound(strThisbook)
Set thisBook = Workbooks.Open(strThisbook(i))
Application.ScreenUpdating = False
For Each sh In thisBook.Worksheets
If thisBook.Name = "All file ,*.xlsx" Then
If sh.Name <> "AA" Then
sh.Delete
End If
ElseIf thisBook.Name = "All file ,*.xlsx" Then
If sh.Name <> "BB" Then
sh.Delete
End If
ElseIf thisBook.Name = "All file ,*.xlsx" Then
If sh.Name <> "CC" Then
sh.Delete
End If
End If
Next sh
thisBook.Close True
Next i
Application.DisplayAlerts = True
End Sub
You do not have the required permissions to view the files attached to this post.