创build一个macrosbutton,将列中的特定范围复制到另一个具有增加的列号的表格中,以便我可以添加新的数据

我正在尝试创build一个macros来完成以下任务:

如果我在工作表Sheet1中,并在A列1-10行,我会点击一个button,它将数据传输到Sheet2列A.如果再次input数据,按下button,这次它转移到列B,然后再C等我已经创build了一个macros的副本,但我不知道如何使它不断改变输出列。 这是我迄今为止。

Sub details() Dim currentWB As String Dim futureWB As String currentWB = ActiveWorkbook.Name On Error Resume Next Sheets("temporarysheet").Delete On Error GoTo 0 Sheets.Add ActiveSheet.Name = "temporarysheet" Sheets("Sheet1").Select If ActiveSheet.AutoFilterMode Then Cells.Select On Error Resume Next ActiveSheet.ShowAllData On Error GoTo 0 End If Columns("B:B").Select Selection.Copy Sheets("temporarysheet").Select Range("A1").Select ActiveSheet.Paste Application.CutCopyMode = False If (Cells(1, 1) = "") Then lastrow = Cells(1, 1).End(xlDown).Row If lastrow <> Rows.Count Then Range("A1:A" & lastrow - 1).Select Selection.Delete Shift:=xlUp End If End If Columns("A:A").Select Columns("A:A").AdvancedFilter Action:=xlFilterCopy, _ CopyToRange:=Range("B1"), Unique:=True Columns("A:A").Delete Cells.Select Selection.Sort _ Key1:=Range("A2"), Order1:=xlAscending, _ Header:=xlYes, OrderCustom:=1, _ MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal lMaxSupp = Cells(Rows.Count, 1).End(xlUp).Row For suppno = 2 To lMaxSupp Windows(currentWB).Activate supName = Sheets("temporarysheet").Range("A" & suppno) If supName <> "" Then Workbooks.Add ActiveWorkbook.SaveAs supName futureWB = ActiveWorkbook.Name Windows(currentWB).Activate Sheets("Sheet1").Select Cells.Select If ActiveSheet.AutoFilterMode = False Then Selection.AutoFilter End If Selection.AutoFilter Field:=2, Criteria1:="=" & supName, _ Operator:=xlAnd, Criteria2:="<>" lastrow = Cells(Rows.Count, 2).End(xlUp).Row Rows("1:" & lastrow).Copy Windows(futureWB).Activate ActiveSheet.Paste ActiveWorkbook.Save ActiveWorkbook.Close End If Next Sheets("temporarysheet").Delete Sheets("Sheet1").Select If ActiveSheet.AutoFilterMode Then Cells.Select ActiveSheet.ShowAllData End If End Sub 

在OP规范之后编辑 ,仅复制值

你的代码几乎与你的问题无关

至于后者,你可以附加下面的macros到你的button:

 Option Explicit Sub copycolumn() Dim sourceSht As Worksheet: Set sourceSht = ThisWorkbook.Worksheets("Sheet1") '<--| set your source sheet Dim destSht As Worksheet: Set destSht = ThisWorkbook.Worksheets("Sheet2") '<--| set your destination sheet GetFirstEmptyCell(destSht, 1).Resize(10).Value = sourceSht.Range("A1:A10").Value '<--| copy range "A1:A10" from 'source' sheet to 'destination' sheet first empty column in row 1 End Sub Function GetFirstEmptyCell(sht As Worksheet, row As Long) As Range Set GetFirstEmptyCell = sht.Cells(row, sht.Columns.Count).End(xlToLeft) '<--| define the first non empty cell in passed row of passed worksheet If Not IsEmpty(GetFirstEmptyCell) Then Set GetFirstEmptyCell = GetFirstEmptyCell.Offset(, 1) '<--| if it's not empty then shift one column to the right End Function 

只需更改相关数据(图表名称,范围,行)以匹配您的实际数据

好吧,你的代码似乎是从input表复制到B列,而不是A列 – 我已经把你的代码,只是啾啾它:

 Sub details() Dim currentWB As String Dim futureWB As String currentWB = ActiveWorkbook.Name On Error Resume Next Sheets("temporarysheet").Delete On Error GoTo 0 Sheets.Add ActiveSheet.Name = "temporarysheet" Sheets("Sheet1").Select If ActiveSheet.AutoFilterMode Then Cells.Select On Error Resume Next ActiveSheet.ShowAllData On Error GoTo 0 End If Columns("A:A").Select ' Changed from Columns("B:B").Select Selection.Copy Sheets("temporarysheet").Select Range("XFD1").End(xlToLeft).Offset(0, 1).Select 'Changed to move to the column immediately to the right of the pasted data ActiveSheet.Paste Application.CutCopyMode = False If (Cells(1, 1) = "") Then lastrow = Cells(1, 1).End(xlDown).Row If lastrow <> Rows.Count Then Range("A1:A" & lastrow - 1).Select Selection.Delete Shift:=xlUp End If End If Columns("A:A").Select Columns("A:A").AdvancedFilter Action:=xlFilterCopy, _ CopyToRange:=Range("B1"), Unique:=True Columns("A:A").Delete Cells.Select Selection.Sort _ Key1:=Range("A2"), Order1:=xlAscending, _ Header:=xlYes, OrderCustom:=1, _ MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal lMaxSupp = Cells(Rows.Count, 1).End(xlUp).Row For suppno = 2 To lMaxSupp Windows(currentWB).Activate supName = Sheets("temporarysheet").Range("A" & suppno) If supName <> "" Then Workbooks.Add ActiveWorkbook.SaveAs supName futureWB = ActiveWorkbook.Name Windows(currentWB).Activate Sheets("Sheet1").Select Cells.Select If ActiveSheet.AutoFilterMode = False Then Selection.AutoFilter End If Selection.AutoFilter Field:=2, Criteria1:="=" & supName, _ Operator:=xlAnd, Criteria2:="<>" lastrow = Cells(Rows.Count, 2).End(xlUp).Row Rows("1:" & lastrow).Copy Windows(futureWB).Activate ActiveSheet.Paste ActiveWorkbook.Save ActiveWorkbook.Close End If Next Sheets("temporarysheet").Delete Sheets("Sheet1").Select If ActiveSheet.AutoFilterMode Then Cells.Select ActiveSheet.ShowAllData End If End Sub 

我没有看过你的代码的其余部分,但我认为它没问题(虽然它可以绝对清理!)