2017-06-08 2 views
0

ich diesen Code ausführen und Excel-Sitzung zerstört oder in der Nähe nicht. Ich weiß, dass ich nicht leaves.add oder ws.delete verwende, weil es mit Bestätigung sagt und ich nicht weiß, wie man die einzelnen Linien benutzt, die meine Gegenstände erklären. Ich kenne die Syntax nicht, denke ich. Hier ist der Code. Kann jemand darauf hinweisen, wie man es repariert?Excel wird nicht nach dem Verfahren schließen ausgeführt

Public Function ComboLists() 
Dim xlApp As Excel.Application 
Dim wb As Excel.Workbook 
Dim ws As Excel.Worksheet 
Dim MyFileName As String 
Dim bfile As String 
Dim MyList(1) As String 
Dim lRow As Long 

bfile = "S:\_Reports\KSMS\Designated Letter\KSMS Designated Letter - " 

MyFileName = bfile & Format(Date, "mm-dd-yyyy") & ".xls" 

On Error Resume Next 
Set xlApp = CreateObject("Excel.Application") 
On Error GoTo 0 

Set wb = xlApp.Workbooks.Open(MyFileName) 
Set ws = wb.Sheets(1) 
ws.Activate 
xlApp.DisplayAlerts = False 

MyList(0) = "Approve Location" 
MyList(1) = "Delete Location" 

lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row 

i = 2 

For Each c In wb.Sheets(1).Range("M" & lRow) 

If ws.Cells(i, 12).Value = "US" Then 
rng = "M" & i '& ":" & "Z" & i 

With Range(rng).Validation 
.Delete 
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _ 
    Operator:=xlBetween, Formula1:=Join(MyList, ",") 
wb.CheckCompatibility = False 
wb.Save 
wb.CheckCompatibility = True 
End With 
Else 
rng = "A" & i & ":" & "L" & i 
With xlApp.Range(rng).Validation 
ws.Delete 
wb.CheckCompatibility = False 
wb.Save 
wb.CheckCompatibility = True 
'wb.Close savechanges:=False 
End With 
End If 


i = i + 1 

Next c 

Set ws = wb.Sheets(1) 
ws.Activate 

ws.Cells.Rows("1:1").Select 

wb.CheckCompatibility = False 
wb.Save 
wb.CheckCompatibility = True 
wb.Close savechanges:=False 

xlApp.Quit 
xlApp.Quit 
xlApp.Quit 
xlApp.Quit 

Set xlApp = Nothing 
Set wb = Nothing 
Set ws = Nothing 

Exit Function 

End Function 

Ich würde jede Hilfe zu diesem Thema schätzen.

+1

Wohin läuft dieser Code? Wenn es in Excel ausgeführt wird, sollte die Variable 'xlApp' nicht benötigt werden. –

+0

Ich glaube, das Problem liegt darin, dass die Arbeitsmappe, die den Code ausführt, geschlossen ist, so dass das Makro nicht mehr funktioniert. – dwirony

+0

Es öffnet eine Arbeitsmappe und fügt den Zellen in einer Schleife Drop-Down-Validierung hinzu. – Atlas80808

Antwort

1

ändern

lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row 

zu

lRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row 

sonst werden Sie noch einen Verweis auf die Excel-Anwendung, nachdem der Code beendet.

Mit dem unqualifizierten Rows muss Ihre Anwendung (Word? Access? PowerPoint?) Ein Dummy ActiveSheet Objekt erstellen, das von der Methode verwendet werden soll. Bis das Dummy-Objekt zerstört ist, muss eine Instanz von Excel gepflegt werden. Dieses Objekt wird erst zerstört, wenn Sie Ihre Anwendung verlassen. Daher bleibt die Instanz von Excel hängen, bis Sie Ihre App beenden.


Ich habe nicht es ursprünglich vor Ort, aber Sie haben auch eine unqualifizierte Range

With Range(rng).Validation 

wird

With ws.Range(rng).Validation 

Die goldene Regel der Verwendung mehrerer Anwendungs ​​sein müssen Objekte ist immer voll qualifizieren Sie alles.

-1
Public Function ComboLists() 
Dim xlApp As Excel.Application 
Dim wb As Excel.Workbook 
Dim ws As Excel.Worksheet 
Dim MyFileName As String 
Dim bfile As String 
Dim MyList(1) As String 
Dim lRow As Long 

bfile = "S:\_Reports\KSMS\Designated Letter\KSMS Designated Letter - " 

MyFileName = bfile & Format(Date, "mm-dd-yyyy") & ".xls" 

On Error Resume Next 
Set xlApp = CreateObject("Excel.Application") 
On Error GoTo 0 

Set wb = xlApp.Workbooks.Open(MyFileName) 
Set ws = wb.Sheets(1) 
ws.Activate 
xlApp.DisplayAlerts = False 

MyList(0) = "Approve Location" 
MyList(1) = "Delete Location" 

lRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row 

MsgBox lRow 

i = 2 

'For Each c In wb.Sheets(1).Range("M2:M1000") '" & Range("V" & Rows.count).End(xlUp).Row) 
For Each c In wb.Sheets(1).Range("M" & lRow) 

If ws.Cells(i, 12).Value = "US" Then 
rng = "M" & i '& ":" & "Z" & i 

With xlApp.Range(rng).Validation 
.Delete 
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _ 
    Operator:=xlBetween, Formula1:=Join(MyList, ",") 
wb.CheckCompatibility = False 
wb.Save 
wb.CheckCompatibility = True 
DoEvents 
End With 
Else 
rng = "A" & i & ":" & "L" & i 
With xlApp.Range(rng).Validation 
ws.Delete 
wb.CheckCompatibility = False 
wb.Save 
wb.CheckCompatibility = True 
DoEvents 
End With 
DoEvents 
End If 

i = i + 1 

Next c 

DoEvents 

Set ws = wb.Sheets(1) 
ws.Activate 

ws.Cells.Rows("1:1").Select 

wb.CheckCompatibility = False 
wb.Save 
wb.CheckCompatibility = True 
wb.Close savechanges:=False 
DoEvents 
MsgBox "quit" 
xlApp.Quit 

Set xlApp = Nothing 
Set wb = Nothing 
Set ws = Nothing 

Exit Function 

End Function 

I die lRow hinzufügen lRow = ws.Cells (ws.Rows.Count, 1) .End (xlUp) .Row I hinzugefügt ws. vor Zellen und ws vor Reihen und das half, die Sitzung zu zerstören

+0

Es ist eine gute Idee, um Ihre 'Range' Objekte auch richtig zu qualifizieren -' xlApp.Range' ist äquivalent zu 'xlApp.ActiveWorkbook.ActiveSheet.Range'. Es wäre besser, 'ws.Range' zu ​​verwenden, das genau angibt, auf welchem ​​Blatt in welcher Arbeitsmappe in welcher Excel-Instanz Sie sich beziehen. – YowE3K

Verwandte Themen