Excel宏-仅将非空单元格从一张工作表粘贴到另一张工作表

Excel宏-仅将非空单元格从一张工作表粘贴到另一张工作表,excel,copy-paste,vba,Excel,Copy Paste,Vba,下面是我用来从一张纸上复制单元格并粘贴到另一张纸上的代码 Sheets("codes").Select Range("A5:A100").Select Selection.Copy Sheets("Sheet2").Select Range("B28").Select ActiveSheet.Paste 问题是,此范围内的某些单元格为空白,但我不希望将其复制到Sheet2。我从中得到了一些想法,但是这个方法太长了。是否有一种方法可以迭代选择并检查值是否为非空并粘贴。这样,我也可以在空白单元格中

下面是我用来从一张纸上复制单元格并粘贴到另一张纸上的代码

Sheets("codes").Select
Range("A5:A100").Select
Selection.Copy
Sheets("Sheet2").Select
Range("B28").Select
ActiveSheet.Paste

问题是,此范围内的某些单元格为空白,但我不希望将其复制到Sheet2。我从中得到了一些想法,但是这个方法太长了。是否有一种方法可以迭代选择并检查值是否为非空并粘贴。这样,我也可以在空白单元格中粘贴一些其他的文本,如Na。

看起来你可能在这里犯下一些常见的新手错误。没关系,我们都做到了。p> 逐行解释的VBA示例 提示:尽量不要使用选择或复制。当您只需引用单元格本身时,为什么要使用select?例如,不使用

Sheets("codes").Select
Range("A5:A100").Select
Selection.Copy
Sheets("Sheet2").Select
Range("B28").Select
ActiveSheet.Paste
只用

dim mySheet as Worksheet, myOtherSheet as Worksheet, myBook as Workbook 'Define your workbooks and worksheets as variables
set myBook = Excel.ActiveWorkbook
set mySheet = myBook.Sheets("codes")
set myOtherSheet = myBook.Sheets("Sheet2")

dim i as integer, j as integer 'Define a couple integer variables for counting

j = 28 'This variable will keep track of which row we're on in Sheet2 (I'm assuming you want to start on line 28)
for i = 5 to 100 'This is the beginning the the loop which will repeat from 5 to 100 . . .
   if mySheet.Cells(i,1).value <> "" then ' . . . for each digit, it will check if the cell's value is blank. If it isn't then it will . . .
      myOtherSheet.Cells(j,2).value = mySheet.Cells(i,1).value ' . . . Copy that value into the cell on Sheet2 in the row specified by our "j" variable.
      j = j + 1 'Then we add one to the "j" variable so the next time it copies, we will be on the next available row in Sheet2.
   end if
next i 'This triggers the end of the loop and moves on to the next value of "i".
当我刚开始的时候,我一直在做同样的事情,但结果总是不对。选择“左”和“右”会导致错误。使用我的代码,阅读评论,你会没事的。一个快速警告:这台计算机上没有Excel,因此无法测试代码。如果因为某种原因它不起作用,请给我留言,明天我会在工作中修复它

将数据复制到第二张工作表时,上述代码将完全忽略空白单元格。如果要为空白单元格输入特定文本,如N/a,则可以使用以下选项:

 dim mySheet as Worksheet, myOtherSheet as Worksheet, myBook as Workbook 'Define your workbooks and worksheets as variables
 set myBook = Excel.ActiveWorkbook
 set mySheet = myBook.Sheets("codes")
 set myOtherSheet = myBook.Sheets("Sheet2")

 dim i as integer, j as integer 'Define a couple integer variables for counting

 j = 28 'This variable will keep track of which row we're on in Sheet2 (I'm assuming you want to start on line 28)
 for i = 5 to 100 'This is the beginning the the loop which will repeat from 5 to 100 . . .
    if mySheet.Cells(i,1).value <> "" then ' . . . for each digit, it will check if the cell's value is blank. If it isn't then it will . . .
       myOtherSheet.Cells(j,2).value = mySheet.Cells(i,1).value ' . . . Copy that value into the cell on Sheet2 in the row specified by our "j" variable.
    else 'If the cell is blank, then . . .
       myOtherSheet.Cells(j,2).value = "N/A" ' . . . place the text "N/A" into the cell in row "j" in Sheet2.
    end if 'NOTICE we moved the "end if" statement up a line, so that it closes the "if" statement before the "j = j + 1" statement. _
      This is because now we want to add one to the "j" variable (i.e., move to the next available row in Sheet2) regardless of whether the cell in the "codes" sheet is blank or not.
       j = j + 1 'Then we add one to the "j" variable so the next time it copies, we will be on the next available row in Sheet2.
 next i 'This triggers the end of the loop and moves on to the next value of "i".

如果您不需要格式化,我将使用以下格式。它所做的只是将您在工作表上指定的范围复制到一个变量,循环遍历该变量,检查是否有空单元格,并放入您喜欢的字符串中。它又好又快。如果要保留格式,可以仅将特殊格式粘贴到输出范围

Sub CopyNonBlankCells(rFromRange As Range, rToCell As Range, sSubIn As String)
    'You have three inputs.  A range to copy from (rFromRange), a range to copy to (rToCell) and a string to put in the blank cells.        

    Dim vData As Variant, ii As Integer, jj As Integer

   'Set to a variable since it's quicker
    vData = rFromRange.Value

    'Loop through to find the blank cells
    For ii = LBound(vData, 1) To UBound(vData, 1)   'Loop the rows
        For jj = LBound(vData, 2) To UBound(vData, 2)    'Loop the columns
            'Check for empty cell.  Quicker to use Len function then check for empty string
            If VBA.Len(vData(ii, jj)) = 0 Then vData(ii, jj) = sSubIn
        Next jj
    Next ii

    'Output to target cell.  Use the 'With' statement because it makes the code easier to read and is more efficient
    With rToCell.Parent
        .Range(.Cells(rToCell.Row, rToCell.Column), .Cells(rToCell.Row + UBound(vData, 1) - 1, rToCell.Column + UBound(vData, 2) - 1)).Value = vData
    End With

End Sub
并称之为:

Call CopyNonBlankCells(Sheets("codes").Range("A5:A100"), Sheets("Sheet2").Range("B28"), "Non-blank")
简单:

  Sheet1.Range("A1:a500").SpecialCells(xlCellTypeConstants).Copy Sheet2.Range("b2")
我使用了xlCellTypeConstants,但还有许多其他的可能性


表1通常相当于表1。第一个是VBE程序员视图中的名称,第二个是用户界面用户视图中的名称。我通常更喜欢第一种语法,因为它较短,允许用户重命名工作表而不影响代码。

非常感谢您抽出时间……这是我第一次使用宏和excel工作表,我三天前开始工作,截止日期是今天。EOD:所以我做了我想做的任何事情:D…我会尝试这段代码,希望它会work@antnewbee不客气。我在一开始也经历了一段艰难的时光,我做的正是你上面所做的。我所写的代码/注释正好勾勒出我希望有人在我刚开始时会告诉我的内容。我不能代表其他人说话,但一旦以上概念为我所用,一切就变得简单多了。@从你的解决方案来看,这需要时间,因为你要一个单元格一个单元格地复制。它只复制值公式,格式,e.t.c.丢失。但是对于初学者的评论+1实际上+1对于评论:@Loppside:是的,现在几乎完成了:现在我所要做的就是动态地选择图表的范围…顺便说一句: