代码中有什么错误,因为它不起作用

时间:2014-10-03 06:04:37

标签: vba excel-vba excel

在我的Excel工作簿中,有14列是C到P,范围是C1:P200第1行包含时间值。示例:C1 = 9/27/2014 8:42:34 AM; D1 = 9/27/2014 9:12:25 AM&所以...直到P1。

需要输出:C2:C200应该立即免费配方&当系统时间> = C1时保留该值。同样,D2:200应该变为公式免费&系统时间> = D1&时保留该值所以直到P列,即P2:P200应该变成无公式&系统时间> = P1

时保留该值

ThisWorkbook代码:

Private Sub Workbook_Open()
    Dim cl As Range

    For Each cl In ThisWorkbook.Sheets("Sheet1").Range("A1:P1")
        Application.OnTime cl.Value, "'ConvertFormulasToValues """ & cl.Range("A2:A199").Address & """'"
    Next cl
End Sub

然后我按如下方式创建了一个Sub:

Private Sub ConvertFormulasToValues(RngToConvertAddress As String)
Dim rng As Range
    Set rng = Range(RngToConvertAddress)
    rng.Value = rng.Value
End Sub

由于代码不符合要求,我所犯的错误。

3 个答案:

答案 0 :(得分:0)

几件事:

一个。在Private之前移除单词Sub可能是解决方案!

湾你提到14列是C到P.但是如果不是Range("A1:P1"),你的代码包含Range("C1:P1")

顺便说一句,如果过程中出现任何错误消息,您应该发帖。

附录:

Private Sub Workbook_Open()
Dim cl As Range

For Each cl In ThisWorkbook.Sheets("Sheet1").Range("C1:P1")
Application.OnTime cl.Value, "'ConvertFormulasToValues """ & cl.Range("A2:A199").Address & """'"  
Next cl
End Sub

Sub ConvertFormulasToValues(RngToConvertAddress As String)
Dim rng As Range
Set rng = Range(RngToConvertAddress)
rng.Value = rng.Value
End Sub

APPENDED Image:Range.Range说明

Range.Range Explanation

答案 1 :(得分:0)

您需要将以下内容放入“模块”中,并将Private更改为Public

Public Sub ConvertFormulasToValues(RngToConvertAddress As String)
Dim rng As Range
    Set rng = Range(RngToConvertAddress)
    rng.Value = rng.Value
End Sub

以下内容可以留在“ThisWorkbook”下:

Private Sub Workbook_Open()
    Dim cl As Range

    For Each cl In ThisWorkbook.Sheets("Sheet1").Range("A1:P1")
        Application.OnTime cl.Value, "'ConvertFormulasToValues """ & cl.Range("A2:A199").Address & """'"
    Next cl
End Sub

测试并运作。

答案 2 :(得分:0)

Sub t()

For Each cell In Range("c1:p1")
    If Now >= cell.Value Then
        Call timer1(cell.Column)
    End If
 Application.CutCopyMode = false
 Range("c1").select

Next cell

End Sub


Sub timer1(col As Integer)
With Sheets("Sheet1")
    .Range(.Cells(2, col), .Cells(199, col)).Copy
    .Cells(2, col).PasteSpecial (xlPasteValues)
End With

End Sub