如何使代码“循环 - 为每个”正确?

时间:2016-04-23 09:32:53

标签: excel vba excel-vba

我最近开始使用VBA Excel进行财务建模,并且已经发现它可以优化我的工作流程。

现在我尝试开发一个宏来帮助我自动格式化工作簿中的所有单元格,而且我不知道如何编写这样的宏。

我想要做的是以下 - 我有3种类型的数字:

  1. 硬数字
  2. 内部链接的数字 - 它们是工作簿的其他工作表中的值。
  3. 外部链接的数字 - 它们是其他工作簿中的值。
  4. 到目前为止,我已经编写了这段代码......

     Sub AllLinksAndValuesFormating()
    '===============================================
    'All links (internal) within this book are blue
    'All external links are black and font filled
    'All values are like this (# ##0;(# ##0);"-"??)
    '===============================================
    Dim Criteria_1 As Boolean
    Dim Criteria_2 As Boolean
    
    Dim Size As Integer
    Size = 8
    
    Dim Style As String
    Style = "Arial"
    
    Dim Value As Variant
    
    Criteria_1 = VBA.InStr(1, Value.Formula, ".xls") > 0
    Criteria_2 = VBA.InStr(1, Cells.Formula, "!") > 0
    
        For Each Value In ActiveSheet.Cells
    
            If IsNumeric(Value) Then
    
            If Criteria_1 = True Then
            '====================================
            'External Link
            '====================================
    
                With Selection
                    .Font.Color = RGB(38, 38, 38)
                    .Font.Name = Style
                    .Font.Size = Size
                    .Interior.Color = 11402743
    
                    .Borders(xlEdgeLeft).LineStyle = xlContinuous
                    .Borders(xlEdgeRight).LineStyle = xlContinuous
                    .Borders(xlEdgeTop).LineStyle = xlContinuous
                    .Borders(xlEdgeBottom).LineStyle = xlContinuous
    
                    .Borders(xlEdgeLeft).Weight = xlHairline
                    .Borders(xlEdgeRight).Weight = xlHairline
                    .Borders(xlEdgeTop).Weight = xlHairline
                    .Borders(xlEdgeBottom).Weight = xlHairline
    
                End With
    
            Else
    
            If Criteria_1 = False And Criteria_2 = True Then
            '====================================
            'Internal link
            '====================================
    
                With Selection
                    .Font.Color = RGB(38, 38, 38)
                    .Font.Name = Style
                    .Font.Size = Size
                    .Interior.ColorIndex = 0
    
                    .Borders(xlEdgeLeft).LineStyle = xlContinuous
                    .Borders(xlEdgeRight).LineStyle = xlContinuous
                    .Borders(xlEdgeTop).LineStyle = xlContinuous
                    .Borders(xlEdgeBottom).LineStyle = xlContinuous
    
                    .Borders(xlEdgeLeft).Weight = xlHairline
                    .Borders(xlEdgeRight).Weight = xlHairline
                    .Borders(xlEdgeTop).Weight = xlHairline
                    .Borders(xlEdgeBottom).Weight = xlHairline
    
                End With
                Else
    
                End If
    
    
        End If
        End If
    
          Next
    End Sub
    

    错误出现在这里:

    Criteria_1 = VBA.InStr(1, Value.Formula, ".xls") > 0
    

    错误是需要的对象

1 个答案:

答案 0 :(得分:1)

我会得到细胞的公式:

Cells(1,1).Formula

如果字符串包含任何指示外部工作簿的文本,请检查该字符串。

要知道单元格是硬编码还是参考,您可以使用

 Cells(1,1).HasFormula

这将返回true或false。