0

I am sanitizing contact data in an excel spreadsheet in order to bulk upload into SalesForce. SalesForce, however, can only manage worksheets 100 contacts long. I want a macro that can split up a 700 row worksheet, for example, into seven workbooks, each containing 100 cell references.

I did some research on how to go about doing this, and I believe this is the only reference on StackOverflow to aid in this end: Solution for Dividing WorkSheet into Multiple Files with vba/excel/c#

Also, this solution looks promising, but I don't understand enough VBA to grasp it just yet: https://superuser.com/questions/57157/can-i-split-a-spreadsheet-into-multiple-files-based-on-a-column-in-excel-2007

However, the selected answer doesn't really serve my ends. Can someone point me in the right direction / enumerate the commands for doing this?

Here's what I have so far-- this appears to produce the right number of workbooks. Now I just need to figure out how to cut and paste 100 rows into each one.

    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

Here's the code I'm using now:

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
4

2 回答 2

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
于 2013-06-05T20:59:06.320 回答
0

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

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

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

每组将有您接下来的一百条记录 - 将它们复制并粘贴到新工作簿中并根据需要保存。此外,从这一点来看,记录一个快速宏来完成最后一部分是一个微不足道的案例。

即使它不完全符合您的要求,但我希望它的简单性会有所帮助。

于 2013-06-05T19:20:51.003 回答