我發現了一些 VBA 可以幫助我做我想做的事情,其中包括以下內容:
With ThisWorkbook.Sheets(TargetSh)
NxtEmptyRw = .Cells(65536, 1).End(xlUp).Row + 1
.Cells(NxtEmptyRw, 1).Value = ActiveWorkbook.Sheets(SourceSh).Range("C2").Value
.Cells(NxtEmptyRw, 2).Value = ActiveWorkbook.Sheets(SourceSh).Range("C3").Value
.Cells(NxtEmptyRw, 3).Value = ActiveWorkbook.Sheets(SourceSh).Range("G2").Value
.Cells(NxtEmptyRw, 4).Value = ActiveWorkbook.Sheets(SourceSh).Range("G3").Value
End With
End Sub
我將如何調整引用 的行G2
,以便它返回 下的值C2
,而不是沿著同一行進行 - 有效地創建一個兩行兩列的表,而不是一個一行四列的表?
答案1
我更喜歡這樣更短的解決方案:
With ThisWorkbook.Sheets(TargetSh)
NxtEmptyRw = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
ActiveWorkbook.Sheets(SourceSh).Range("C2:C3,G2:G3").Copy
.Cells(NxtEmptyRw, 1).PasteSpecial xlPasteValues, , , True
End With
答案2
代替:
.Cells(NxtEmptyRw, 1).Value = ActiveWorkbook.Sheets(SourceSh).Range("C2").Value
.Cells(NxtEmptyRw, 2).Value = ActiveWorkbook.Sheets(SourceSh).Range("C3").Value
.Cells(NxtEmptyRw, 3).Value = ActiveWorkbook.Sheets(SourceSh).Range("G2").Value
.Cells(NxtEmptyRw, 4).Value = ActiveWorkbook.Sheets(SourceSh).Range("G3").Value
和:
.Cells(NxtEmptyRw, 1).Value = ActiveWorkbook.Sheets(SourceSh).Range("C2").Value
.Cells(NxtEmptyRw, 2).Value = ActiveWorkbook.Sheets(SourceSh).Range("C3").Value
.Cells(NxtEmptyRw + 1, 1).Value = ActiveWorkbook.Sheets(SourceSh).Range("G2").Value
.Cells(NxtEmptyRw + 1, 2).Value = ActiveWorkbook.Sheets(SourceSh).Range("G3").Value