从共享文件夹复制并粘贴到另一个Excel工作簿,然后粘贴回同一文件夹。

huangapple go评论55阅读模式
英文:

copy and paste from another excel workbook on a shared folder and back to same folder

问题

请原谅我复杂的解释,但我正在尽力解决已经让我感到足够复杂的问题!所以请帮帮我。谢谢。

根据@stringeater的第一个回答,我编辑了我的代码。请检查它,并帮助我调整下一步该怎么做。现在我只在setwbkDATABAS = Nothing这一行出现错误。

Sub DATA_BASE_ARCHIVE_FullArchive()

    Dim rngNEWROUND As Excel.Range
    Dim arrNEWROUND As Variant
    Dim wbkDATABASE As Excel.Workbook
    Dim rngDataTarget As Excel.Range
    Dim rngDataSource As Excel.Range
    Dim varData As Variant
    Dim rngArchive As Excel.Range
    
    Application.ScreenUpdating = False
    
    Set rngNEWROUND = ThisWorkbook.Sheets("NEWROUND").Range("A1:N2000")
    
    arrNEWROUND = ThisWorkbook.Sheets("NEWROUND").Range("A1:N2000").Value
    
    Set wbkDATABASE = Workbooks.Open(filename:="E:\DELEGATION APPLICATION SAMPLE-2023\DATABASE.xlsm")
    
    Set rngDataTarget = wbkDATABASE.Sheets("FullArchive").Range("A2001")
    
    Set rngDataTarget = rngDataTarget.Resize(UBound(arrNEWROUND, 1), UBound(arrNEWROUND, 2))
    
    rngDataTarget.Value = arrNEWROUND 
    
    Set rngDataSource = rngDataTarget.Worksheet.Range("A2001")
    
    varData = rngDataSource.Value 
    wbkDATABASE.Save
    wbkDATABASE.Close
    
    Set wbkDATABASE = Nothing '(这里我得到了错误)
    
    Set rngArchive = ThisWorkbook.Sheets("ARCHIVE").Range("A1") '参考范围
    
    rngArchive.Value = varData   
       
    ThisWorkbook.Worksheets("ARCHIVE").Sort.SortFields.Clear
    ThisWorkbook.Worksheets("ARCHIVE").Sort.SortFields.Add Key:=Columns("L:L"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ThisWorkbook.Worksheets("ARCHIVE").Sort.SortFields.Add Key:=Columns("A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
    With ThisWorkbook.Worksheets("ARCHIVE").Sort
        .SetRange Columns("A:P")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Application.ScreenUpdating = True
    
    Sheets("FORM").Select
    
End Sub
英文:

Please bare with me for my below problem I have been working on this for a 3 months and I cant get my head around it.

I have to explain the whole project so that you can understand what I want my code to do:

I have created a user form which is data entry and it will be used by 3 users at the same time each user on a pc has same excel workbook "ENTRY APPLICATION" and data is entered on a sheet called "NEW ROUND" each user data entries have a serial number that starts with 1 - 1000 for example. and another workbook on a shared folder which is there to have all data entered by 3 users be copied and pasted on the shared workbook "DATABASE", then the data collected on the "DATABASE" to be copied again and pasted on same work book "ENTRY APPLICATION" for the users but in another sheet so that it is mirrors to the shared workbook for the user while sorting so that serial number for the data is sorted correctly for each user, fot that I have same workbook for the 3 users but each one just changed the range for them so that their data be copied on a range so that they dont clear other user data entries, for example : user 1 the paste range A1:N2000, user 2 the paste range is A2001:N4000, user 3 paste range is A4001:N6000
then they all get sorted out when pasted again in their workbook which is "DATA APPLICATION" with the user form.

"DATABASE" workbook which is the shared that all collected data is in it , to prevent duplication entries from users (which is in a different module ) but for now my struggle is I'm trying to have that done by less time and more efficient so that I dont have to use the screenupdate and open activate save close workbooks all the time, which can make work slow and might crash.

I have come to read a great thread here now about Parent Object which apparently save great time and errors for my same needs, but I have no idea what so ever how to reflect that on my userform workbook and how to adjust my code.

please help me adjust my code , hope that I have explained it correctly.

Sub DATA_BASE_ARCHIVE_FullArchive()

Application.ScreenUpdating = False

Windows("ENTRY APPLICATION.xlsm").Activate
Sheets("NEWROUND").Select
Range("A1:N2000").Select

Selection.Copy

Workbooks.Open filename:= _
    "\-2023\DATABASE.xlsm"
Windows("DATABASE.xlsm").Activate
Range("A2001").Select
Sheets("FullArchive").Paste
Cells.Select
Range("A2001").Activate
Application.CutCopyMode = False
Selection.Copy


Windows("ENTRY APPLICATION.xlsm").Activate
Sheets("ARCHIVE").Select

Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.Worksheets("ARCHIVE").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("ARCHIVE").Sort.SortFields.ADD Key:=Columns("L:L") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("ARCHIVE").Sort.SortFields.ADD Key:=Columns("A:A") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("ARCHIVE").Sort
    .SetRange Columns("A:P")
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With


Windows("DATABASE.xlsm").Activate
ActiveWorkbook.Save
ActiveWindow.Close
Application.CutCopyMode = False
Windows("ENTRY APPLICATION.xlsm").Activate
Sheets("FORM").Select
End Sub

please forgive my complicated explanation, but the thing I am trying to do has already complicated me enough ! so please help. thank you.

my edited code according to the first answer I received from @stringeater. please check it and help me what to adjust next. Im just getting an error now in setwbkDATABAS = Nothing

    Sub DATA_BASE_ARCHIVE_FullArchive()

Dim rngNEWROUND As Excel.Range
Dim arrNEWROUND As Variant
Dim wbkDATABASE As Excel.Workbook
Dim rngDataTarget As Excel.Range
Dim rngDataSource As Excel.Range
Dim varData As Variant
Dim rngArchive As Excel.Range


Application.ScreenUpdating = False

Set rngNEWROUND = ThisWorkbook.Sheets("NEWROUND").Range("A1:N2000")

arrNEWROUND = ThisWorkbook.Sheets("NEWROUND").Range("A1:N2000")

Set wbkDATABASE = Workbooks.Open(filename:="E:\DELEGATION APPLICATION SAMPLE-2023\DATABASE.xlsm")

Set rngDataTarget = wbkDATABASE.Sheets("FullArchive").Range("A2001")

Set rngDataTarget = rngDataTarget.Resize(UBound(arrNEWROUND, 1), UBound(arrNEWROUND, 2))

rngDataTarget.Value = arrNEWROUND 

Set rngDataSource = rngDataTarget.Worksheet.Range("A2001")

varData = rngDataSource.Value 
wbkDATABASE.Save
wbkDATABASE.Close

setwbkDATABASE = Nothing '(and Im getting error here)

Set rngArchive = ThisWorkbook.Sheets("ARCHIVE").Range("A1") 'reference range

rngArchive.Value = varData   
   
   
ThisWorkbook.Worksheets("ARCHIVE").Sort.SortFields.Clear
ThisWorkbook.Worksheets("ARCHIVE").Sort.SortFields.ADD Key:=Columns("L:L"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ThisWorkbook.Worksheets("ARCHIVE").Sort.SortFields.ADD Key:=Columns("A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

With ThisWorkbook.Worksheets("ARCHIVE").Sort
      .SetRange Columns("A:P")
      .Header = xlGuess
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
End With

Application.ScreenUpdating = True

Sheets("FORM").Select

End Sub

答案1

得分: 2

我已将您的代码行注释掉,并将我的代码行放在下面,以便您可以看到如何“翻译”您的代码。

Sub DATA_BASE_ARCHIVE_FullArchive()

    ' 声明
    Dim rngNewRound As Excel.Range    ' ThisWorkbook 中的范围对象
    Dim arrNewRound As Variant        ' 包含范围内容的二维数组
    Dim wbkDatabase As Excel.Workbook ' DATABASE 对象
    Dim rngDataTarget As Excel.Range  ' 数据库中的目标范围
    Dim rngDataSource As Excel.Range  ' 数据库中的源范围
    Dim varData As Variant            ' 来自数据库的单元格内容
    Dim rngArchive As Excel.Range     ' 工作表 ARCHIVE 上的目标范围

    Application.ScreenUpdating = False ' 不要忘记在最后设置为 True

    ' 设置 rngNewRound 为 ThisWorkbook 中 "NEWROUND" 工作表的范围
    Set rngNewRound = ThisWorkbook.Sheets("NEWROUND").Range("A1:N2000")
    
    ' 将 rngNewRound 的内容保存到数组 arrNewRound 中
    arrNewRound = rngNewRound.Value
    
    ' 打开名为 "DATABASE-2-2023.xlsm" 的工作簿
    Set wbkDatabase = Workbooks.Open(Filename:="\-2023\DATABASE-2-2023.xlsm")
    
    ' 设置 rngDataTarget 为数据库工作簿 "FullArchive" 工作表的 "A2001" 单元格的左上角
    Set rngDataTarget = wbkDatabase.Sheets("FullArchive").Range("A2001")
    
    ' 调整范围大小以匹配数组的大小
    Set rngDataTarget = rngDataTarget.Resize(UBound(arrNewRound, 1), UBound(arrNewRound, 2))
    
    ' 将 arrNewRound 插入到 rngDataTarget
    rngDataTarget.Value = arrNewRound
    
    ' 设置 rngDataSource 为相同工作表中的新范围 "A2001"
    Set rngDataSource = rngDataTarget.Worksheet.Range("A2001")
    
    ' 保存数据库工作簿
    wbkDatabase.Save
    
    ' 关闭数据库工作簿
    wbkDatabase.Close
    
    ' 释放内存
    Set wbkDatabase = Nothing
    
    ' 设置 rngArchive 为 ThisWorkbook 中 "ARCHIVE" 工作表的 "A1" 单元格的引用
    Set rngArchive = ThisWorkbook.Sheets("ARCHIVE").Range("A1")
    
    ' 将 varData 插入到单元格 A1
    rngArchive.Value = varData
    
    ' 对工作表 "ARCHIVE" 进行排序
    ThisWorkbook.Worksheets("ARCHIVE").Sort.SortFields.Clear
    ThisWorkbook.Worksheets("ARCHIVE").Sort.SortFields.Add Key:=Columns("L:L"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ThisWorkbook.Worksheets("ARCHIVE").Sort.SortFields.Add Key:=Columns("A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
    With ThisWorkbook.Worksheets("ARCHIVE").Sort
        .SetRange Columns("A:P")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    ' 保存当前工作簿
    ThisWorkbook.Save
    
    ' 关闭当前窗口
    ActiveWindow.Close
    
    ' 设置屏幕更新为 True,以便您可以看到更新后的结果
    Application.ScreenUpdating = True
    
    ' 选择工作表 "FORM"
    ThisWorkbook.Sheets("FORM").Select

End Sub
英文:

I have commented your code lines out and put my lines underneath so that you can see how to "translate" your code. You could shorten the code even more but it might not become more understandable.

Sub DATA_BASE_ARCHIVE_FullArchive()
  
        'declarations
  Dim rngNewRound As Excel.Range    'range object in ThisWorkbook
  Dim arrNewRound As Variant        '2-dim array with content of range
  Dim wbkDatabase As Excel.Workbook 'DATABASE object
  Dim rngDataTarget As Excel.Range  'target range in database
  Dim rngDataSource As Excel.Range  'source range in database
  Dim varData As Variant            'cell content from database
  Dim rngArchive As Excel.Range     'target range on sheet ARCHIVE

  Application.ScreenUpdating = False 'don't forget ... = True at the end
    
  'Windows("ENTRY APPLICATION.xlsm").Activate
  'Sheets("NEWROUND").Select
  'Range("A1:N2000").Select
        'supposing that this VBA code is contained in ENTRY APPLICATIN.xlsm
  Set rngNewRound = ThisWorkbook.Sheets("NEWROUND").Range("A1:N2000") 'reference range
  'Selection.Copy
  arrNewRound = rngNewRound.Value 'save the content of the range in an array
    
  'Workbooks.Open Filename:="\-2023\DATABASE-2-2023.xlsm"
  Set wbkDatabase = Workbooks.Open(Filename:="\-2023\DATABASE-2-2023.xlsm")
        'is DATABASE.xlsm = DATABASE-2-2023.xlsm? Please clarify!
  'Windows("DATABASE.xlsm").Activate
  'Range("A2001").Select
  Set rngDataTarget = wbkDatabase.Sheets("FullArchive").Range("A2001") 'set top-left corner
        'resize the range so that it matches the size of the array
  Set rngDataTarget = rngDataTarget.Resize(UBound(arrNewRound, 1), UBound(arrNewRound, 2))
  'Sheets("FullArchive").Paste
  rngDataTarget.Value = arrNewRound 'insert array to range
  
  'Cells.Select
  'Range("A2001").Activate
  Set rngDataSource = rngDataTarget.Worksheet.Range("A2001") 'new range in same worksheet
  'Application.CutCopyMode = False
  'Selection.Copy
  varData = rngDataSource.Value     'save range value to single variable
  wbkDatabase.Save                  'save the database
  wbkDatabase.Close                 'close the database
  Set wbkDatabase = Nothing         'release memory
  
  'Windows("ENTRY APPLICATION.xlsm").Activate
  'Sheets("ARCHIVE").Select
  'Range("A1").Select
  Set rngArchive = ThisWorkbook.Sheets("ARCHIVE").Range("A1") 'reference range
  'ActiveSheet.Paste
  'Application.CutCopyMode = False
  rngArchive.Value = varData        'insert single variable to cell A1
  
  'The sorting is probably ok. Please use ThisWorkbook instead of ActiveWorkbook
  ActiveWorkbook.Worksheets("ARCHIVE").Sort.SortFields.Clear
  ActiveWorkbook.Worksheets("ARCHIVE").Sort.SortFields.Add Key:=Columns("L:L"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  ActiveWorkbook.Worksheets("ARCHIVE").Sort.SortFields.Add Key:=Columns("A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  With ActiveWorkbook.Worksheets("ARCHIVE").Sort
      .SetRange Columns("A:P")
      .Header = xlGuess
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
    End With
  End With
  
  Windows("DATABASE.xlsm").Activate
  ActiveWorkbook.Save
  ActiveWindow.Close
  Application.CutCopyMode = False
  Windows("ENTRY APPLICATION.xlsm").Activate
  
  Application.ScreenUpdating = True 'otherwise you may not see the updated result
  
  Sheets("FORM").Select

End Sub

答案2

得分: 1

以下是您要翻译的内容:

"Since I don't have your sample and all the codes, I can only imagine. Please try using the Range object instead of Selection, unless you want to see it running when it is running, otherwise, there is no need to refresh the screen. In your code:
> Application.ScreenUpdating = False

ScreenUpdating = False and you also said
>so that I don't have to use the screenupdate and open activate save close workbooks all the time. So that I don't have to use the screenupdate and open activate save close workbooks all the time, which can make work slow and might crash.

also :
> want a more non human approach to be faster and more efficient

So it is ideal to use Range instead of Selection to implement. This is the same principle as in Word and PowerPoint VBA. Unless you need to refresh the screen or something needs to have the focus to do the operation, you should not actually use Selection. I think @Stringeater's answer upstairs has already done this, so he commented out your Selection statements and used the Range object below to execute it.

If you still don't understand clearly or have questions, please let us know again.

20230527 12:59 (CST)

If you just want to save the results as an Excel file (with or without the code), it is easy to do. The simplest way is to save it locally and then overwrite the destination file with a CopyFile method. If local saving is not possible and only save-as is allowed, then the SaveAs method in ExcelVBA will have to be used. It is more complicated to pay attention to the Multitasking reading conflict. I'm sure @Stringeater will come up with a nice solution, just like he did before. Thank you"

英文:

Since I don't have your sample and all the codes, I can only imagine. Please try using the Range object instead of Selection, unless you want to see it running when it is running, otherwise, there is no need to refresh the screen. In your code :
> Application.ScreenUpdating = False

ScreenUpdating = False and you also said
>so that I don't have to use the screenupdate and open activate save close workbooks all the time. So that I don't have to use the screenupdate and open activate save close workbooks all the time, which can make work slow and might crash.

also :
> want a more non human approach to be faster and more efficient

So it is ideal to use Range instead of Selection to implement. This is the same principle as in Word and PowerPoint VBA. Unless you need to refresh the screen or something needs to have the focus to do the operation, you should not actually use Selection. I think @Stringeater 's answer upstairs has already done this, so he commented out your Selection statements and used the Range object below to execute it.

If you still don't understand clearly or have questions, please let us know again.

20230527 12:59 (CST)

If you just want to save the results as an Excel file (with or without the code), it is easy to do. The simplest way is to save it locally and then overwrite the destination file with a CopyFile method. If local saving is not possible and only save-as is allowed, then the SaveAs method in ExcelVBA will have to be used. It is more complicated to pay attention to the Multitasking reading conflict. I'm sure @Stringeater will come up with a nice solution, just like he did before. Thank you

huangapple
  • 本文由 发表于 2023年5月25日 03:47:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定