简体   繁体   English

VBA宏-接收偏移对象定义的错误

[英]VBA Macro - Receiving Offset Object Defined Error

I'm trying to loop through the rows in a column of a spreadsheet in which there is a time in column 1, a date in column 2, and a value in column 3. the value changes every now and then and I need to single the times out that the value was changed. 我正在尝试遍历电子表格的一列中的行,其中在第1列中有一个时间,在第2列中有一个日期,在第3列中有一个值。该值不时更改,我需要单超时,该值已更改。 I have the following code which gives me a (run time error 1004- application defined or object defined error) and highlites 我有以下代码,给我一个(运行时错误1004-应用程序定义或对象定义的错误)和高级提示

  activecell.offset(-1,0).Range("A1").select 

I have been trying to fix it/research the problem most of the afternoon but can't get an answer that works for me. 我一直在试图解决它/研究整个下午的问题,但找不到适合我的答案。 any help would be appreciated 任何帮助,将不胜感激

Sub Timestamp

    Range("C1").Select

    While ActiveCell.Value <> ""

        Number = ActiveCell.Value
        Counter = Counter +1
        activecell.offset(-1,0).Range("A1").select 

        If ActiveCell.Value <> Number Then

            activecell.offset(-1,0).Range("A1").select 
            Time = ActiveCell.Value
            Sample = Sample +1
            activecell.offset((Counter - Sample),3).Range("A1").select 
            ActiveCell.Value = Time
            activecell.offset(-(Counter - Sample),-1).Range("A1").select 

        End If
    Loop
End Sub

You don't need to select the cells to get their values. 您无需选择单元格即可获取其值。 All you need is to refer to them. 您只需要参考它们即可。 VBA is an Object Oriented programming language. VBA是一种面向对象的编程语言。 Take a look at this document for basics: Object Oriented VBA 看一下本文档的基础知识: 面向对象的VBA

As far as getting a count of the times the Number changes, this should give you that value and you can add in any additional code you need to be handling. 至于获得Number更改次数的计数,这应该为您提供该值,并且您可以添加需要处理的任何其他代码。

    Option Explicit

Sub test()
Dim rng As Range
Dim number As Long
Dim counter As Integer

    Set rng = Range("C1")
    Do While rng.Value <> ""
        number = rng.Value
        If rng.Offset(1, 0).Value <> number Then
            counter = counter + 1
        End If
    Set rng = rng.Offset(1, 0)
    Loop
    MsgBox counter & " changes"
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA宏:应用程序定义或对象定义的错误 - VBA Macro: Application-defined or object-defined error 使用 Range.Offset 时出现 VBA 运行时错误 1004“应用程序定义或对象定义的错误” - VBA Runtime Error 1004 “Application-defined or Object-defined error” when using Range.Offset VBA 使用用户定义的连接字符串的宏中的应用程序定义或对象定义错误 function - VBA Application-defined or object-defined error in macro using concatenated strings with user defined function 为什么在VBA中收到“运行时错误1004&#39;应用程序定义的错误或对象定义的错误&#39;”? - Why am I receiving “Run Time Error 1004 'Application Defined or Object Defined Error'” in VBA? VBA 宏一次工作 - 现在给出 1004 错误“应用程序定义或 object 定义错误” - VBA macro worked at one time - now giving 1004 error "application defined or object defined error" MACRO VBA - “运行时错误 &#39;1004&#39; 应用程序 - 定义或对象定义错误” - MACRO VBA - "run time error '1004' application - defined or object defined error" 在宏中设置范围时,VBA运行时错误1004“应用程序定义的错误或对象定义的错误” - VBA Runtime Error 1004 “Application-defined or Object-defined error” when setting Range in macro VBA对象定义的错误 - VBA Object Defined Error 对象定义的错误VBA - Object defined error VBA 方法中未定义用户类型宏错误 vba - User type not defined macro error vba in method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM