如果第一个单元格为空,则将其复制并粘贴到另一个第一个空行,粘贴到上一行之上 [英] Copy and paste to another sheet first empty row, pastes over the previous row if first cell is empty

查看:641
本文介绍了如果第一个单元格为空,则将其复制并粘贴到另一个第一个空行,粘贴到上一行之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一张纸( B14:I14 )复制范围,并将值粘贴到第一个空行中的另一张纸。如果范围具有第一个单元格中的数据( B14 ),则此操作正常。



当有一些数据的单元格,但不是B14,下一次它粘贴在最后一次执行宏时的同一行。



范围将随所有单元格包含数据而有所不同或只是两个。我需要它来检查目标行是否为空,而不仅仅是第一个单元格。



这是我的宏:

  Sub Save7()
Dim NextRow As Range
With Sheets(Sheet3)
Set NextRow = .Cells(.Rows.Count,2 ).End(xlUp).Offset(1,0)
结束
Sheet1.Range(B14:I14)。复制
NextRow.PasteSpecial粘贴:= xlValues,Transpose:= False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub


解决方案

我建议使用这个:

  Sub Save7()
Dim NextRow As Range
Set NextRow = Range(B& Sheets(Sheet3)。UsedRange.Rows.Count + 1)
Sheet1.Range(B14:I14)。复制
Sheet3.Activate
NextRow.PasteSpecial粘贴:= xlValues,Transpose:= False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub

这应该很好UsedRange检查所有列,如果B为空,则不会覆盖它。唯一的问题是如果第一行是空白,但你可以只是添加标题数据在那里,或什么,它不会再被它打扰了。当第一行为空时,UsedRange.Rows.Count仍然输出1.享受!


I'm copying a range from one sheet (B14:I14) and pasting the values to another sheet in the first empty row. This works fine if the range has data in the first cell (B14).

When there is data in some of the cells but not B14, the next time it pastes over the same row as the last time I executed the macro.

The range will vary from having all cells containing data or just two. I need it to check that target row is empty, not just the first cell.

Here is my macro:

Sub Save7()
    Dim NextRow As Range
    With Sheets("Sheet3")
        Set NextRow = .Cells(.Rows.Count, 2).End(xlUp).Offset(1, 0)
    End With
    Sheet1.Range("B14:I14").Copy
    NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
    Application.CutCopyMode = False
    Set NextRow = Nothing
End Sub

解决方案

I'd suggest using this:

Sub Save7()
    Dim NextRow As Range
    Set NextRow = Range("B" & Sheets("Sheet3").UsedRange.Rows.Count + 1)
    Sheet1.Range("B14:I14").Copy
    Sheet3.Activate
    NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
    Application.CutCopyMode = False
    Set NextRow = Nothing
End Sub

That should work well. UsedRange checks across all columns so if B is empty it won't overwrite it. Only problem is if the 1st row is blank, but you can just add header data there, or something and it won't be bothered by it ever again. When the first row is blank UsedRange.Rows.Count still outputs 1. Enjoy!

这篇关于如果第一个单元格为空,则将其复制并粘贴到另一个第一个空行,粘贴到上一行之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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