简体   繁体   English

VBA 问题 - 从一个工作簿复制到另一个?

[英]Issue with VBA - Copying from one workbook to another?

I'll preface this by saying I'm a complete novice to VBA but not coding.我先说我是 VBA 的完全新手,但不会编码。 I'm trying to write part of a VBA script that can copy data from one worksheet in another workbook to a pre-existing worksheet in another workbook.我正在尝试编写 VBA 脚本的一部分,该脚本可以将数据从另一个工作簿中的一个工作表复制到另一个工作簿中的预先存在的工作表中。

I'm trying to make use of dynamic ranges as I know the starting point for the data every time however my issues comes when it comes to the copying.我正在尝试利用动态范围,因为我每次都知道数据的起点,但是在复制时出现了我的问题。 The code runs up to the point of the copy where it stagnates with no error codes.代码一直运行到副本的点,在那里它停滞不前,没有错误代码。 Effectively gets stuck in stasis requiring user action.有效地陷入需要用户操作的停滞状态。

Set sht = ThisWorkbook.Worksheets("Requisitions Raised")
Set StartCell = ThisWorkbook.Worksheets("Requisitions Raised").Range("A8")
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column
Set startingRange = sht.Range(StartCell, sht.Cells(LastRow, LastColumn))

Set sht = Workbooks(FileName).Worksheets("1-Data")
Set StartCell = Workbooks(FileName).Worksheets("1-Data").Range("A8")
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column
Set copyRange = sht.Range(StartCell, sht.Cells(LastRow, LastColumn))

ThisWorkbook.Worksheets("Requisitions Raised").startingRange.Value = Workbooks(FileName).Worksheets("1-Data").copyRange.Value

I've also tried the below to no avail我也试过以下无济于事

Workbooks(FileName).Worksheets("1-Data").copyRange.Copy Destination:=ThisWorkbook.Worksheets("Requisitions Raised").startingRange

What's causing the error?导致错误的原因是什么? Is there a better copying solution?有没有更好的复制解决方案?

Cheers!干杯!

As per my comment, you currently Set your Range object, but then try to use them as members of the Worksheet , which would (at least for me) result into a compile error.根据我的评论,您当前SetRange对象,但随后尝试将它们用作Worksheet成员,这会(至少对我而言)导致编译错误。

Instead of copy/paste, I would advise a dynamic Value transfer.我建议进行动态Value转移,而不是复制/粘贴。 As per @FoxfireAndBurnsAndBurns also said;根据@FoxfireAndBurnsAndBurns 还说; you need equal sized Range object (or arrays).您需要大小相等的Range对象(或数组)。 So therefor I included a Resize of Range("A8") .因此,我包含了一个Resize of Range("A8") Try the following:请尝试以下操作:

Dim sht1 As Worksheet, sht2 As Worksheet
Dim lr As Long, lc As Long

Set sht1 = ThisWorkbook.Worksheets("Requisitions Raised")
Set sht2 = Workbooks(FileName).Worksheets("1-Data")

lr = sht2.Cells(sht2.Rows.Count, 1).End(xlUp).Row
lc = sht2.Cells(8, sht2.Columns.Count).End(xlToLeft).Column

sht1.Cells(8, 1).Resize(lr - 7, lc).Value = sht2.Range(sht2.Cells(8, 1), sht2.Cells(lr, lc)).Value

As you can see I used also two different variables for Worksheet .如您所见,我还为Worksheet使用了两个不同的变量。 Not per se really meaningfull names, but it helps to distinquish.本身并不是真正有意义的名称,但它有助于区分。 I guess it's a personal preference.我想这是个人喜好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM