Results 1 to 23 of 23

Thread: [RESOLVED] Questions On Formatting Recordset Output In Excel

  1. #1

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

    Resolved [RESOLVED] Questions On Formatting Recordset Output In Excel

    Reference this thread which I monitored closely as it evolved.

    I'm using the code that DKenny supplied in that thread to output a recordset to a newly created Excel Worksheet, and the recordset data is displaying just fine. I have questions on formatting.

    1. My first column is a number, which by default in Excel, is right justified. I need to left justify it. I thought converting the recordset element to a string before dumping it to Excel would do the trick, but it has not. How do I left justify this number?

    2. I need to print the report in Landscape. What would I use ensure this would happen (I know I can do it through the page setup in Excel, but I'd like to pass the Landscape attribute to the worksheet when it gets created.)

    3. How do I turn the gridlines off programmatically?

    4. I will never know how many records are in the recordset, so I will never know how far down the page the records will go. Regardless of that, however, I need to print two totals two lines below the last line of the worksheet. How would I calculate in what row those totals should go?

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Questions On Formatting Recordset Output In Excel

    This should get you started.
    VB Code:
    1. Sub HackFormat()
    2. Dim oWin As Window
    3. Dim lTotalRow As Long
    4.    
    5.     With ThisWorkbook.Worksheets(1)
    6.        
    7.         'Q1 Left Align Column A
    8.         .Columns(1).HorizontalAlignment = xlLeft
    9.        
    10.         'Q2 Set Orintation to Landscape
    11.         .PageSetup.Orientation = xlLandscape
    12.        
    13.         'Q4 Find Last Used Row + 2 as Row number for Totals
    14.         lTotalRow = .Range("A1").End(xlDown).Row + 2
    15.        
    16.     End With
    17.    
    18.     'Q3 Turn off gridlines
    19.     For Each oWin In ThisWorkbook.Windows
    20.         oWin.DisplayGridlines = False
    21.     Next oWin
    22.    
    23. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

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

    Re: Questions On Formatting Recordset Output In Excel

    Thanks.

    DisplayGridLines doesn't seem to be a method of oWin. In fact, I get the "Method or data member not found" error message.

    The drop down intellisense for oWin reveals:

    DisplayHorzontialScrollbar
    DisplayLeftScrollbar
    DisplayRulers
    DisplayScreenTips
    DisplayVerticalRuler
    DisplayVerticalScrollbar

    But, no DisplayGridLines

    I have, as your example dictates, declared oWin As Window.

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Questions On Formatting Recordset Output In Excel

    Are you running this code in Excel VBA or in VB?

    If the answer is VB, it may be that 'Window' could represent a Windows Window. So, try changing the declaration to
    VB Code:
    1. Dim oWin As Excel.Window

    It's running fine for me in VBA.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

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

    Re: Questions On Formatting Recordset Output In Excel

    Quote Originally Posted by DKenny
    Are you running this code in Excel VBA or in VB?

    If the answer is VB, it may be that 'Window' could represent a Windows Window. So, try changing the declaration to
    VB Code:
    1. Dim oWin As Excel.Window

    It's running fine for me in VBA.
    I should have stipulated that I'm using VB6 (I have never developed an actual application in VBA). I'll try that.

    Thanks.

  6. #6

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

    Re: Questions On Formatting Recordset Output In Excel

    Excel.Window did the trick.

    Now, I have Q1, Q2 and Q3 working.

    Still mucking about with Q4, but I suspect that will just take a little playing around. I'm not getting any errors, but I'm also not having anything displayed.

    Stay tuned whilst I play...

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Questions On Formatting Recordset Output In Excel

    Hack
    For Q4 all I did was pass the row number to a variable. What do you need to achieve with that row?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8

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

    Re: Questions On Formatting Recordset Output In Excel

    Quote Originally Posted by DKenny
    Hack
    For Q4 all I did was pass the row number to a variable. What do you need to achieve with that row?
    The last two rows on my recordset contain dollar amounts. Via SQL, when I create my recordset, I SUM the two fields that hold the dollar amouts for a total dollar amount for each of those two columns. (Not that is has any bearing, but they are estimated vs actual dollar amounts for a budgeting program that I'm working on).

    What I need to do is calculate two rows below the last row of my recordset and insert those totals there (I have the totals stored in Long variables). The recordset will always contain the same 6 data elements, but the number or records will vary depending on what the user selects as query criteria when my SQL SELECT is run.

    The estimate and actual amounts and totals will always be in column's E and F, but the row that the totals will be displayed on will vary depending on the number of records in my recordset.

  9. #9
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Questions On Formatting Recordset Output In Excel

    Here's the revised code, you will need to ammend this to refer to your variable names.

    VB Code:
    1. Sub HackFormat()
    2. Dim oWin As Excel.Window
    3. Dim lTotalRow As Long
    4.    
    5.     With ThisWorkbook.Worksheets(1)
    6.        
    7.         'Q1 Left Align Column A
    8.         .Columns(1).HorizontalAlignment = xlLeft
    9.        
    10.         'Q2 Set Orintation to Landscape
    11.         .PageSetup.Orientation = xlLandscape
    12.        
    13.         'Q4 Find Last Used Row + 2 as Row number for Totals
    14.         lTotalRow = .Range("A1").End(xlDown).Row + 2
    15.            
    16.         'Insert Totals
    17.         .Cells(lTotalRow, 5).Value = lYourTotVariableForThe5thDataElement
    18.         .Cells(lTotalRow, 6).Value = lYourTotVariableForThe6thDataElement
    19.     End With
    20.    
    21.     'Q3 Turn off gridlines
    22.     For Each oWin In ThisWorkbook.Windows
    23.         oWin.DisplayGridlines = False
    24.     Next oWin
    25.  
    26. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  10. #10

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

    Re: Questions On Formatting Recordset Output In Excel

    I get an "Application defined or object defined error" on
    VB Code:
    1. bkWorkBook.Worksheets(1).Cells(lTotalRow, 5).Value = lngEstAmt
    bkWorkBook is declared as WorkBook

  11. #11

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

    Re: Questions On Formatting Recordset Output In Excel

    Also, I'm using this line of code
    VB Code:
    1. shWorkSheet.Columns("A:BZ").AutoFit
    to make sure everything displays properly.

    It works just fine for everything except one recordset element, and that is the one that contains the Comments field. Comments is stored in a varchar field in my SQL Server 2000 database, and I need to display them on this particular report. However, when I do display them, the entire comments field is strung out in one, long, continuous string. I suspect it is because of the AutoFit thing.

    How can I display the Comments (which could be a couple or more paragraphs in length) as a block of text rather than one, long, continuous string?

  12. #12
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Questions On Formatting Recordset Output In Excel

    For the Comments column, after you use the autofit method for the entire range, you should set the WrapText property and then set the column width to your desired value.

    I would include all this within a WITH block for the worksheet as above.
    VB Code:
    1. With .Columns(10) 'Change to the Col num for you Comments field
    2.             .WrapText = True
    3.             .ColumnWidth = 12 'Change to the desired Width
    4.         End With

    On the other issue, can you run to the error and see if lTotalRow has a value?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  13. #13

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

    Re: Questions On Formatting Recordset Output In Excel

    Quote Originally Posted by DKenny
    On the other issue, can you run to the error and see if lTotalRow has a value?
    I'll play with the comments things later. This is more important.

    After reading your question I looked back at what I had and couldn't see anywhere that it was getting any kind of value (other than zero of course) and after stepping through the code, I discover this is the case.

    When it hits the .Cells(Cells(lTotalRow, 5).Value = lngEstAmt, lTotalRow = 0

  14. #14
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Questions On Formatting Recordset Output In Excel

    In my original code I had the following line
    VB Code:
    1. 'Q4 Find Last Used Row + 2 as Row number for Totals
    2.         lTotalRow = .Range("A1").End(xlDown).Row + 2

    This line finds the last used row in Column A and the moves two rows down. I am assuming that your RS has a value for each record in the first field.


    EDIT: I'm still thinking in VBA, you may need to add a declaration for xlDown
    VB Code:
    1. Const xlDown as Long = -4121
    Last edited by DKenny; May 8th, 2006 at 10:31 AM.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Questions On Formatting Recordset Output In Excel

    If the sheet is newly created, you can set it using the UsedRange (it apparently tends to have issues, which is why the "xlDown" version is often better), eg:
    VB Code:
    1. lTotalRow = .UsedRange.Rows.Count



    EDIT: I'm still thinking in VBA, you may need to add a declaration for xlDown
    If a reference has been set, no constant declarations are required (if there is no reference, MS has a VB module with all the constant declarations in).

  16. #16

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

    Re: Questions On Formatting Recordset Output In Excel

    Quote Originally Posted by DKenny
    In my original code I had the following line
    VB Code:
    1. 'Q4 Find Last Used Row + 2 as Row number for Totals
    2.         lTotalRow = .Range("A1").End(xlDown).Row + 2

    This line finds the last used row in Column A and the moves two rows down. I am assuming that your RS has a value for each record in the first field.


    EDIT: I'm still thinking in VBA, you may need to add a declaration for xlDown
    VB Code:
    1. Const xlDown as Long = -4121
    Nope. Your VBA code worked just fine. For reasons I can't remember, that line was commented out. The only thing I can think of is that I commented it out because something wasn't working, and then when the "something" got fixed, I forgot to uncomment it.

    Anyway, with that line running, it works just fine. I just ran it through 6 or 7 different tests selecting different things each time so my query would produce recordsets of varying lengths and no matter how many, or how few, records were returned, the totals were always two lines below that last one.

    Yes! *dances*

    Now, I'm going to play around with my comments thing, but the original 4 questions are now resolved. Thank you sir!

  17. #17
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Questions On Formatting Recordset Output In Excel

    Just glad I could help.
    Now, don't forget to mark the thread as resolved, Moe
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  18. #18

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

    Re: Questions On Formatting Recordset Output In Excel

    Quote Originally Posted by DKenny
    For the Comments column, after you use the autofit method for the entire range, you should set the WrapText property and then set the column width to your desired value.

    I would include all this within a WITH block for the worksheet as above.
    VB Code:
    1. With .Columns(10) 'Change to the Col num for you Comments field
    2.             .WrapText = True
    3.             .ColumnWidth = 12 'Change to the desired Width
    4.         End With

    On the other issue, can you run to the error and see if lTotalRow has a value?
    Are these properties of the Workbook object or the Worksheet object? (I still haven't sorted out what properties belong to what object yet, so until I get comfortable with that, I'm staying away from With/End With.)

  19. #19
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Questions On Formatting Recordset Output In Excel

    Columns is a property of the Worksheet object and it returns a collection.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  20. #20

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

    Re: Questions On Formatting Recordset Output In Excel

    Quote Originally Posted by DKenny
    Columns is a property of the Worksheet object and it returns a collection.
    When I do a
    VB Code:
    1. bkWorkBook.Worksheets(1).Columns (12)
    I'm told the "Object does not support this property or method."

  21. #21
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Questions On Formatting Recordset Output In Excel

    You are referencing the 12th member of the columns collection, but you are not doing anything with it (i.e. you are not calling a method or referencing a property of the column).
    Try
    VB Code:
    1. bkWorkBook.Worksheets(1).Columns(12).WrapText = True
    2. bkWorkBook.Worksheets(1).Columns(12).ColumnWidth = 12 'Change to the desired Width
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  22. #22

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

    Re: Questions On Formatting Recordset Output In Excel

    Quote Originally Posted by DKenny
    You are referencing the 12th member of the columns collection, but you are not doing anything with it (i.e. you are not calling a method or referencing a property of the column).
    Try
    VB Code:
    1. bkWorkBook.Worksheets(1).Columns(12).WrapText = True
    2. bkWorkBook.Worksheets(1).Columns(12).ColumnWidth = 12 'Change to the desired Width
    Geez oh boy...now I know how the noobs feel in ClassicVb.

  23. #23

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

    Re: [RESOLVED] Questions On Formatting Recordset Output In Excel

    I am cocked, locked and ready to rock.

    Thank you sir!

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