Excel border issue not working at 100%

Hello all

I'm having this function where one of the thing will be to add borders in excel. But the thing is that it does only the first five rows and that's it.

Can you help me on this?


Sub FlexExcel(fg As MSHFlexGrid)
   Dim c As Integer, a As Integer
   Dim TotalRecs As Long, r As Long, cnt As Long
   Rem *** Get total number of records ***
   TotalRecs = fg.Rows - 1
   If TotalRecs = 0 Then
      MsgBox "Zero records to export", vbCritical
      On Error GoTo GoofedUp
      Rem *** Do da Excel magic! ***
      Dim AppExcel As Variant, txt
      Set AppExcel = CreateObject("Excel.application")
      AppExcel.Visible = False
      Rem *** Add column headers ***
      For c = 0 To fg.Cols - 1
         AppExcel.Cells(5, c + 1).Font.Bold = True
         AppExcel.Cells(5, c + 1).Formula = fg.TextMatrix(0, c)
         AppExcel.Cells(5, c + 1).Borders.Weight = 2
         AppExcel.Cells(5, c + 1).Interior.Color = RGB(205, 197, 191)
         AppExcel.Cells(6, c + 1).RowHeight = 2
      Next c
      r = 0
      Rem *** Add data ***
      While cnt < TotalRecs
         r = r + 1
         txt = FlexGet(fg, r)
         For c = 0 To fg.Cols - 1
            AppExcel.Range(Chr$(65 + c) & CStr(r + 6)) = ParseLine(txt, vbTab, c + 1)
         Next c
         cnt = cnt + 1
      Rem *** Add cell borders ***
      AppExcel.Range("A5:" & Chr$(64 + fg.Cols) & CStr(TotalRecs + 1)).Borders.Weight = 1
      Rem *** Resize all columns to width of their content ***
      Rem *** Set proper column alignment
      For c = 0 To fg.Cols - 1
         Select Case fg.ColAlignment(c)
            Case 0 To 2 'Left
               a = 2
            Case 3 To 5 'Center
               a = 3
            Case 6 To 8 'Right
               a = 4
            Case Else   'Contents
               a = 1
         End Select
         AppExcel.ActiveSheet.Columns(Chr$(65 + c)).HorizontalAlignment = a
      Next c
      AppExcel.Visible = True
   End If
   AppExcel.ActiveWindow.FreezePanes = True
   With AppExcel.ActiveWorkbook.ActiveSheet
   AppExcel.ActiveWorkbook.ActiveSheet.Range("a1") = "Periode d'acftivité: " & Form3.DTPicker1 & " - " & Form3.DTPicker2
    AppExcel.ActiveWorkbook.ActiveSheet.Range("a1").Font.Bold = True
    AppExcel.ActiveWorkbook.ActiveSheet.Range("a1").Font.Size = 17
    End With
    AppExcel.ActiveWorkbook.ActiveSheet.Range("a3") = "Date du rapport:"
    AppExcel.ActiveWorkbook.ActiveSheet.Range("a3").Font.Bold = True
    AppExcel.ActiveWorkbook.ActiveSheet.Range("B3") = Format(Date, "dddd dd mmmm yyyy")
    AppExcel.ActiveWorkbook.ActiveSheet.Range("b3").Font.Bold = True
    AppExcel.ActiveWorkbook.ActiveSheet.Range("D5").NumberFormat = "0"

   Exit Sub

   If Err.Number >= 1 Then
      MsgBox Err.Description, vbCritical, Err.Number
   End If

End Sub

Open in new window

LVL 11
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

James ElliottManaging DirectorCommented:
AppExcel.Range("A5:" & Chr$(64 + fg.Cols) & CStr(TotalRecs + 1)).Borders.Weight = 1

This is adding the borders, and the line number is defined by TotalRecs, which in turn is defined as

TotalRecs = fg.Rows - 1

fg is the parameter to the function and is a flexgrid, therefore I assume there are only 5 records in the flexgrid?

You could use something like this instead:

AppExcel.UsedRange.Borders.Weight = 1

Wilder1626Author Commented:
Hello jell

Well in th example of my first post, starting at row 5 i have 12 rows of data but only the first 5 have borders.

I have switch to this:
AppExcel.UsedRange.Borders.Weight = 1

But now, i have a runtime error 438
Object doesn't support this property or method
James ElliottManaging DirectorCommented:
Try replacing this:

AppExcel.Range("A5:" & Chr$(64 + fg.Cols) & CStr(TotalRecs + 1)).Borders.Weight = 1

with this:

AppExcel.Range("A5:" & Chr$(64 + fg.Cols) & CStr(r + 1)).Borders.Weight = 1
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Wilder1626Author Commented:
i just did another test and here is the excel result.
Wilder1626Author Commented:
I just tried like this:
AppExcel.Range("A5:" & Chr$(64 + fg.Cols) & CStr(r + 1)).Borders.Weight = 1

Open in new window

But still the same result has the excel file in my previous post.

This is strange.
James ElliottManaging DirectorCommented:
I've found the problem:

Use this:

AppExcel.Range("A5:" & Chr$(64 + fg.Cols) & CStr(TotalRecs + 6)).Borders.Weight = 1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wilder1626Author Commented:
Oh yes. This is perfect.

Thanks for your help and happy new year
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.