Results 1 to 6 of 6

Thread: how to bold rows/cells in excel from VB

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904

    how to bold rows/cells in excel from VB

    I have a VB app that populates an excel spreadsheet very successfully, but now I want to do some formatiing and haven't a clue how.

    I've done

    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlDataSheet As Excel.Worksheet
    Dim xlSumSheet As Excel.Worksheet
    Set xlApp = New Excel.Application
    Set xlBook = xlApp.Workbooks.Open _
    ("c:\gandalf\template.xls", , True)
    Set xlDataSheet = xlBook.Worksheets("Data")
    Set xlSumSheet = xlBook.Worksheets("Sums")

    to open up the excel file and a couple of worksheets, and I push data into them with no problem and can close and save, so my content is where I want it and now I'm interested in cosmetics.

    I'd like to have row 3 in the "Data" sheet become all bold, and I'd like cell (5,2) in the Sums sheet to become bold

    I'd appreciate any guidance.

  2. #2
    khalik
    Guest
    Range("E6:E10").Select
    Selection.Font.Bold = True
    change the cell coordinates

  3. #3
    Si_the_geek
    Guest
    You need to precede Khalik's code with the appropriate objects, in your current example it would be:

    VB Code:
    1. xlDataSheet.Range("3:3").Select
    2. xlApp.Selection.Font.Bold = True
    3. xlSumSheet.Range("E2").Select
    4. xlApp.Selection.Font.Bold = True

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904

    still can't get range to go bold

    sure sounds like the right thing to do, and I apprecieate the guidance, but I can't get it to work:

    xlDataSheet.Range("B2:B20").Select

    gets me "select method of range class failed"

    so does

    xlDataSheet.Range("2:2").Select

    and

    xlDataSheet.Range("2:20").Select

    on the other hand,

    xlDataSheet.Range("2").Select

    gets me "method range of objext worksheet failed"

    clearly I'm missing something fundamental, but have no idea what it is.

  5. #5
    Si_the_geek
    Guest
    the error "method range of object worksheet failed" means you have specified an invalid range (to select a row you need "n:n", where n is the row number). The other error means (in this situation at least) that you cannot select ANYTHING on that sheet at the moment.

    you may have to get the worksheet activated first, eg:

    xlDataSheet.Activate

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    activate was it, Si.

    thanks very much for the help !

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