我发现一些 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