VBA宏根据工作表中的行数将一个工作表拆分为多个工作簿

时间:2013-06-05 18:41:40

标签: excel vba

我正在清理excel电子表格中的联系人数据,以便批量上传到SalesForce。但是,SalesForce只能管理工作表100个联系人的长度。我想要一个宏,可以将700行工作表拆分为七个工作簿,每个工作簿包含100个单元格引用。

我做了一些关于如何做到这一点的研究,我相信这是StackOverflow的唯一参考,以帮助实现这一目标: Solution for Dividing WorkSheet into Multiple Files with vba/excel/c#

此外,这个解决方案看起来很有希望,但我还不明白VBA还没有掌握它:https://superuser.com/questions/57157/can-i-split-a-spreadsheet-into-multiple-files-based-on-a-column-in-excel-2007

但是,所选答案并不能真正实现我的目的。有人能指出我正确的方向/列举这样做的命令吗?

这是我到目前为止所做的 - 这似乎产生了正确数量的工作簿。现在我只需要弄清楚如何将100行剪切并粘贴到每一行中。

    Sub Macro12()

    Dim wb As Workbook
    Dim p As Double
    Dim p_quotient As Double
    Application.ScreenUpdating = False

    p = ActiveSheet.UsedRange.Rows.Count
    p_quotient = p / 100
    p_quotient = Application.WorksheetFunction.RoundUp(p_quotient, 0)

    For i = 1 To p_quotient
        Workbooks.Add
        Set wb = ActiveWorkbook
        ThisWorkbook.Activate
        wb.SaveAs ThisWorkbook.Path & "test" & i
        wb.Close

    Next i
    Application.ScreenUpdating = True
    Set wb = Nothing


End Sub

这是我现在使用的代码:

Sub Macro12()

  Dim wb As Workbook
      Dim ThisSheet As Worksheet
      Dim NumOfColumns As Integer
      Dim RangeToCopy As Range
      Dim WorkbookCounter As Integer
      Dim myDate As String
      myDate = Format(Date, "yyyy.mm.dd")

      Set ThisSheet = ThisWorkbook.ActiveSheet
      NumOfColumns = ThisSheet.UsedRange.Columns.Count
      WorkbookCounter = 1


      For p = 1 To ThisSheet.UsedRange.Rows.Count Step 101
        Set wb = Workbooks.Add

        Set RangeToCopy = ThisSheet.Range(ThisSheet.Cells(p, 1), ThisSheet.Cells(p + 100, NumOfColumns))
        RangeToCopy.Copy wb.Sheets(1).Range("A1")

        wb.SaveAs ThisWorkbook.Path & "\Salesforce Lead Conversion " & myDate & " Part " & WorkbookCounter & ".xls"
        wb.Close
        WorkbookCounter = WorkbookCounter + 1
      Next p

      Application.ScreenUpdating = True
      Set wb = Nothing

  End Sub

2 个答案:

答案 0 :(得分:2)

按照你的逻辑,这应该这样做:

  Sub Macro12()

      Dim wb As Workbook
      Dim ThisSheet As Worksheet
      Dim NumOfColumns As Integer
      Dim RangeToCopy As Range
      Dim WorkbookCounter As Integer

      Application.ScreenUpdating = False


      Set ThisSheet = ThisWorkbook.ActiveSheet
      NumOfColumns = ThisSheet.UsedRange.Columns.Count
      WorkbookCounter = 1

      For p = 1 To ThisSheet.UsedRange.Rows.Count Step 101
        Set wb = Workbooks.Add

        Set RangeToCopy = ThisSheet.Range(ThisSheet.Cells(p, 1), ThisSheet.Cells(p + 100, NumOfColumns))
        RangeToCopy.Copy wb.Sheets(1).Range("A1")

        wb.SaveAs ThisWorkbook.Path & "\test" & WorkbookCounter
        wb.Close
        WorkbookCounter = WorkbookCounter + 1
      Next p

      Application.ScreenUpdating = True
      Set wb = Nothing


  End Sub

答案 1 :(得分:0)

虽然有很多方法可以做到这一点(有些甚至非常容易使用VBA)我认为最简单的是不需要VBA就可以执行以下操作:

  • 添加额外的“帮助”栏
  • 在该列中,输入以下公式=FLOOR(ROW()/100,1)
  • 对数据进行过滤

现在,在帮助列中过滤每个数字0,1,2,3,...,7。

每个集合将包含您的下一百条记录 - 将它们复制并粘贴到新工作簿中并根据需要进行保存。此外,从这一点来看,记录一个快速宏来完成最后一部分是一件非常简单的事情。

尽管它并不完全符合您的要求,但我希望它的简单性有所帮助。