简体   繁体   English

将数据从一个工作簿复制到另一个工作簿会粘贴在随机单元格中

[英]Copying data from one workbook to another gets pasted in random cells

Update更新

I have been playing around with my code and figured out how to make the data paste in the right place the second time but then the data won't paste at all if you press the button again.我一直在玩我的代码,并想出了如何第二次将数据粘贴到正确的位置,但是如果再次按下按钮,数据将根本无法粘贴。 Here is the updated code:这是更新后的代码:

Dim kRow As Long
kRow = ws2.Range("W" & Rows.count).End(xlUp).Row

If ws2.Range("W4").Value = "" Then
ws2.Range("W4").Value = ws1.Range("A4").Value
Else:
kRow = ws2.Range("W" & Rows.count).End(xlUp).Row - 31
ws2.Range("W" & kRow).Offset(1).Value = ws1.Range("A4").Value
End If

Dim lRow As Long
lRow = ws2.Range("V" & Rows.count).End(xlUp).Row

If ws2.Range("V10").Value = "" Then
ws2.Range("V10").Value = ws1.Range("P2").Value
Else:
lRow = ws2.Range("V" & Rows.count).End(xlUp).Row - 31
ws2.Range("V" & lRow).Offset(1).Value = ws1.Range("P2").Value
End If

Dim mRow As Long
mRow = ws2.Range("Y" & Rows.count).End(xlUp).Row

If ws2.Range("Y10").Value = "" Then
ws2.Range("Y10").Value = ws1.Range("K37").Value
Else:
mRow = ws2.Range("Y" & Rows.count).End(xlUp).Row - 28
ws2.Range("Y" & mRow).Offset(1).Value = ws1.Range("K37").Value
End If

Original Post原帖


I'm having this annoying issue with copying and pasting data from one workbook to another.我在将数据从一个工作簿复制并粘贴到另一个工作簿时遇到了这个烦人的问题。 I am trying to copy some data from a user form to a master workbook that keeps all the data.我正在尝试将一些数据从用户表单复制到保留所有数据的主工作簿。 I have it set up so that when then user is done with the form, they click a macro button and the master workbook opens, the data from the form gets pasted into the master workbook and then it closes.我已经设置好了,当用户完成表单时,他们单击一个宏按钮并打开主工作簿,表单中的数据被粘贴到主工作簿中,然后关闭。 The next time the user form is filled out and the macro button is pressed, the data is pasted in the next available row in the master workbook.下次填写用户表单并按下宏按钮时,数据将粘贴到主工作簿的下一个可用行中。 Most of the data I want to get transferred works except for 5 pieces of data.除了5条数据外,我想要传输的大部分数据都有效。

The first time I press the button, everything is fine.我第一次按下按钮时,一切都很好。 However, if I press the button again, these five pieces of data do not get pasted in the next row as intended but it gets pasted 31 to 28 rows lower than intended.但是,如果我再次按下按钮,这五个数据不会按预期粘贴到下一行,而是粘贴到比预期低 31 到 28 行。 I have been trying everything to fix this but I can't seem to find the issue.我一直在尝试一切来解决这个问题,但我似乎无法找到问题所在。

I will post the relevant code below.我将在下面发布相关代码。 Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks!谢谢!

Dim kRow As Long
kRow = ws2.Range("W" & Rows.count).End(xlUp).Row

If ws2.Range("W10").Value = "" Then
ws2.Range("W10").Value = ws1.Range("A4").Value
Else: ws2.Range("W" & kRow).Offset(1).Value = ws1.Range("A4").Value
End If

Dim lRow As Long
lRow = ws2.Range("V" & Rows.count).End(xlUp).Row

If ws2.Range("V10").Value = "" Then
'ws2.Range("V10").Value = ws1.Range("P2").Value
Else: ws2.Range("V" & lRow).Offset(1).Value = ws1.Range("P2").Value
End If

Dim mRow As Long
mRow = ws2.Range("Y" & Rows.count).End(xlUp).Row

If ws2.Range("Y10").Value = "" Then
ws2.Range("Y10").Value = ws1.Range("K37").Value
Else: ws2.Range("Y" & mRow).Offset(1).Value = ws1.Range("K37").Value
End If

You're setting the offset to 1, meaning it's taking both dimensions of the range and offsetting it by that much.您将偏移量设置为 1,这意味着它采用了范围的两个维度并将其偏移了那么多。

If you want it to appear in the row after last row, try ws2.Range("Y" & mRow).Offset(1,0).Value如果您希望它出现在最后一行之后的行中,请尝试ws2.Range("Y" & mRow).Offset(1,0).Value

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

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