-
Dec 5th, 2013, 05:51 AM
#1
Thread Starter
Member
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.
-
Dec 5th, 2013, 06:22 AM
#2
Re: 12 interval of Excel output
Moved From The CodeBank (which is for sharing code rather than posting questions )
-
Dec 5th, 2013, 06:48 PM
#3
Re: 12 interval of Excel output
Also dupe of another thread....hard to keep up with this guy!
-
Dec 6th, 2013, 01:14 AM
#4
Thread Starter
Member
Re: 12 interval of Excel output
-
Dec 6th, 2013, 03:02 AM
#5
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
-
Dec 6th, 2013, 05:30 AM
#6
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|