vba Visual basic - Excel 单元格的边框属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17106722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:39:02  来源:igfitidea点击:

Visual basic - Excel cell's border property

visual-studioexcelvbaborder

提问by F.M.

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

我是 Visual Basic 的新手,我正在尝试构建一个简单的应用程序,该应用程序可以对 Excel 文件执行某些操作。

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.htmBorder around each cell in a rangehttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/93bb7ff7-0aed-4ce1-adca-aabde5fc3c2c

我在网上找到了一些有趣的资源: http://www.functionx.com/vbaexcel/cells/Lesson4.htm一个范围内每个单元格周围的边框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.

命令 oBook.Worksheets(page).Range("B2").Borders(xlEdgeRight).LineStyle = xlContinuous oBook.Worksheets(page).Range("B2").Borders(xlEdgeRight).Weight = xlThin 不适用于因为 Visual Studio 无法识别并标记我 xlEdgeRight、xlContinuous、xlEdgeRight、xlThin 变量并假装我声明了这一点。

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.

希望有人可以帮助我,问候,非常感谢。

回答by NickSlash

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

xlEdgeRight、xlContinuous、xlEdgeRight、xlThin 等所有常量都只是长整数。

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=374I assume there not tied to a specific excel version, but if they are you just need to lookup the ones for your version.

以下页面列出了所有 excel 常量及其值。http://www.smarterdatacollection.com/Blog/?p=374我假设没有绑定到特定的 excel 版本,但如果它们是你只需要查找你的版本的那些。