简体   繁体   中英

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.

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. 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.

If you want it to appear in the row after last row, try ws2.Range("Y" & mRow).Offset(1,0).Value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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