将其他工作表的行添加到主工作表

从字面上看,二十年来我第一次做了这个,甚至超级基础(没有双关语意思)。 我有Sheet1 ("Main Page") ,我正在根据Column Binput的数据从("Control Sequences")复制数据。 我有它的工作。 我遇到的问题是当我复制第一组数据,然后想要引入另一组数据时,代码再次运行整个表单,并覆盖之前做的任何调整。 我希望能够将一组数据引入到Sheet1 ,手动跳过几行,在下面的column B键入另一个值,重新运行代码并添加新的数据。我将尝试出现如果这样做没有意义的话,更简单的解释。 大脑在VBA吸收5小时之后就被炒了出来:P这里是我迄今为止完整的代码(这是一种powershell的方式,所以要小心):

 Sub test() Dim i As Integer 'Main Page Sheet Row Number Dim n As Integer 'Control Sequences Sheet Row Number Dim x As Integer 'Main Page Current Row Number Dim y As Integer 'Column Number Dim CSrow As Integer 'Current Row Dim NextCS As Integer 'Next Control Sequence Dim NextCSrow As Integer 'Row To Stop At Dim ws1 As Worksheet 'Var Dim ws2 As Worksheet 'Var Set ws1 = Worksheets("Main Page") Set ws2 = Worksheets("Control Sequences") y = 2 'Cycles through the codes in sheet 1 For i = 2 To ws1.Cells(ws1.Rows.Count, y).End(xlUp).row Step 1 For n = 2 To ws2.Cells(ws2.Rows.Count, y).End(xlUp).row Step 1 If ws1.Cells(i, y).Value = ws2.Cells(n, y).Value Then x = i CSrow = ws2.Cells(n, y).row NextCS = ws1.Cells(i, y).Value + 1 NextCSrow = Application.WorksheetFunction.Match(NextCS, ws2.Range("B1:B200"), 0) NextCSrow = NextCSrow - 1 For CSrow = CSrow To NextCSrow y = y + 1 For y = 3 To 7 ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value Next y ' ws1.Cells(x, 8).Formula = ws2.Cells(CSrow, 8).Formula y = y + 1 ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value y = y + 2 ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value x = x + 1 y = 2 Next CSrow End If Next n Next i End Sub 

感谢任何人的帮助和input。

编辑2014年2月13日
正如在下面的答案的评论中所提到的,我拿出了.End(xlUp)一部分,它的工作。 我也改变了写作循环的主体:

  For CSrow = CSrow To NextCSrow ' y = y + 1 ' For y = 3 To 7 ' ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value ' Next y ' ws1.Cells(x, 8).Formula = ws2.Cells(CSrow, 8).Formula ' y = y + 1 ' ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value ' y = y + 2 ' ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value ' x = x + 1 ' y = 2 ws2.Rows(CSrow).Copy Destination:=ws1.Cells(x, 1) x = x + 1 Next CSrow` 

我有格式和公式复制,而不保留原始的参考:D对第四部分…testing所有的variables,而不是1;)我将继续更新这个线程…以及。 .. 更新。

编辑2014年2月20日这是完整的代码,因为它现在:

 Sub test() Dim i As Long 'Main Page Sheet Row Number Dim j As Long 'Placeholder Dim n As Long 'Control Sequences Sheet Row Number Dim x As Long 'Main Page Current Row Number Dim y As Long 'Column Number Dim z As Long Dim a As Long Dim CSrow As Long 'Current Row Dim NextCS As Long 'Next Control Sequence Dim NextCSrow As Long 'Row To Stop At Dim ws1 As Worksheet 'Var Dim ws2 As Worksheet 'Var Dim ws3 As Worksheet 'Var Dim ws4 As Worksheet 'Var ' Set ws1 = Worksheets("Main Page") Set ws1 = ActiveSheet Set ws2 = Worksheets("Control Sequences") Set ws3 = Worksheets("Cost 1") Set ws4 = Worksheets("Cost 2") If ws1.Name = ws2.Name Or ws1.Name = ws3.Name Or ws1.Name = ws4.Name Then End End If y = 2 z = 10 a = ws1.Cells(ws1.Rows.Count, z).End(xlUp).row + 2 If IsEmpty(ws1.Cells(a, y).Value) Then End 'Cycles through the codes in sheet 1 j = ws1.Cells(ws1.Rows.Count, y).End(xlUp).row i = ws1.Cells(j, y).row For i = i To j Step 1 For n = 2 To ws2.Cells(ws2.Rows.Count, y).End(xlUp).row Step 1 If ws1.Cells(i, y).Value = ws2.Cells(n, y).Value Then x = i CSrow = ws2.Cells(n, y).row NextCS = ws1.Cells(i, y).Value + 1 NextCSrow = Application.WorksheetFunction.Match(NextCS, ws2.Range("B1:B100"), 0) NextCSrow = NextCSrow - 1 For CSrow = CSrow To NextCSrow ' y = y + 1 ' For y = 3 To 7 ' ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value ' Next y ' ws1.Cells(x, 8).Formula = ws2.Cells(CSrow, 8).Formula ' y = y + 1 ' ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value ' y = y + 2 ' ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value ' x = x + 1 ' y = 2 ws2.Rows(CSrow).Copy Destination:=ws1.Cells(x, 1) x = x + 1 Next CSrow End If Next n Next i End Sub 

我添加了一个检查,如果用户在任何“模板”工作表,代码将停止。 这有点暴躁,但它完成了工作,这是我唯一的代码。 也许如果我继续这样做的话,我会尽量让它更“精简”。 :D感谢大家的意见和帮助。

我想我有它。 你的问题在你的循环的第一行:

 For i = 2 To ws1.Cells(ws1.Rows.Count, y).End(xlUp).row Step 1 

尝试在循环开始之前dynamic设置iDIM另一个variablesj为此,然后用以下代替上面的行:

 j = ws1.Cells(ws1.Rows.Count, y).End(xlUp).row i = ws1.Cells(j, y).End(xlUp).row For i = i to j Step 1 

当你在它的时候,把行整数改为long,因为工作表中的行数比整数可以处理的要多。