|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
How to insert blank row in Excel via VB?
Hi all, I have folowing code, and I want it to insert 2 blank rows before new bank name appears in excelsheet. Output should be like this in Excel:
ING ING blank row blank row RoyalBank RoyalBank RoyalBank blank row blank row ================= Dim p As Integer Dim getTT As String ' select all bank names from BANK_ACCOUNT table getReport = "SELECT BANK_NAME FROM BANK_ACCOUNT" Set rs = cn.Execute(getReport) Do While Not rs.EOF ' Loop the Bank name from Bank_Account For p = 0 To rs.EOF getTT = rs.Fields(p) Dim rs2 As New ADODB.Recordset Dim getReport2 As String ' get all data(s) from REPORT table according to bank name getReport2 = "SELECT * FROM REPORT WHERE BANK_NAME='" & getTT & "' " 'ORDER BY ACCOUNT_TYPE" Set rs2 = cn.Execute(getReport2) .Cells(6, 1).CopyFromRecordset rs2 'Copy recordset to Excel file starting on row 6 and col 1. ' Make every columns autofit to the data(s) .Range(.Cells(4, 1), .Cells(20, 8)).Select xlsApp.Selection.Columns.AutoFit ' here should insert two blank rows, butit doesn't work .cells(i+6, 1).entirerow.insert Next rs.MoveNext Loop |
|
#2
|
|||
|
|||
|
The following selects rows 1 to 2, and inserts blank rows
Code:
Rows("1:2").Select
Selection.Insert shift:=xlDown
__________________
How can I soar like an eagle when I'm flying with turkey's? |
|
#3
|
|||
|
|||
|
No ... it doesn't work
![]() |
|
#4
|
||||
|
||||
|
Hb... I just gotta ask... it doesn't look like anything is working for you in Excel?
__________________
Fisherman "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - A.Einstein |
|
#5
|
|||
|
|||
|
It did appears blank rows. But not what i want it that way
![]() |
|
#6
|
||||
|
||||
|
Hey HB.. it's me again.. Here's what I think you should do in this instance...
Instead of copyfromrecordset... why don't you loop until rs.eof and place your information in the field where you would like it using counter variables? an example would be dim intI as integer dim intJ as integer dim rsRecordset as ADODB.Recordset dim cnConnection as ADODB.Connection dim strSQL as string dim XLApp as excel.application dim xlBook as excel.workbook dim xlSheet as excel.worksheet set rsrecordset = new adodb.recordset set cnconnection = new adodb.connection set xlapp = createobject("excel.application") cnconnection.connectionstring = "CONNECTIONSTRING" cnconnection.open strSQL = "SQL STRING" rsrecordset.open strsql, cnconnection,,, inti=1 intj=1 do until rsrecordset.eof = true for intj = 0 to (rsrecordset.fields.count) 'Declare Excel cells as xlapp.activesheet.cells(rowindex,columnindex) xlapp.activesheet.cells(inti, intj) = rsrecordset.fields.item(intj) next intj inti = inti + 1 rsrecordset.movenext loop A structure such as that should put your fields into particular fields. The advantage to doing that, is that if you want to move two rows ahead, then you simply increase inti by 2 before you enter the loop again. |
|
#7
|
|||
|
|||
|
Thanks
))))))) It works with your sample code ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > How to insert blank row in Excel via VB? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|