Excel VBA Copy and Paste (without using Copy+Paste functions) on empty row

巧了我就是萌 提交于 2020-01-05 02:31:06

问题


I've done this before and I'm scratching my head to why I can't figure it out.

I'm trying to pull data from one sheet and paste it into another sheet on the next row. I've done this before without using "copy" or "paste" VBA functions.. I just used values and I would like to make it easier on the user that way (no random copied things on the excel clipboard)

Here is my code:

Sub SubmitChange()
row_number = 2

Do
DoEvents
Row_number = row_number + 1
item_in_review = Sheet44.Range("B" & row_number)
Loop Until item_in_review = ""

Sheet44.Range("B" & row_number) = Sheet43.Range(A6).Value

End Sub

回答1:


Try this:

Option Explicit

Sub SubmitChange()

    Dim lastRow As Long

    lastRow = Sheet44.Cells(Sheet44.Rows.Count, 2).End(xlUp).Row + 1

    Sheet44.Range("B" & lastRow).Value2 = Sheet43.Range("A6").Value2

End Sub

.

Notes:

  • You should always use Option Explicit at the top of the module

    • it will force you to properly declare variables
  • If you copied and pasted your code without any changes, there is a subtle issue:

    • your variable "Row_number" is defined outside the scope of this sub (global)
    • inside this sub you have a second variable named "row_number" (lower case "r")
    • the loop increments the global variable "Row_number"
    • but in the last line the value gets always assigned to Sheet44.Range("B2")
    • "row_number" (local to this sub) never gets incremented as you'd expect

The other issue is this Sheet43.Range(A6).Value should be this Sheet43.Range("A6").Value



来源:https://stackoverflow.com/questions/32152282/excel-vba-copy-and-paste-without-using-copypaste-functions-on-empty-row

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