Hello! I am trying to delete a worksheet from an *.xls file and then create a different one with the same name. The thing is that excel hangs up during the for sequence. If I mark the For...Next sequence with ' then everything is ok. What is wrong? Please help me. I am desperate. Please check the code below:
VB Code:
Dim objXLSheet As Object Dim ExcelApp As excel.Application Dim ExcelBook As excel.Workbook Dim ExcelSheet As excel.Worksheets Dim g_ExcelPath As String g_ExcelPath = "C:\listamatforan\1_08A.xls" strSheetName = "EXTENDED_PART_LIST" Set ExcelApp = CreateObject("Excel.Application") With ExcelApp '.ExcelBook .Workbooks.Open (g_ExcelPath) ' Set ExcelSheet = ExcelApp.Worksheets.Add ' Set ExcelSheet = ExcelApp.Sheets(1) 'With .ExcelSheet If .Worksheets.Count = 1 Then .Worksheets.Add.Name = "DELETE" .Workbooks.Application.SaveWorkspace End If If .Worksheets.Count > 1 Then For buc_sheets = 1 To .Worksheets.Count den_sheet = Worksheets(buc_sheets).Name If strSheetName = den_sheet Then .Worksheets(strSheetName).Activate .Worksheets(strSheetName).Delete End If Next Workbooks.Close '(g_ExcelPath) 'Set .Workbooks.Application.ActiveSheet = Nothing End If 'End With End With ExcelApp.Application.Quit Set ExcelApp = Nothing




Reply With Quote