Excel VBA工作簿更新

时间:2014-05-18 21:07:23

标签: excel vba excel-vba updating

我目前有一个带有宏的工作簿来更新主工作表上相同范围的范围。现在,我只是使用宏来打开主工作簿,从中复制范围并将值粘贴到另一个工作簿的相同范围内。

以下是我目前使用的代码:

Sub GetDataFromClosedWorkbook()
'Created by XXXX 5/2/2014
Application.ScreenUpdating = False ' turn off the screen updating

Dim wb As Workbook
Set wb = Workbooks.Open("LOCATION OF FILE", True, True)
' open the source workbook, read only
With ThisWorkbook.Worksheets("1")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("F8:K25").Value = wb.Worksheets("1").Range("F8:K25").Value
End With

With ThisWorkbook.Worksheets("2")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("V5:Z359").Value = wb.Worksheets("2").Range("V5:Z359").Value
End With

With ThisWorkbook.Worksheets("3")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("V5:AE238").Value = wb.Worksheets("3").Range("V5:AE238").Value
End With

With ThisWorkbook.Worksheets("4")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("V5:AB33").Value = wb.Worksheets("4").Range("V5:AB33").Value
End With

With ThisWorkbook.Worksheets("5")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("V5:Y140").Value = wb.Worksheets("5").Range("V5:Y140").Value
End With

With ThisWorkbook.Worksheets("6")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("V5:AD170").Value = wb.Worksheets("6").Range("V5:AD170").Value
End With

With ThisWorkbook.Worksheets("7")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("V5:AF579").Value = wb.Worksheets("7").Range("V5:AF579").Value
End With

With ThisWorkbook.Worksheets("8")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("Q5:AC182").Value = wb.Worksheets("8").Range("Q5:AC182").Value
End With

With ThisWorkbook.Worksheets("9")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("U5:AK120").Value = wb.Worksheets("9").Range("U5:AK120").Value
End With

With ThisWorkbook.Worksheets("10")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("V5:AC140").Value = wb.Worksheets("10").Range("V5:AC140").Value
End With

With ThisWorkbook.Worksheets("11")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("V5:AG947").Value = wb.Worksheets("11").Range("V5:AG947").Value
End With

With ThisWorkbook.Worksheets("12")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("V5:AB145").Value = wb.Worksheets("12").Range("V5:AB145").Value
End With

With ThisWorkbook.Worksheets("13")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("O5:AE10").Value = wb.Worksheets("13").Range("O5:AE10").Value
End With

With ThisWorkbook.Worksheets("14")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("V5:AA14").Value = wb.Worksheets("14").Range("V5:AA14").Value
End With

With ThisWorkbook.Worksheets("15")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("V5:AF201").Value = wb.Worksheets("15").Range("V5:AF201").Value
End With

With ThisWorkbook.Worksheets("16")
    ' read data from the source workbook: (Left of (=) is paste @ destination, right of it is copy)
    .Range("Q5:AB14").Value = wb.Worksheets("16").Range("Q5:AB14").Value
End With

wb.Close False ' close the source workbook without saving any changes
Set wb = Nothing ' free memory
Application.ScreenUpdating = True ' turn on the screen updating

End Sub  

如您所见,代码非常简单,并且更新了自定义范围,其中数据位于工作簿的16个工作表中。

我遇到的问题是:在主工作簿上,当我向其添加数据时,行数会发生变化。通常,我会通过复制整个工作表来解决这个问题,从而在每次更新时复制整个工作簿,问题是正在更新的工作簿是由多个人保存的,他们在工作簿的列中记录笔记/标记进度。为了帮助描绘更好的图片,想象下面的(粗)矩阵是一个工作表。每个人的工作表都是a,b,c& d更新,但是它们输入1,4,2等等。我需要将1,4,2等等保存在它们各自的每一行中,即使是更多行的a,b,c和amp; d被添加。现在,如果我要添加一行w,x,y& z在主机的第3行上方,4将成为该行的一部分,i,j,k& l在更新的表格上将是空白的。

a b c 1 d  
e f g   h                                   
i j k 4 l   
m n o   p  
q r s 2 t   
u v w 6 x

很抱歉没有提前说清楚。你们是对的,链接工作簿是正确的方法 - 问题是这里的服务器安全性不会让这种情况发生。因此,我需要从一个打开的工作簿中复制:(

2 个答案:

答案 0 :(得分:1)

您不需要Macro或VBA来执行此操作 试试这个:(注意 - 从Excel 2010生成的屏幕截图)

  1. Data Tab,选择Existing Connections enter image description here

  2. 然后选择Browse for more... enter image description here

  3. 然后选择您的Excel文件或主工作簿 enter image description here

  4. 您将被问到下面的表格。
    enter image description here

  5. 然后会询问您希望它如何反映以及目的地范围。按下确定。
    enter image description here

  6. 你有你的主工作簿sheet1(如果你选择sheet1)复制品 要更新其他用户在主工作簿中执行的操作,只需点击Refresh All旁边Data Tab中的Existing Connections

    编辑1:您还可以尝试删除重复工作簿中的所有工作表,然后从Master Workbook复制所有工作表。请参阅下面的VBA代码:

    Sub Test()
    Dim wbM As Workbook, wbR As Workbook, ws As Worksheet
    Dim fpath As String
    Dim mysheets
    
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With
    
    fpath = "Location of File"
    Set wbR = ThisWorkbook
    Set wbM = Workbooks.Open(fpath, True, True)
    '~~> Create a dummy sheet
    '~~> This is needed, Excel won't allow deleting all sheets
    wbR.Sheets.Add(wbR.Sheets(1)).Name = "Temp"
    
    '~~> Delete all sheets except the dummy
    For Each ws In wbR.Worksheets
        If ws.Name <> "Temp" Then ws.Delete
    Next
    
    '~~> Generate the array of sheet names
    For Each ws In wbM.Worksheets
        If IsEmpty(mysheets) Then
            mysheets = Array(ws.Name)
        ElseIf IsArray(mysheets) Then
            ReDim Preserve mysheets(UBound(mysheets) + 1)
            mysheets(UBound(mysheets)) = ws.Name
        End If
    Next
    
    '~~> Copy the sheets from Master workbook
    wbM.Sheets(mysheets).Copy after:=wbR.Sheets(1)
    '~~> Clean up, delete the dummy sheet
    wbR.Sheets("Temp").Delete
    wbM.Close False
    
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
    
    End Sub
    

答案 1 :(得分:0)

如果在命名的单元格区域中插入行,则范围会增大。

如果那并不能说明你想要的A1:A12以及例如B1:12的内容,那么你就不会这样做。