【问题标题】:How to solve property error when trying to convert multiple ranges into values?尝试将多个范围转换为值时如何解决属性错误?
【发布时间】:2020-05-15 10:01:12
【问题描述】:

所有,我是 VBA 新手,正在自学...尝试更改范围公式时出现属性错误...范围可以在代码底部找到。如何让这些单元格变为值而不抛出错误?

Sub InvestorModelMacro()

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    DisplayGridlines = False

    Dim r As Range, ws As Worksheet

    For Each r In Worksheets("Asset Dashboard").Range("C6:C9")   'go through each cell in DV list
        If Len(r) > 0 Then                                       'only do something if cell not empty
            Worksheets("Live").Range("D3").Value = r.Value        'transfer value to cell D3 of 'Live' tab
            Application.Calculate
            Set ws = Worksheets.Add                               'add new sheet
            ws.Name = Worksheets("Investor Model").Range("D3")    'renames new sheet after selected asset
            Worksheets("Investor Model").Cells.Copy
            ws.Range("A1").PasteSpecial xlFormulas                'copy values only from Investor Model to new sheet
            ws.Range("A1").PasteSpecial xlFormats                 'copy formats only from Investor Model to new sheet
            ActiveWindow.DisplayGridlines = False                 'turns off gridlines
            ws.Range("F46:G52,G54,G56:G58,G60:G61,G65:G72,G75:G80,G87,G89:G92").Values
            ws.Range("F46:G52,G54,G56:G58,G60:G61,G65:G72,G75:G80,G87,G89:G92").Font.Color = vbBlue

        End If
    Next r

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True


End Sub

【问题讨论】:

  • 您会在该范围内调用 .Value = .Value,但这不适用于多区域范围(其中单元格并非全部连续)。
  • PasteSpecial xlFormulas 粘贴公式 - 您的意思是粘贴值吗?
  • @BigBen,我明白了,那我是不是只需要打破多区域范围并将单元格单独转换为值?
  • @Tim,我希望我的大部分新工作表都显示公式,只是范围内的单元格需要是值。

标签: excel vba fonts colors


【解决方案1】:

最多可以使用区域

Dim Area As Range
'...
For Each Area In ws.Range("F46:G52,G54,G56:G58,G60:G61,G65:G72,G75:G80,G87,G89:G92").Areas
    Area.Value = Area.Value
    Area.Font.Color = vbBlue
Next Area

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多