Excel VBA复制和粘贴(不使用复制+粘贴功能)在空白行上 [英] Excel VBA Copy and Paste (without using Copy+Paste functions) on empty row

查看:330
本文介绍了Excel VBA复制和粘贴(不使用复制+粘贴功能)在空白行上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前已经做过这件事,现在我想弄清楚为什么我不知道为什么。

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

我正在尝试从其中一个提取数据并将其粘贴到下一行的另一张表中。
我在不使用复制或粘贴 VBA函数的情况下完成了此操作。.我只是使用值,我想以这种方式使用户更轻松(在Excel剪贴板上没有随机复制的内容)

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)

这是我的代码:

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


推荐答案

尝试一下:

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

注意:


  • 您应该始终在模块顶部使用 Option Explicit


  • 它将强制您正确声明变量

如果您复制并粘贴了代码而没有任何更改,则存在一个细微的问题:

If you copied and pasted your code without any changes, there is a subtle issue:


  • 您的变量 Row_number在此子(全局)范围之外定义

  • 在此变量内sub中有第二个变量名为 row_number(小写的 r)

  • 循环将全局变量 Row_number
  • 递增
  • 最后总是将值赋给Sheet44.Range( B2)
  • 的最后一行
  • row_number(此子项的局部值)永远不会像您期望的那样递增

  • 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

另一个问题是此 Sheet43.Range(A6).Value 应该是这个 Sheet43.Range( A6)。Value

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

这篇关于Excel VBA复制和粘贴(不使用复制+粘贴功能)在空白行上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆