|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Dim app As Excel.Application = New Excel.Application()
Dim twb As Excel.Workbook = app.Workbooks.Open("C:\Users\Administrator\Desktop\工作簿1.xlsx")
Dim extwbk As Excel.Workbook = app.Workbooks.Open("G:\工作簿2.xlsx")
Dim x As Excel.Range = extwbk.Worksheets("Sheet").Range("A:B")
With twb.Sheets("MH")
For rw As Integer = 2 To .Cells(.Rows.Count, 1).End(Excel.XlDirection.xlUp).Row
Dim lookupResult As Object = app.WorksheetFunction.VLookup(.Cells(rw, 1).Value2, x, 2, False)
If Not IsNothing(lookupResult) Then
.Cells(rw, 2).Value = lookupResult
End If
Next rw
End With
extwbk.Close(SaveChanges:=False)
twb.Close(SaveChanges:=True)
app.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(x)
System.Runtime.InteropServices.Marshal.ReleaseComObject(extwbk)
System.Runtime.InteropServices.Marshal.ReleaseComObject(twb)
System.Runtime.InteropServices.Marshal.ReleaseComObject(app)
MessageBox.Show("完成")
以上代码在打开EXCEL软件时,新建一个工作簿运行代码时没有问题,但我想直接打开 工作簿1.xlsx 来运行代码,则会出现一个副本,怎么将 twb 修改为当前工作簿及工作表?
|
|