Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21127

Can I adjust the column width of an excel spreadsheet I export listview data into?

$
0
0
Hello there everyone. I am finishing up a prog that tracks student data, and now that I can put their data into a listview, I am hoping to be able to put it into an excel spreadsheet so teachers can save it for later reference.


I am using this code to basically open excel, and copy the listview data over, but the thing is, while I can adjust the column width of the listview columns no problem, is there a similar way to do it for excel once the spreadsheet is open? Or just make them just as wide as the ones in the listview? I am using this code to open excel, which works great!

VB Code:
  1. Private Sub SaveToExcelONEDAY_Click()
  2. Dim ExcelObj As Object
  3.     Dim ExcelBook As Object
  4.     Dim ExcelSheet As Object
  5.     Dim i As Integer
  6.  
  7.     Set ExcelObj = CreateObject("Excel.Application")
  8.     Set ExcelBook = ExcelObj.WorkBooks.Add
  9.     Set ExcelSheet = ExcelBook.WorkSheets(1)
  10.  
  11.     With ExcelSheet
  12.         For i = 1 To ResultsView.ColumnHeaders.Count
  13.             .cells(1, i) = ResultsView.ColumnHeaders(i).Text
  14.         Next i
  15.         For i = 1 To ResultsView.ListItems.Count
  16.             .cells(i + 1, 1) = ResultsView.ListItems(i).Text
  17.             .cells(i + 1, 2) = ResultsView.ListItems(i).SubItems(1)
  18.             .cells(i + 1, 3) = ResultsView.ListItems(i).SubItems(2)
  19.             .cells(i + 1, 4) = ResultsView.ListItems(i).SubItems(3)
  20.            
  21.             .cells(i + 1, 5) = ResultsView.ListItems(i).SubItems(4)
  22.             .cells(i + 1, 6) = ResultsView.ListItems(i).SubItems(5)
  23.             .cells(i + 1, 7) = ResultsView.ListItems(i).SubItems(6)
  24.  
  25.         Next
  26.     End With
  27.    
  28.     ExcelObj.Visible = True
  29. End Sub

But the only small issue is the columns sometimes look crowded unless I manually resize them, which is no big problem, but it would be more convenient for other teachers if it could do it when it opens. Has anyone any experience adjusting excel columns? Thanks!

Viewing all articles
Browse latest Browse all 21127

Trending Articles