+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Thread: Split Excel workbooks in flat source

  1. #16
    Registered User
    Join Date
    05-10-2011
    Location
    Delhi, India
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Split Excel workbooks in flat source

    its done

    I just delete the else condition. and now it perfectly give me 40 files , with exact number I want

  2. #17
    Valued Forum Contributor
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    1,957

    Re: Split Excel workbooks in flat source

    Try this slight modification:

    Sub SplitIntoText()
    
    Const sStartCell = "A1"
    Const sHeaderText = "MOBILE"
    Const sFilePath = "C:\" 'Change to whichever directory you want the files in, ending with a \
    Const sFileName = "DATA"
    Const sFileExt = ".txt"
    
    Dim rngCurrentExport As Range
    Dim rngCellLoop As Range
    Dim avMobileNumbers As Variant
    Dim sDateText As String
    Dim lFileCounter As Long
    Dim lExportRows As Long
    Dim lLastRow As Long
    Dim iFileHandle As Integer
    Dim lMobLoop As Long
    
    avMobileNumbers = Array("980000001", "9900000002", "9700000003")
    lFileCounter = 0
    
    With ActiveSheet
    
      lLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
      While Trim(.Range("A" & lLastRow).Value) = ""
        lLastRow = lLastRow - 1
      Wend
      
      Set rngCurrentExport = Range(sStartCell)
      
      While rngCurrentExport.Row <= lLastRow
      
        lFileCounter = lFileCounter + 1
        Select Case lFileCounter
          Case Is < 10
            lExportRows = 2000
          Case 10 To 18
            lExportRows = 4000
          Case Else
            lExportRows = 7000
        End Select
        
        If rngCurrentExport.Offset(lExportRows - 1, 0).Row > lLastRow Then
          lExportRows = lLastRow - rngCurrentExport.Row
        End If
        
        iFileHandle = FreeFile
        
        Open sFilePath & Format(Now(), "ddmmm") & " " & sFileName & lFileCounter & sFileExt For Output As iFileHandle
          Print #iFileHandle, sHeaderText
          
          For lMobLoop = LBound(avMobileNumbers) To UBound(avMobileNumbers)
            Print #iFileHandle, avMobileNumbers(lMobLoop)
          Next lMobLoop
        
          For Each rngCellLoop In .Range(rngCurrentExport, rngCurrentExport.Offset(lExportRows - 1, 0)).Cells
            Print #iFileHandle, rngCellLoop.Value
          Next rngCellLoop
          
          For lMobLoop = LBound(avMobileNumbers) To UBound(avMobileNumbers)
            Print #iFileHandle, avMobileNumbers(lMobLoop)
          Next lMobLoop
          
        Close #iFileHandle
        
        Set rngCurrentExport = rngCurrentExport.Offset(lExportRows, 0)
        
      Wend
      
    End With
          
    End Sub

    The while loop after lLastRow is first set will exclude blank cells from the export.

  3. #18
    Registered User
    Join Date
    05-10-2011
    Location
    Delhi, India
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Split Excel workbooks in flat source

    awesome work dude
    (y)

    thanks a ton

  4. #19
    Valued Forum Contributor
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    1,957

    Re: Split Excel workbooks in flat source

    Happy to help - please mark this thread as "Solved" if everything is working now.

  5. #20
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Split Excel workbooks in flat source

    Wouldn't this suffice ?

    Sub snb()
     sn = Array(40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 5, 5, 5, 5, 5, 5, 5, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 2, 2, 2, 2)
     y=1
        
     For j = 1 To 40
      Open "E:\" & Format(Date, "ddmm_") & "data" & j & ".txt" For Output As #1
       Print #1, Replace("MOBILE|98 xxxxxxxxx|99 xxxxxxxxx|97 xxxxxxxxx|", "|", vbCrLf) & Join(Application.Transpose(Cells(y, 1).Resize(sn(j - 1) * 10 ^ 3)), vbCrLf) & Replace("|98 xxxxxxxxx|99 xxxxxxxxx|97 xxxxxxxxx", "|", vbCrLf)
      Close
       y = y + sn(j-1)*10^3
     Next
    End Sub
    Last edited by snb; 09-16-2011 at 10:42 AM.



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0