Results 1 to 6 of 6

Thread: 12 interval of Excel output

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2013
    Posts
    54

    12 interval of Excel output

    I have this code below with a loop, outputting 117 records into excel. I want to output 12 records then skip a line, another 12 then skip a line to the finish of all the data onto the same excel sheet. please help.

    Code:
    MySQL = "SELECT convert(datetime, RunMonth + '01', 112) as EffectiveMonth, SalaryBill, Rate " _
        & "From Hpacc4 " _
        & "where Scheme = '" & frmLogin.MaskEdBox1.Text & "' " _
        & "AND convert(datetime, RunMonth + '01', 112) <= '" + RunMonthDate + "01 ' " _
        & "AND convert(datetime, RunMonth + '01', 112) > DATEADD(yyyy,-5, convert(datetime, '20130201', 112)) " _
        & "AND AccCode = '110' order by EffectiveMonth "
            
            With rsHPData
                .CursorLocation = adUseClient
                .LockType = adLockReadOnly
                .Open MySQL, cnHPtest, adOpenForwardOnly, adLockReadOnly
            End With
    
            numRecs = rsHPData.RecordCount
             
             With oWS
               'SET THE TOP ROWS WITH TITLES--Change Font to Bold
                .Range("A2:C3").Font.Bold = True 'sets top row (stuff below) in bold print
                  .Cells(2, 1).Value = "Month"
                  .Cells(2, 2).Value = "Payroll"
                  .Cells(2, 3).Value = "Rate"
             'Run through the RECORDSET, stating in ROW 2, until end of the RECORDSET
             For x = 3 To numRecs + 1
                  .Cells(x, 1).Value = Format(rsHPData!EffectiveMonth, "mmmm-yyyy")
                  .Cells(x, 2).Value = Trim(rsHPData!SalaryBill)
                  .Cells(x, 3).Value = Trim(rsHPData!Rate)
                  
                rsHPData.MoveNext  'Move through the RECORDSET
    
                  'For
                  ' I think twelve intervals has to come here
                  'Next 
    
             Next x
             End With
             'This for-loop makes the columns just wide enough for the largest 'string' in each column
             For x = 1 To 3 'where 3, in my case is three columns  (State Name, State Abbreviation and Date Entered Union
                  oWS.Columns(x).AutoFit
               Next x
            'close down the rs and connection
            rsHPData.Close
            cnHPtest.Close
            oExcel.Visible = True  'so you can see what you did
            'set up the active excel sheet
            Set oWS = oExcel.ActiveSheet
            Set oWB = oExcel.ActiveWorkbook
            oWB.SaveAs FileName:=App.Path + "\Recon.xlsx"  'use whatever name you want here
        Screen.MousePointer = vbDefault
    Last edited by stahorse; Dec 5th, 2013 at 06:07 AM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: 12 interval of Excel output

    Moved From The CodeBank (which is for sharing code rather than posting questions )

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: 12 interval of Excel output

    Also dupe of another thread....hard to keep up with this guy!

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2013
    Posts
    54

    Re: 12 interval of Excel output

    New Thread

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: 12 interval of Excel output

    try like
    Code:
             
    rw = 3
    For x = 3 To numRecs + 1
                  If (x - 3) Mod 12 = 0 Then rw = rw + 1
                  .Cells(rw, 1).Value = Format(rsHPData!EffectiveMonth, "mmmm-yyyy")
                  ' rest of code, change all instances of x to rw
                  rw = rw + 1
    next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2013
    Posts
    54

    Re: 12 interval of Excel output

    ok thanks for all the help, and I will

Posting Permissions

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



Click Here to Expand Forum to Full Width