Pregunta

i have written this simple code which i found on using excel macro.

Private Sub Macro3(ByVal Target As Range)
    If Not Intersect(Target, Range("A2:A2")) Is Nothing Then
        Application.EnableEvents = False
        Range("A3:A31").Value = Range("A2:A30").Value
        Application.EnableEvents = True
    End If
End Sub

but after creating i cannot find the macro nor can i run it through VBA editor. please help me out. i am using ms office 2010

Edits

what i actually want to implement.

i have a continuously changing value in A1 cell of my sheet. as soon as it changes i want it to be copied to A2 and value of A2 to A3 and so on...

¿Fue útil?

Solución 2

you can also try by implementing the following macro

Sub Macro()
  Dim n As Integer
    n = 1
  Do
    Application.EnableEvents = False
    Range("A2").EntireRow.Insert
    Range("A2").Value = Range("A1").Value
    Application.Wait Now + TimeValue("0:00:10")
    Application.EnableEvents = True
  Loop Until n != 1
End Sub

now should be able to store data column vise at an periodic time interval.

Otros consejos

As the Video explains at 01:00,

  • the code must be placed in Sheet1 (or the sheet you use to capture the server data), meaning that you have to doubleclick the sheet's name in the top left project explorer pane in VBA to access its code window

  • you must use the name of the Sub() exactly as shown on Youtube ... Private Sub Worksheet_Change(ByVal Target As Range). You can either type the code by hand or select "Worksheet" and "Change" from the selection boxes above the code window. Delete the "Sub Selection_Change" Sub eventually being automatically created when you select "Worksheet" (including the End Sub) This ensures that the macro is fired each time a (=any) cell in the worksheet changes.

  • The If Not Intersect(...) cares that only a change of a certain cell is processed by the rest of the code

  • if you set a breakpoint at the first line - Private Sub ... - using Debug/Toggle Breakpoint or {F9} - you will be able to step through the code using {F8} and watch

Edit:

after some googling ... =RTD() doesn't trigger Change macros (much to the disappointment of many) - so different strategies are needed.

=RTD() fetches data every now and then (time interval X) and puts it into a cell C different from your current [A2]. You could create a block of code which executes every X/2 and checks if C <> [A2] ... if yes move C to [A2] and copy the old values down.

Example:

Create a module "TimerFunctions" and add this code

Public IsTimer As Boolean

Sub TimerSet(IntervalSec As Date, TimerProcName As String)
    If IsTimer Then Application.OnTime Now() + IntervalSec, TimerProcName, , True
End Sub


Sub TimerAction()
    ' look for new value ... e.g. compare RTD cell [A1] with last stored value [A2]
    If [A2] <> [A1] Then
        ' your original code - move whole stack one down
        Range("A3:A31").Value = Range("A2:A30").Value
        ' copy new value on top of stack
        [A2] = [A1]
    End If

    ' do it again in 2 seconds
    TimerSet TimeValue("00:00:02"), "TimerAction"
End Sub

Sheet1

Private Sub Worksheet_Activate()
    IsTimer = True
    TimerSet TimeValue("00:00:02"), "TimerAction"
End Sub

Private Sub Worksheet_Deactivate()
    IsTimer = False
End Sub

Now every 2 seconds the RTD function is checked for a new value, and if found the value is saved on the top of the stack while the rest of 30 is pushed 1 down. This works as soon and as long you are in the sheet. If you change to Sheet2 (= deactivate Sheet1) this mechanism stops until you go back to Sheet1 (= activate sheet).

If this timed thing should run all the time you must put a bit more effort in addressing your cells so that all this is working even if the acive sheet is another one.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top