excel 如何在某个日期后冻结公式中的单元格值?

jmo0nnb3  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(62)

我有一个单元格区域(Sheet 1!E3:E55),这些单元格使用公式从另一个工作表(Sheet 2!C7)上的特定单元格提取计算完成百分比。
我希望在该范围内的单元格(即Sheet 1!E3)的计算值在该工作表上的另一个单元格(即Sheet 1!A4)中指定的日期变为静态/锁定单元格值。
我知道这需要搜索引擎,但我是一个新手,并没有能够找到任何通过谷歌。谢谢!
我尝试过各种公式,但通过谷歌搜索发现,这只能通过宏来实现。我尝试记录自己的宏来复制然后粘贴单元格的值,但这只适用于一个单元格,而不是范围。不幸的是,我对宏的了解不够,无法编辑代码来获得我想要的。

w6mmgewl

w6mmgewl1#

Private Sub Worksheet_Activate()
    '--------------------------------------------------------------------------------
    ' Right Click On "Sheet1" Tab and
    ' Select "View Code" and paste the following code
    '--------------------------------------------------------------------------------
    
    Dim FreezVal As Long, LastRow As Long, x As Long
    Dim FreezOnDate As Date, rowItemDate As Date
    Dim ws As Workbook
    
    Dim sh1 As Worksheet
    Dim sh2 As Worksheet
    
    Set ws = ThisWorkbook
    Set sh1 = ws.Sheets("Sheet1")
    Set sh2 = ws.Sheets("Sheet2")
    
    FreezOnDate = CDate(sh2.Range("E7").Value)  'refer to Sheet2!E7 value
    LastRow = sh1.Range("A3").End(xlDown).Row   'find last row of data in Sheet1
    
      For x = 3 To LastRow   ''In Sheet1 loop till last row and compare value in cell E7 in Sheet2
      
        rowItemDate = CDate(sh1.Range("A" & x).Value)
        FreezVal = sh1.Range("E" & x).Value
        
        sh1.Range("D" & x).Value = "=" & sh1.Range("c" & x).Address & "/" & sh1.Range("B" & x).Address
          
          If rowItemDate = FreezOnDate Then
          
            sh1.Range("D" & x).Value = Range("D" & x).Value
            sh1.Range("A" & x, "D" & x).Font.Color = vbBlue ''freezed row highlighted
          
          Else
          
           sh1.Range("A" & x, "D" & x).Font.Color = vbBlack
           
          End If
        
      Next x
    End Sub

字符串

相关问题