цикл vba через края ячейки для границы - PullRequest
1 голос
/ 27 октября 2010

По сути, я хочу пройтись по краям коллекции Borders.есть ли «для каждого» способ сделать это?

Private Function getCellBorder(ByVal vArg As Range) As String

  For Each Edge in Borders
    Debug.Print vArg.Borders(Edge).LineStyle
  Next Edge

End Function

Ответы [ 2 ]

1 голос
/ 27 октября 2010
Function getCellBorder(ByVal vArg As Range) As String

Dim a

  For Each a In vArg.Borders 
     Debug.Print a.LineStyle
  Next a

End Function  

Редактировать

Ниже приведен гораздо более сложный код, который использует какое-то отражение и повторяется в Enums.

Связи Enum легко запоминаются в Excel, поскольку вы не можете их зациклить ... кроме как с помощью этого трюка.

Option Explicit
Option Compare Text

Sub a()
getCellBorder (Worksheets("Sheet1").Range("A1"))
End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Module:   TLIReporter modified by Belisarius for SO answers
' Author:   Chip Pearson at Pearson Software Consulting, LLC.
' Date:     10-Nov-2000
' Usage:    Freely distributable, with attribution.
' Desription:   Lists all of the objects in the Excel object model, with
'               properties and methods, and their data types.
' Requirements: Requires TLBINF32.DLL (provided with Visual Studio 6) & available at
'               http://www.nodevice.com/dll/TLBINF32_DLL/item16735.html (as of 20090729)
'  TLBINF32.DLL (TypeLib Information) must be referenced from this project.
'  Help file from MS at
'  http://support.microsoft.com/support/kb/articles/Q224/3/31.ASP
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Original file available at Chip Pearson's website:
'http://www.cpearson.com/Zips/XLConsts2.ZIP
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function getCellBorder(ByVal vArg As Range) As String
Dim TLApp As TLI.TLIApplication         ' The TLI application
Dim TLInfo_XL As TLI.TypeLibInfo        ' The TYPELIB for Excel
Dim TLInfo_MSO As TLI.TypeLibInfo       ' The TYPELIB for Office
Dim ConstInfo As TLI.ConstantInfo
Dim MemInfo As TLI.MemberInfo
Dim a As Range
    Set TLApp = New TLI.TLIApplication
    ' Get the XL and MSO typelibs
    Set TLInfo_XL = TLApp.TypeLibInfoFromFile(ThisWorkbook.VBProject.References("EXCEL").FullPath)
    Set TLInfo_MSO = TLApp.TypeLibInfoFromFile(ThisWorkbook.VBProject.References("OFFICE").FullPath)

        For Each ConstInfo In TLInfo_XL.Constants
            'Debug.Print ConstInfo.Name
            If ConstInfo.Name = "XlBordersIndex" Then
                For Each MemInfo In ConstInfo.Members
                   Debug.Print MemInfo.Value, MemInfo.Name, vArg.Borders.Item(MemInfo.Value).LineStyle
                 Next MemInfo
                 Exit Function
            End If
        Next ConstInfo
End Function

Пример вывода:

 Border        Border Enum                 Line Style
 Enum Type     Name                        Enum Number

 12            xlInsideHorizontal          -4142 
 11            xlInsideVertical            -4142 
  5            xlDiagonalDown              -4119 
  6            xlDiagonalUp                -4142 
  9            xlEdgeBottom                    1 
  7            xlEdgeLeft                  -4118 
 10            xlEdgeRight                     4 
  8            xlEdgeTop                   -4115 

HTH!

1 голос
/ 27 октября 2010

Не совсем .Я полагаю, вы могли бы определить Edge array / type / enum, но поскольку мы говорим только о нескольких границах, это не стоит (о, и ваш код psuedo также будет включать диагональные границы).Вот мой производственный код:

With Objws.Application.Selection
    .NumberFormat = "#,##0"
    .Borders(xlEdgeLeft).LineStyle = xlContinuous
    .Borders(xlEdgeRight).LineStyle = xlContinuous
    .Borders(xlEdgeTop).LineStyle = xlContinuous
    .Borders(xlEdgeBottom).LineStyle = xlContinuous
    .Borders(xlInsideVertical).LineStyle = xlContinuous
    .Borders(xlInsideHorizontal).LineStyle = xlContinuous
    .HorizontalAlignment = xlHAlignCenter
    .VerticalAlignment = xlVAlignCenter
    .WrapText = True

Имейте в виду, что существует такая вещь, как "чрезмерная оптимизация" вашего кода.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...