从网络驱动器打开工作簿并将数据粘贴到现有工作簿中

时间:2018-02-02 16:21:24

标签: excel vba excel-vba

我有两本工作簿。 WB1是来自网络文件夹本身的活动工作簿,WB2是网络驱动器中已有的工作簿。

我想打开WB2,复制所有内容并粘贴到WB1并关闭WB2而不保存。

此外,我试图在WB1的第8列中删除所有不包含" TRU"的那些。

我尝试了以下代码,但我不确定如何指定为网络驱动器打开的获取。

这是我的代码。

Sub newWB()
Dim WB1 As workbook
Dim WB2 As workbook
Dim i As Long, j As Long
Dim totalrows As Long
Dim PasteToStart As Range
Dim FileToOpen
Dim sheet As Worksheet

Set WB1 = ActiveWorkbook
Set PasteToStart = [Sheet1!A1]

FileToOpen = Application.GetOpenFilename("file://cw.wan.com/root/Loc/PL/04/Projektlist.xlsx")

If FileToOpen = False Then
MsgBox ("no File Found")
Exit Sub

Else

Set WB2 = Workbooks.Open(Filename:=FileToOpen)

For Each sheet In WB2.Sheets

With sheet.UsedRange
.Copy PasteToStart
Set PasteToStart = PasteToStart.Offset(.Rows.Count)
End With
Next sheet
End If

With ActiveWorkbook.Sheets("Sheet1")
    totalrows = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = totalrows To 2 Step -1
        If .Cells(i, 8).Value <> "TRU" Then
        Cells(i, 8).EntireRow.Delete
        End If
Next
End With

WB2.Close
End Sub

0 个答案:

没有答案