添加另一个常量/列到这个macros?

现在做什么是取什么input列A:E,并添加你列在列F,在它的末尾,保持A:E不变。 这使得它更容易,而不是复制和粘贴,但我想添加另一行,使A:F是常量,切换列G列。

例如,一旦它被输出,

A1,B1,C1,D1,E1,F1 A1,B1,C1,D1,E1,F2 A1,B1,C1,D1,E1,F3 etc. 

我只是想添加另一列来做到这一点

 A1,B1,C1,D1,E1,F1,G1 A1,B1,C1,D1,E1,F1,G2 A1,B1,C1,D1,E1,F1,G3 

这是我迄今为止。

 Dim LastRowIput As String With Sheets("Input") LastRowInput = .Cells(.Rows.Count, "C").End(xlUp).Row End With For I = 2 To LastRowInput Dim LastRowLoc As String With Sheets("Output") LastRowLoc = .Cells(.Rows.Count, "F").End(xlUp).Row + 1 End With Sheets("Input").Select Range("F2").Select Range(Selection, Selection.End(xlDown)).Select Selection.Copy Sheets("Output").Select Range("F" & LastRowLoc).Select ActiveSheet.Paste Sheets("Input").Select Range("A" & I & ":" & "E" & I).Select Application.CutCopyMode = False Selection.Copy Sheets("Output").Select Dim LastRow As String With ActiveSheet LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row + 1 End With Range("A" & LastRow).Select ActiveSheet.Paste Dim LastRowLoc2 As String With Sheets("Output") LastRowLoc2 = .Cells(.Rows.Count, "F").End(xlUp).Row End With Application.CutCopyMode = False Range("A" & LastRow & ":" & "E" & LastRowLoc2).Select Selection.FillDown Sheets("Input").Select Next I 

看来你想复制A:G从input到输出的行,在G中的每一行扩展A:F。

 Dim i As Long, lastRowInput As Long, nextRowOutput As Long Dim wso As Worksheet Set wso = Worksheets("Output") With Sheets("Input") lastRowInput = .Cells(.Rows.Count, "C").End(xlUp).Row For i = 2 To lastRowInput nextRowOutput = wso.Cells(.Rows.Count, "G").End(xlUp).Row + 1 .Range(.Cells(2, "G"), .Cells(2, "G").End(xlDown)).Copy _ Destination:=wso.Cells(nextRowOutput, "G") .Range("A" & i & ":" & "F" & i).Copy _ Destination:=wso.Range(wso.Cells(nextRowOutput, "A"), _ wso.Cells(wso.Cells(.Rows.Count, "G").End(xlUp).Row, "F")) Next i End With 

我删除了所有涉及Range .Select和Range.Activate方法的方法,以支持直接引用。

Input_Sample_Data
input工作表中的示例数据

Input_Sample_Data_expanded_to_Output
输出工作表的示例结果