Excel VBA Workbook Update

跟風遠走 提交于 2020-01-17 16:35:14

问题


I currently have a workbook with a macro to update a range from an identical range on a master sheet. Right now, I simply use a macro to open the master workbook, copy the range from it and paste the values in the same range in the other workbook.

Here is the code I am currently using:

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  

As you can see, the code is very simple and updates a custom range where data lies in each of the 16 worksheets in the workbook.

The issue I'm running into is this: on the master workbook, the # of rows can change as I add data to it. Normally, I would get around this by just copying the entire worksheet and thus replicating the entire workbook each time it updates, the problem is that the workbooks being updated are saved by multiple people, who take notes/mark progress in a column in their workbooks. To help paint a better picture, imagine the (crude) matrix below is a worksheet. Each person's worksheet is getting a, b, c & d updated, but THEY enter in 1, 4, 2, etc. I need 1, 4, 2 etc to be preserved in each of their respective rows even as more rows of a, b, c & d are added. Right now, if I were to add a row of w, x, y & z above the 3rd row on the master, 4 would then become part of that row and i, j, k & l would be blank on the updated sheet.

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

Sorry for not making this clear earlier. You guys are right, linking workbooks is the right way to do it- the problem is that the server security here won't let that happen. Thus, I need to copy from an open workbook :(


回答1:


You don't need Macro or VBA to do this.
Try this: (Note - Screen Shots generated from Excel 2010)

  1. From Data Tab, choose Existing Connections.

  2. Then choose Browse for more...

  3. Then select your Excel File or your master workbook.

  4. You will be asked what Sheet as seen below.

  5. Then you will be asked how you want it reflected and the destination range. Just press ok.

And there you have your master workbook sheet1(if you select sheet1) replica.
To update anything that other users did in the master workbook, just click Refresh All in Data Tab beside Existing Connections.

Edit1: You can also try deleting all the sheets in your duplicate workbook and then copying all worksheets from the Master Workbook. See below VBA code:

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



回答2:


If you insert rows into a named range of cells the range grows.

If that doesn't help say what eg A1:A12 you want and what eg B1:12 you don't.



来源:https://stackoverflow.com/questions/23726853/excel-vba-workbook-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!