[英]Copy and Paste Excel Macro for only rows with data
我需要幫助找出一個宏,以便僅將包含數據(而不是較大的設置范圍)的行中的值復制並粘貼到另一個工作表中。
在名為DataEntry
的工作表中,在A到J列中輸入停機時間(可能有2行數據或可能有50行),並且僅將值(而不是公式)粘貼到另一個名為DataCombined
工作表中,並粘貼到A列和下一可用行。
請幫忙!
您的問題有點令人困惑,但這可能是您想開始的地方:(如果可以,請提供帶有所需輸出的示例表,這將有所幫助)
Sub copyRange()
Dim dataSht As Worksheet, comboSht As Worksheet
Set dataSht = Sheets("DataEntry")
Set comboSht = Sheets("DataCombined")
Dim rngToCopy As Range
Set rngToCopy = dataSht.Range(Cells(2, 1), Cells(getLastFilledRow(dataSht), "J"))
rngToCopy.Copy
comboSht.Range("A" & (getLastFilledRow(comboSht) + 1)).PasteSpecial xlPasteValues
rngToCopy = dataSht.Range(Cells(2, 1), Cells(getLastFilledRow(dataSht), "J")).SpecialCells(xlCellTypeConstants)
rngToCopy.ClearContents
End Sub
'Gets the last row of the worksheet that contains data
Public Function getLastFilledRow(sh As Worksheet) As Integer
On Error Resume Next
getLastFilledRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.