如何在vba上关闭从本地vlookup引用打开的外部Excel
调用引用其他Excel的vlookup时,我试图关闭从活动Excel打开的另一个Excel工作簿。我能得到的最接近的方法是关闭工作簿,但Excel保持打开状态。我不想太激进,关闭所有Excel实例,因为它会关闭流程中未涉及的其他工作簿。名为InsertLookup的主子节点调用其他子节点。我可以关闭活动的excel,但由vlookup(OtherExcel.Xlsx)打开的excel仍保持打开状态。我不想让过程中涉及的两个excel文件中的任何一个保持打开状态如何在vba上关闭从本地vlookup引用打开的外部Excel,excel,vba,vlookup,Excel,Vba,Vlookup,调用引用其他Excel的vlookup时,我试图关闭从活动Excel打开的另一个Excel工作簿。我能得到的最接近的方法是关闭工作簿,但Excel保持打开状态。我不想太激进,关闭所有Excel实例,因为它会关闭流程中未涉及的其他工作簿。名为InsertLookup的主子节点调用其他子节点。我可以关闭活动的excel,但由vlookup(OtherExcel.Xlsx)打开的excel仍保持打开状态。我不想让过程中涉及的两个excel文件中的任何一个保持打开状态 Option Explicit
Option Explicit
Public RemoteRowCount As Long
Public LocalRowCount As Long
Function Is_WorkBook_Open(ByVal strWorkbookName As String) As Boolean
Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks(strWorkbookName)
If Err Then
Is_WorkBook_Open = False
Else
Is_WorkBook_Open = True
End If
End Function
Sub GetRemoteRowCount()
Dim LastRowRemote As Long
Dim WkBks As Workbook
Dim WShts As Worksheet
Workbooks.Open Filename:="C:\local\OtherExcel.Xlsx"
If Is_WorkBook_Open("OtherExcel.Xlsx") Then
Set WkBks = Workbooks("OtherExcel.Xlsx")
Else
Workbooks.Open Filename:="C:\test\OtherExcel.Xlsx"
End If
Set WShts = WkBks.Worksheets("FirstSheet")
LastRowRemote = WShts.Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
RemoteRowCount = LastRowRemote
End Sub
Sub GetLocalRowCount()
Dim LastRowLocal As Long
Dim WShts As Worksheet
ThisWorkbook.Activate
Set WShts = ThisWorkbook.Worksheets("OtherSheet")
LastRowLocal = WShts.Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
LocalRowCount = LastRowLocal
'MsgBox (LocalRowCount)
End Sub
Sub InsertLookup()
Call GetRemoteRowCount
Call GetLocalRowCount
Dim rng As Range, cell As Range, LastValue As String, i As Integer
For i = 3 To LocalRowCount
If IsNumeric(Cells(i, "G").Value) = True And IsEmpty(Cells(i, "G").Value) = False Then
LastValue = "=VLOOKUP(" & Cells(i, "N").Offset(, -7).Value & ",[OtherExcel.XLSX]FirstSheet!R13C1:R" & RemoteRowCount & "C12,10,0)"
Cells(i, "N").Formula = LastValue
End If
Next i
ActiveWorkbook.Save
ActiveWorkbook.Close
Call DeleteAllSubs
Call CloseExcel
End Sub
Sub DeleteAllSubs()
Dim otmp As Object
With ActiveWorkbook.VBProject
For Each otmp In .VBComponents
If otmp.Type=100 Then
otmp.CodeModule.DeleteLines 1, otmp.CodeModule.CountOfLines
otmp.CodeModule.CodePane.Window.Close
Else: .VBComponents.Remove otmp
End If
Next otmp
End With
End Sub
Option Explicit
Public RemoteRowCount As Long
Public LocalRowCount As Long
Function Is_WorkBook_Open(ByVal strWorkbookName As String) As Boolean
Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks(strWorkbookName)
If Err Then
Is_WorkBook_Open = False
Else
Is_WorkBook_Open = True
End If
End Function
Sub GetRemoteRowCount()
Dim LastRowRemote As Long
Dim WkBks As Workbook
Dim WShts As Worksheet
Workbooks.Open Filename:="C:\local\OtherExcel.Xlsx"
If Is_WorkBook_Open("OtherExcel.Xlsx") Then
Set WkBks = Workbooks("OtherExcel.Xlsx")
Else
Workbooks.Open Filename:="C:\test\OtherExcel.Xlsx"
End If
Set WShts = WkBks.Worksheets("FirstSheet")
LastRowRemote = WShts.Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
RemoteRowCount = LastRowRemote
End Sub
Sub GetLocalRowCount()
Dim LastRowLocal As Long
Dim WShts As Worksheet
ThisWorkbook.Activate
Set WShts = ThisWorkbook.Worksheets("OtherSheet")
LastRowLocal = WShts.Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
LocalRowCount = LastRowLocal
'MsgBox (LocalRowCount)
End Sub
Sub InsertLookup()
Call GetRemoteRowCount
Call GetLocalRowCount
Dim rng As Range, cell As Range, LastValue As String, i As Integer
For i = 3 To LocalRowCount
If IsNumeric(Cells(i, "G").Value) = True And IsEmpty(Cells(i, "G").Value) = False Then
LastValue = "=VLOOKUP(" & Cells(i, "N").Offset(, -7).Value & ",[OtherExcel.XLSX]FirstSheet!R13C1:R" & RemoteRowCount & "C12,10,0)"
Cells(i, "N").Formula = LastValue
End If
Next i
ActiveWorkbook.Save
ActiveWorkbook.Close
Call DeleteAllSubs
Call CloseExcel
End Sub
Sub DeleteAllSubs()
Dim otmp As Object
With ActiveWorkbook.VBProject
For Each otmp In .VBComponents
If otmp.Type=100 Then
otmp.CodeModule.DeleteLines 1, otmp.CodeModule.CountOfLines
otmp.CodeModule.CodePane.Window.Close
Else: .VBComponents.Remove otmp
End If
Next otmp
End With
End Sub
Option Explicit
Public RemoteRowCount As Long
Public LocalRowCount As Long
Function Is_WorkBook_Open(ByVal strWorkbookName As String) As Boolean
Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks(strWorkbookName)
If Err Then
Is_WorkBook_Open = False
Else
Is_WorkBook_Open = True
End If
End Function
Sub GetRemoteRowCount()
Dim LastRowRemote As Long
Dim WkBks As Workbook
Dim WShts As Worksheet
Workbooks.Open Filename:="C:\local\OtherExcel.Xlsx"
If Is_WorkBook_Open("OtherExcel.Xlsx") Then
Set WkBks = Workbooks("OtherExcel.Xlsx")
Else
Workbooks.Open Filename:="C:\test\OtherExcel.Xlsx"
End If
Set WShts = WkBks.Worksheets("FirstSheet")
LastRowRemote = WShts.Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
RemoteRowCount = LastRowRemote
End Sub
Sub GetLocalRowCount()
Dim LastRowLocal As Long
Dim WShts As Worksheet
ThisWorkbook.Activate
Set WShts = ThisWorkbook.Worksheets("OtherSheet")
LastRowLocal = WShts.Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
LocalRowCount = LastRowLocal
'MsgBox (LocalRowCount)
End Sub
Sub InsertLookup()
Call GetRemoteRowCount
Call GetLocalRowCount
Dim rng As Range, cell As Range, LastValue As String, i As Integer
For i = 3 To LocalRowCount
If IsNumeric(Cells(i, "G").Value) = True And IsEmpty(Cells(i, "G").Value) = False Then
LastValue = "=VLOOKUP(" & Cells(i, "N").Offset(, -7).Value & ",[OtherExcel.XLSX]FirstSheet!R13C1:R" & RemoteRowCount & "C12,10,0)"
Cells(i, "N").Formula = LastValue
End If
Next i
ActiveWorkbook.Save
ActiveWorkbook.Close
Call DeleteAllSubs
Call CloseExcel
End Sub
Sub DeleteAllSubs()
Dim otmp As Object
With ActiveWorkbook.VBProject
For Each otmp In .VBComponents
If otmp.Type=100 Then
otmp.CodeModule.DeleteLines 1, otmp.CodeModule.CountOfLines
otmp.CodeModule.CodePane.Window.Close
Else: .VBComponents.Remove otmp
End If
Next otmp
End With
End Sub
Option Explicit
Public RemoteRowCount As Long
Public LocalRowCount As Long
Function Is_WorkBook_Open(ByVal strWorkbookName As String) As Boolean
Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks(strWorkbookName)
If Err Then
Is_WorkBook_Open = False
Else
Is_WorkBook_Open = True
End If
End Function
Sub GetRemoteRowCount()
Dim LastRowRemote As Long
Dim WkBks As Workbook
Dim WShts As Worksheet
Workbooks.Open Filename:="C:\local\OtherExcel.Xlsx"
If Is_WorkBook_Open("OtherExcel.Xlsx") Then
Set WkBks = Workbooks("OtherExcel.Xlsx")
Else
Workbooks.Open Filename:="C:\test\OtherExcel.Xlsx"
End If
Set WShts = WkBks.Worksheets("FirstSheet")
LastRowRemote = WShts.Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
RemoteRowCount = LastRowRemote
End Sub
Sub GetLocalRowCount()
Dim LastRowLocal As Long
Dim WShts As Worksheet
ThisWorkbook.Activate
Set WShts = ThisWorkbook.Worksheets("OtherSheet")
LastRowLocal = WShts.Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
LocalRowCount = LastRowLocal
'MsgBox (LocalRowCount)
End Sub
Sub InsertLookup()
Call GetRemoteRowCount
Call GetLocalRowCount
Dim rng As Range, cell As Range, LastValue As String, i As Integer
For i = 3 To LocalRowCount
If IsNumeric(Cells(i, "G").Value) = True And IsEmpty(Cells(i, "G").Value) = False Then
LastValue = "=VLOOKUP(" & Cells(i, "N").Offset(, -7).Value & ",[OtherExcel.XLSX]FirstSheet!R13C1:R" & RemoteRowCount & "C12,10,0)"
Cells(i, "N").Formula = LastValue
End If
Next i
ActiveWorkbook.Save
ActiveWorkbook.Close
Call DeleteAllSubs
Call CloseExcel
End Sub
Sub DeleteAllSubs()
Dim otmp As Object
With ActiveWorkbook.VBProject
For Each otmp In .VBComponents
If otmp.Type=100 Then
otmp.CodeModule.DeleteLines 1, otmp.CodeModule.CountOfLines
otmp.CodeModule.CodePane.Window.Close
Else: .VBComponents.Remove otmp
End If
Next otmp
End With
End Sub
Option Explicit
Public RemoteRowCount As Long
Public LocalRowCount As Long
Function Is_WorkBook_Open(ByVal strWorkbookName As String) As Boolean
Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks(strWorkbookName)
If Err Then
Is_WorkBook_Open = False
Else
Is_WorkBook_Open = True
End If
End Function
Sub GetRemoteRowCount()
Dim LastRowRemote As Long
Dim WkBks As Workbook
Dim WShts As Worksheet
Workbooks.Open Filename:="C:\local\OtherExcel.Xlsx"
If Is_WorkBook_Open("OtherExcel.Xlsx") Then
Set WkBks = Workbooks("OtherExcel.Xlsx")
Else
Workbooks.Open Filename:="C:\test\OtherExcel.Xlsx"
End If
Set WShts = WkBks.Worksheets("FirstSheet")
LastRowRemote = WShts.Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
RemoteRowCount = LastRowRemote
End Sub
Sub GetLocalRowCount()
Dim LastRowLocal As Long
Dim WShts As Worksheet
ThisWorkbook.Activate
Set WShts = ThisWorkbook.Worksheets("OtherSheet")
LastRowLocal = WShts.Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
LocalRowCount = LastRowLocal
'MsgBox (LocalRowCount)
End Sub
Sub InsertLookup()
Call GetRemoteRowCount
Call GetLocalRowCount
Dim rng As Range, cell As Range, LastValue As String, i As Integer
For i = 3 To LocalRowCount
If IsNumeric(Cells(i, "G").Value) = True And IsEmpty(Cells(i, "G").Value) = False Then
LastValue = "=VLOOKUP(" & Cells(i, "N").Offset(, -7).Value & ",[OtherExcel.XLSX]FirstSheet!R13C1:R" & RemoteRowCount & "C12,10,0)"
Cells(i, "N").Formula = LastValue
End If
Next i
ActiveWorkbook.Save
ActiveWorkbook.Close
Call DeleteAllSubs
Call CloseExcel
End Sub
Sub DeleteAllSubs()
Dim otmp As Object
With ActiveWorkbook.VBProject
For Each otmp In .VBComponents
If otmp.Type=100 Then
otmp.CodeModule.DeleteLines 1, otmp.CodeModule.CountOfLines
otmp.CodeModule.CodePane.Window.Close
Else: .VBComponents.Remove otmp
End If
Next otmp
End With
End Sub
不必发布所有代码。。。试着只选择与你喜欢的不同的相关部分。并清楚地解释哪些是您的期望,以及代码如何没有实现这些期望。在
VBProject.code
模块中删除行与您提出的问题有什么共同之处?基本上,关闭任何打开的工作簿都很简单。。。我没有足够的耐心在如此繁杂的代码中寻找这样的序列。抱歉…我假设您正在尝试从尚未设置工作簿的子系统关闭工作簿。是否将工作簿(“OtherExcel.Xlsx”)。关闭工作?@FaneDuru尝试执行所有操作的部分都是子插入查找。然后,使用工作簿(“OtherExcel.Xlsx”)。关闭False
<代码>错误
如果不希望保存它。。。但是,我可以在ActiveWorkbook.VBProject
中看到那里有ActiveWorkbook.Close
,然后删除一些行。您希望成为哪个ActiveWorkbook
?当我调用返回行计数的sub时,会打开您刚才关闭的那一个,还是另一个?@DaveXcel OtherExcel。调用InsertLookup子项时保持打开状态,并尝试关闭两个excel文件。工作簿(“OtherExcel.Xlsx”)。关闭不起作用。