0

I'm newbie in Visual Basic and I'm tring to build a simply application who perform some operation on an Excel file.

I want to edit the cell's border properties of my sheet, I need to edit the weight and the color of the separate border of some specified cells (for example only the bottom border or the top border).

Ifound some interesting resource on the web: http://www.functionx.com/vbaexcel/cells/Lesson4.htm Border around each cell in a range http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/93bb7ff7-0aed-4ce1-adca-aabde5fc3c2c

anyway is impossible to me to follow the suggested example. This is an extract of my code:

Public Class mytest
Dim oExcel As Object 'Oggetto per la gestione del file Excel
Dim oBook As Object 'Oggetto per la gestione del file Excel
Dim page As Integer = 1 'Indice per la gestione dei fogli Excel
....

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    'Creazione nuovo workbook in Excel
    oExcel = CreateObject("Excel.Application")
    oBook = oExcel.Workbooks.Add

    'Add data to cells of the first worksheet in the new workbook

    'Apertura file in lettura
    Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("input.csv")
        MyReader.TextFieldType = FileIO.FieldType.Delimited
        'Imposto il carattere di separazione tra i campi
        MyReader.SetDelimiters(";")

        'Creo stringa lettura righe
        Dim currentRow As String()

        'Leggo 1 volta per saltare
        currentRow = MyReader.ReadFields()

        'Fino alla fine del file 
        While Not MyReader.EndOfData
            'Mostra riga nella label
            lblShowElab.Text = page
            Try
                'Formatto i fogli
                oBook.Worksheets(page).Range("A1:B1").Merge()
                oBook.Worksheets(page).Range("A2:B2").Merge()
    ...

                oBook.Worksheets(page).Range("B2").Borders(xlEdgeRight).LineStyle = xlContinuous
                oBook.Worksheets(page).Range("B2").Borders(xlEdgeRight).Weight = xlThin

                'Leggo riga per riga
                currentRow = MyReader.ReadFields()
                'Inserisco i campi di ogni riga nella cella voluta
                oBook.Worksheets(page).Range("F2").Value = currentRow(14)
                oBook.Worksheets(page).Range("A5").Value = currentRow(12)
                ...
                'Incremento la pagina
                page = page + 1
                'Se la pagina e' maggiore di 3 la devo creare
                If page > 3 Then

oBook.Worksheets.Add(After:=oBook.Worksheets(oBook.Worksheets.Count))
                End If

            Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
            End Try
        End While
        lblShowElab.Text = "Elaborazione Terminata"
    End Using
    'Salva il Workbook ed esce da Excel
    oBook.SaveAs("output.xlsx")
    oExcel.Quit()
End Sub
End Class

The commands oBook.Worksheets(page).Range("B2").Borders(xlEdgeRight).LineStyle = xlContinuous oBook.Worksheets(page).Range("B2").Borders(xlEdgeRight).Weight = xlThin does not work for me becouse Visual Studio do not recognize and mark me the xlEdgeRight, xlContinuous, xlEdgeRight, xlThin variables and pretend that I declare this.

This commas are common on every example I found in Internet, I do not understand why not works for me. Had I missed some libraries or namespace to declare? What I need?

Hope someone can help me, Regards, thaks a lot.

1 Answer 1

1

All the constants like xlEdgeRight, xlContinuous, xlEdgeRight, xlThin etc are just long integers.

You need to lookup their values and use them in your application.

Ideally you'd create a bunch of constants in your application so you can continue to use the named versions so its easier to understand your code.

The following page lists all the excel constants and their values. http://www.smarterdatacollection.com/Blog/?p=374 I assume there not tied to a specific excel version, but if they are you just need to lookup the ones for your version.

1
  • Quickest way in Excels VBA editor is to run ?xlEdgeRight in the immediate (ctrl+g) window or look them up in the object browser (f2)
    – Alex K.
    Jun 14, 2013 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.