2011-07-27 23 views
10

Excel VBA kullanan bir Excel belgesinde belirli sayfaların var olup olmadığını nasıl kontrol edeceğini bilen var mı? alırsınız böyleExcel-VBA'da belirli sayfaların var olup olmadığını nasıl kontrol edebilirim?

+0

olası yinelenen http://stackoverflow.com/questions/6040164/excel- vba-if-worksheetwsname-var) –

+2

Olası kopya [Test veya sayfanın var olup olmadığını kontrol edin] (https://stackoverflow.com/questions/6688131/test-or-check-if-sheet-exists) – puzzlepiece87

cevap

13

, biz .. Bunu denetlemek için kendi fonksiyonunu oluşturabilir

Aşağıdaki kod ihtiyaçlarınıza uygun Umut.

Edit1: Eklendi da ... deyim

Sub test() 

    If CheckSheet(Sheets(3).Name) then 

     Application.DisplayAlerts = False 
     Sheets(Sheets(3).Name).Delete 
     Application.DisplayAlerts = True 

    End If 

End Sub 

kodu kullanmayı sakıncası yoksa ben Alternatif için ...

Function CheckSheet(ByVal sSheetName As String) As Boolean 

    Dim oSheet As Excel.Worksheet 
    Dim bReturn As Boolean 

    For Each oSheet In ActiveWorkbook.Sheets 

     If oSheet.Name = sSheetName Then 

      bReturn = True 
      Exit For 

     End If 

    Next oSheet 

    CheckSheet = bReturn 

End Function 

giderdik çözüm silmek Bu aktif hataları (ortak kodlama en iyi uygulamalarla tavsiye edilmez) yükseltmek için aşağıdaki 'Spartan Programming wannabe' kodunu kullanabilirsiniz ...

Function CheckSheet(ByVal sSheetName As String) As Boolean 

    Dim oSheet As Excel.Worksheet 
    Dim bReturn As Boolean 

    For Each oSheet In ActiveWorkbook.Sheets 

     If oSheet.Name = sSheetName Then 

      bReturn = True 
      Exit For 

     End If 

    Next oSheet 

    CheckSheet = bReturn 

End Function 


Function CheckSheet(ByVal sSheetName As String) As Boolean 

    On Error Resume Next 
    Dim oSheet As Excel.Worksheet 

    Set oSheet = ActiveWorkbook.Sheets(sSheetName) 
    CheckSheet = IIf(oSheet Is Nothing, False, True) 

End Function 
+0

Bu ayrıca bir mükemmel çözüm - hatalara dayanmadan kontrol eder. Daha iyi bir yol olduğunu biliyordum ve işte bu yolu daha önce kullanmıştım, nasıl yapılacağını hatırlayamadım. @Tiago: Bunu Sheets (1) üzerinde nasıl kullanırdınız? Sadece Sheets (1) .Name geçmek? –

+0

@PaulR, yeap, tam olarak ... 'Sheets (1) .name' hile yapardı. Kullanacağınız çalışma kitaplarını aktif olarak tanımlamanız gerektiğine dikkat edin, çünkü bir 'sayfa' referansı, etkin çalışma kitabına (bazen beklenen değil) işaret eder. –

+0

Doğru, bazen iş yerinde ısırmıştım. Daha büyük işlemler için, Çalışma Sayfalarım ("SheetName") nesnesinde bir With bloğunu kullanma eğilimim var ve bunu iyi olarak adlandırıyorum. Elbette, çalışma sayfalarının ("SheetName") var olduğundan emin olduktan sonra bu olurdu. –

3

şey başlatan:

On Error Resume Next 

Dim wSheet as Worksheet 
Set wSheet = Sheets(1) ' can also be a string, such as Sheets("Sheet1") 

If wSheet Is Nothing Then 
    MsgBox "Worksheet not found!" 
    Set wSheet = Nothing ' make the worksheet point to nothing. 
    On Error GoTo 0 
Else 
    MsgBox "Worksheet found!" 
    Set wSheet = Nothing ' set the found Worksheet object to nothing. You can use the found wSheet for your purposes, though. 
End If 

Bu kod http://www.ozgrid.com/VBA/IsWorkbookOpen.htm dayanıyordu. DoesSheetExist() altını arayın.

Bu yardımcı olur umarız! (Maalesef) böyle bir yöntem mevcut olmamasına rağmen

0

Bu kodu, aşağıda gösterildiği gibi IBM Notes (eski adıyla Lotus Notes) tarafından kullanılan dillerden biri olan LotusScript'te kullanılmak üzere uyarladım.

Public Function ExcelSheetExists(_ 
    xlBook As Variant, _ ' Excel workbook object 
    ByVal strSheetName As String _ 
    ) As Boolean 

    On Error GoTo errHandler 

    ForAll xlSheet In xlBook.Sheets 
     If xlSheet.Name = strSheetName Then 
      ExcelSheetExists = True 
      Exit Forall 
     End If 
    End ForAll 

    GoTo Done 

errHandler: 
    ' Call MyCustomErrorHandler() 
    Resume Done 
Done: 

End Function 
0
On Error GoTo Line1 
If Sheets("BOX2").Index > 0 Then 
Else 
Line1: MsgBox ("BOX2 is missing") 
end if 

ben bu şekilde :) yaptılar

[Excel VBA Eğer çalışma sayfası ("wsName") Var] (içinde
İlgili konular