2017-03-07 205 views
0

我有第2页A列(从单元格A2开始)的ID列表。Excel VBA循环直到空白单元格并将工作表复制到新工作簿中

我试图创建一个宏来遍历每个ID#,将它复制到Sheet 1上的单元格A9中,然后将Sheet 3复制到新的工作簿中。

对于每个ID#,工作表3应该复制到同一个新工作簿下的不同工作表/选项卡下。

我不是一个编码器,所以我拥有的就是我能在Google上找到的东西,而且我似乎无法顺利完成所有工作。任何和所有的帮助,不胜感激。

这是我到目前为止..我无法弄清楚如何结束在空白单元格的循环,如何让复制工作表到新的工作簿后,宏恢复到源,然后如何将后续循环添加到现有的工作簿中。

Sub Test1() 
    Dim x As Integer 
    Application.ScreenUpdating = False 
    ' Set numrows = number of rows of data. 
    NumRows = Range("a2", Range("a2").End(xlDown)).Rows.Count 
    ' Select cell a2. 
    Range("a2").Select 
    ' Establish "For" loop to loop "numrows" number of times. 
    For x = 1 To NumRows 
    Sheets("Sheet 1").Range("A9").Value = ActiveCell 
     Sheets("Sheet 3").Copy 
    ' Selects cell down 1 row from active cell. 
    ActiveCell.Offset(1, 0).Select 
    Next 
    Application.ScreenUpdating = True 

末次

+2

请显示您拥有的任何代码,并描述您卡住或看到错误的位置。 –

+0

好吧,我尝试添加我到目前为止 –

+0

推测Sheet3有公式,它基于Sheet1 A9中的任何内容进行更新。当您复制Sheet3时,它仍然有一个链接回Sheet1 A9的公式,并且Sheet3副本中的其他公式也将根据源工作簿中的Sheet1 A9进行更新。您是否希望Sheet3副本仅作为值,以便它们不会链接回源工作簿? –

回答

0

没有太多的代码从ScreenUpdating左开,和明年。我已经评论了一些可能不明显的步骤,为什么要这样做。关于您可能不熟悉的事情,还有一些额外的评论。

Sub CopySheetsToNewWB() 
Dim ID_cell As Range 'will be used to control loop flow 
Dim SourceWB As Workbook 
Dim DestWB As Workbook 
Dim ControlSheet As Worksheet 'sheet with ID#s 
Dim IDsToCopy As Range 
Dim SheetToCopy As Worksheet 
Dim PathSeparator As String 
Dim SaveName As String 

    Application.ScreenUpdating = False 
    Set SourceWB = ThisWorkbook 
    'test if file saved on device/network or cloud and set separator 
    'because new file will be saved in same location 
    If InStr(1, SourceWB.Path, "\") > 0 Then 
     PathSeparator = "\" 
    Else 
     PathSeparator = "/" 
    End If 
    Set ControlSheet = SourceWB.Sheets("Sheet2") 
    Set SheetToCopy = SourceWB.Sheets("Sheet3") 
    With ControlSheet 
     Set IDsToCopy = Range(.[A2], .[A2].End(xlDown)) 
    End With 
    For Each ID_cell In IDsToCopy 
     'As ID_Cell is based on an IFERROR(...,"") formula, test if blank. 
     If ID_cell <> "" Then 
      With SourceWB 'allows subsequent commands without having to specify it 
       .Sheets("Sheet1").[A9] = ID_cell.Value2 
       'Test if DestWB already exists 
       If Not DestWB Is Nothing Then 
        'it's not nothing so it must be something (i.e. it exists) 
        SheetToCopy.Copy after:=DestWB.Sheets(DestWB.Sheets.Count) 
       Else 
        'create DestWB and save it in the same location as SourceWB 
        'using SourceWB name with date appended and SourceWB file extension. 
        'INSTR is similar to FIND in Excel but doesn't error if search 
        'string is not found - just returns 0. INSTRREV finds position of 
        'the last instance of searched string (in case of "."s in filename). 
        SaveName = .Path & PathSeparator & Left(.Name, InStr(1, .Name, ".") - 1) _ 
        & " as at " & _ 
        Format(Date, "yyyymmdd") & _ 
        Right(.Name, Len(.Name) - InStrRev(.Name, ".") + 1) 
        SheetToCopy.Copy 
        ActiveWorkbook.SaveAs Filename:=SaveName, FileFormat:=SourceWB.FileFormat 
        Set DestWB = ActiveWorkbook 
       End If 
      End With 
      'Copied sheet may have formulas linking to SourceWB so change to values 
      'and as it's still named "Sheet3", rename it after ID# 
      With DestWB.Sheets("Sheet3") 
       .UsedRange.Copy 
       .[A1].PasteSpecial xlPasteValues 
       .Name = ID_cell.Value2 
      End With 
     End If 
    Next 
    DestWB.Save 
    Application.ScreenUpdating = True 
End Sub 

所有变量的声明 - 你可以和应该设置你的VBA编辑器“要求变量声明”(下工具 - >选项)。这将在每个新模块的顶部插入“Option Explicit”。

没有“选择”或“激活”命令。通常可以通过使用With ... EndWith结构或完全限定对象来避免它们。

方括号范围参考 - [A2]与范围(“A2”)相同。

有任何问题,发表评论。

+0

非常感谢。我真的很感谢帮助!一个问题..在循环结束..我得到一个运行时错误1004 ..应用程序定义或对象定义错误...当我调试它指向.Name = ID_cell.Value2。在代码中......并且目标工作簿有一个额外的Sheet 3,带有#N/A错误。你怎么看? –

+0

它似乎并没有停在空白单元格(可能是因为它们仍然是该单元格中的公式?)我应该使用count函数来计算有多少个ID,然后使用#来告诉宏多少次循环? –

+0

我不认为你的ID号可能是公式派生的。什么公式?我需要为任何无效的公式结果(如空白,0或错误)添加测试。 –

相关问题