+ Reply to Thread
Results 1 to 17 of 17

the code doesnt work with macro event

  1. #1
    Registered User
    Join Date
    04-01-2015
    Location
    israel
    MS-Off Ver
    2007
    Posts
    15

    the code doesnt work with macro event

    hi
    I try to create index so that whenever the value of cell A1 is different from the value of cell B1, it is added to the variable N one

    The code I wrote:

    Private Sub Worksheet_Change (ByVal Target As Range)
    If Cells (1, 1) .Value <> Cells (1, 2) .Value Then
    Cells (1, 2) .Value = Cells (1, 1) .Value
    N = N + 1
    End If
    Cells (N, 3) .Value = 10
    End Sub

    The problem that the variable N always zero (0)(Although the condition is met) and is not updated according to what is written which prevents the execution of the last command because the variable N is zero.

    in Regular model code works fine and the variable is added to it at a time that the condition exists, but I want to work within a macro event?
    any help please
    Last edited by samisamih; 04-01-2015 at 04:53 PM. Reason: changing

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: the code doesnt work with macro event

    Shalom.

    This will never work the way you intend it to.

    Because a new macro is created whenever your worksheet changes.

    And whenever the new macro is run N = 0.

    ------------------------------------------------------------------------------------------------------

    So the trick is to create a variable N that is accessible by every macro.

    So in a normal macro module create this macro:

    Please Login or Register  to view this content.
    PLease Note that I used N1, for some reason N cannot be used as a global variable.

    realisticly. The following will also work:-

    Please Login or Register  to view this content.
    Last edited by mehmetcik; 04-01-2015 at 05:27 PM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    04-01-2015
    Location
    israel
    MS-Off Ver
    2007
    Posts
    15

    Re: the code doesnt work with macro event

    many thanks for your help
    I understand now , and I wrote this code and its works:

    public n as integer
    Private Sub Worksheet_Change (ByVal Target As Range)
    If Cells (1, 1) .Value <> Cells (1, 2) .Value Then
    Cells (1, 2) .Value = Cells (1, 1) .Value
    n = n + 1
    End If
    Cells (n, 3) .Value = 10
    End Sub

    But the problem remained that the variable (n) gets the value in delay, and the macro runs the last command "Cells (n, 3) .Value = 10" before the variable (n) enough to get the value then i get the error because the variable (N) is still zero, is it possible to solve this problem ?

    when I wrote the last command "Cells (n, 3) .Value = 10" within the Conditional the macro works fine,without delay

    public n as integer
    Private Sub Worksheet_Change (ByVal Target As Range)
    If Cells (1, 1) .Value <> Cells (1, 2) .Value Then
    Cells (1, 2) .Value = Cells (1, 1) .Value
    n = n + 1
    Cells (n, 3) .Value = 10
    End If

    End Sub


    but I need to use the variable (n) Outside the Conditional?


    best regards
    Last edited by samisamih; 04-02-2015 at 03:57 AM.

  4. #4
    Registered User
    Join Date
    04-01-2015
    Location
    israel
    MS-Off Ver
    2007
    Posts
    15

    Re: the code doesnt work with macro event

    the error with the variable (n)
    Attached Images Attached Images

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: the code doesnt work with macro event

    How about eliminating the variable N and just putting 10 in the next row.

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  6. #6
    Registered User
    Join Date
    04-01-2015
    Location
    israel
    MS-Off Ver
    2007
    Posts
    15

    Re: the code doesnt work with macro event

    This code does not help me because I want only whenever there is a difference between cell A1 and cell B1 to register 10 in cell (n,c)

    so (n )will count to me how many times this 2 cells(a1,b1) was difference and then to use ( n )with cell c to Perform certain conditions
    Last edited by samisamih; 04-02-2015 at 10:30 AM.

  7. #7
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: the code doesnt work with macro event

    Sorry my friend.

    You need to declare the Variable in a normal module.

    I think you are declaring it on a sheet specific module.

    And I do not think that will work.

    I will test this and come back if I am wrong.

    Otherwise move the line

    Please Login or Register  to view this content.
    To normal macro module.

  8. #8
    Registered User
    Join Date
    04-01-2015
    Location
    israel
    MS-Off Ver
    2007
    Posts
    15

    Re: the code doesnt work with macro event

    thank for your respond mehmetcik
    even Sets the variable in normal module the result the same
    Attached Images Attached Images

  9. #9
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: the code doesnt work with macro event

    Ok I have checked.


    The reason your macro is crashing is that initially N = 0 or N = "" there is no line 0 or line "" so the macro crashes.

    This code works:-

    Please Login or Register  to view this content.
    But it is not very efficient.

    it will run each time the sheet is modified.

    So if for example you make b1 equal to a1 +1 then

    The macro will run.

    The first line will Make b1 equal to a1 and that will cause the macro to run

    in the above macro range d1 will be made equal to N1 and the macro will run

    then range c & N1 will be made to equal 108 and the macro will run.

    So the macro will run 4 times. This is not an issue with a short program. But you could quickly crash excel in a larger program.


    This version disables events so the macro is only run once.

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    04-01-2015
    Location
    israel
    MS-Off Ver
    2007
    Posts
    15

    Re: the code doesnt work with macro event

    I do not know how to thank you my friend
    The code works fine now:

    Public n1 As Integer

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Cells(1, 1).Value <> Cells(1, 2).Value Then
    Application.EnableEvents = False
    Cells(1, 2).Value = Cells(1, 1).Value
    n1 = n1 + 1
    Range("D" & n1).Value = n1
    Range("C" & n1).Value = 108
    End If

    Cells(n1, 5).Value = 20

    Application.EnableEvents = True
    End Sub

    many many thanks
    תודה רבה

  11. #11
    Registered User
    Join Date
    04-01-2015
    Location
    israel
    MS-Off Ver
    2007
    Posts
    15

    Re: the code doesnt work with macro event

    only I want to ask you one question please
    Is at the moment I am writing Application.EnableEvents = false It neutralizes any change is happening and prevents execution of other macros on the worksheet or cause to be missing result\value with other macros in the same worksheet

    best regards

  12. #12
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: the code doesnt work with macro event

    Please edit your posts to include post tags.




    It stops any macros running until you have done editing.

    Then you start the macros again using:

    Please Login or Register  to view this content.

    If that causes you a problem. then there is another solution:-

    Please Login or Register  to view this content.
    The Changeflag allows you to control how many times this macro runs without affecting any other macros.

  13. #13
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: the code doesnt work with macro event

    Quote Originally Posted by samisamih View Post
    This code does not help me because I want only whenever there is a difference between cell A1 and cell B1 to register 10 in cell (n,c)

    so (n )will count to me how many times this 2 cells(a1,b1) was difference and then to use ( n )with cell c to Perform certain conditions
    You don't need N to count how many times the two cells are together.

    Rather than "every time they equal, increase N and put 10 in row N" you could "every time they equal, put 10 in the row below the last filled row."

    That fills up the same cells, eliminates the fiddling with N (and the losing N if there is a error in some other routine or you press ESC).

  14. #14
    Registered User
    Join Date
    04-01-2015
    Location
    israel
    MS-Off Ver
    2007
    Posts
    15

    Re: the code doesnt work with macro event

    hello mikerickson

    My goal is not exactly to execute the command "Cells (n, 3) .Value = 10" ,
    I wrote just for testing that the variable N does not change after the condition and work well ,
    And because this is a macro event what was happening then( after met and closing the condition) the variable n is reset again (zero) and then I get the error

    I think the variable Boolean in post#12 solved the problem

    thank you a lot for your help
    Last edited by samisamih; 04-03-2015 at 05:21 AM.

  15. #15
    Registered User
    Join Date
    04-01-2015
    Location
    israel
    MS-Off Ver
    2007
    Posts
    15

    Re: the code doesnt work with macro event

    to mehmetcik
    thank you I think the Boolean variable work ok now with the counter I will test more and update you

    best regards
    Last edited by samisamih; 04-06-2015 at 02:03 PM.

  16. #16
    Registered User
    Join Date
    04-01-2015
    Location
    israel
    MS-Off Ver
    2007
    Posts
    15

    Re: the code doesnt work with macro event

    all is ok with the counter N thank you a lot for you help
    Last edited by samisamih; 04-06-2015 at 03:10 PM.

  17. #17
    Registered User
    Join Date
    04-01-2015
    Location
    israel
    MS-Off Ver
    2007
    Posts
    15

    Re: the code doesnt work with macro event

    hi mehmetcik
    can you help me please in one thing please
    i have problem with my macro that there is slowdown in performance when I write the result of the macro in cell
    my code is :

    Public n1 As Integer
    Public changeflag as boolean
    public st as string
    dim arr(1 to 10) as integer
    Private Sub Worksheet_Change(ByVal Target As Range)
    If changeflag = true then exit sub
    changeflag = true
    If Cells(1, 1).Value <> st Then
    st = Cells(1, 1).Value
    n1 = n1 + 1
    End If

    arr(n1)=*number* related to other macro
    cells(n1,2).value=arr(n1)
    Changeflag = false

    End Sub

    the problem in " cells(n1,2).value=arr(n1) " the result i get in the cell is printed in delay which cause to be miss data sometimes(its happend when there is vaery very fast update of data in the sheet-part of second)

    i tried to add the command application.screenupdating=false/true at the Beginning and the end but did not help.
    is there a way to display the arry value in the cell without delay data , without using msgbox .?
    i need that the macro work without lossing any data. its possible?

    best regards
    Last edited by samisamih; 04-21-2015 at 07:34 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Run macro when cell is changed(also as a result of listboxchange-change event doesnt work)
    By drrazor in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-09-2014, 04:13 PM
  2. [SOLVED] Click event fires but doesnt work under initialze procedure
    By kmakjop in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-19-2014, 02:40 PM
  3. [SOLVED] How to make my code work as an event macro
    By strud in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-13-2013, 11:32 AM
  4. code doesnt work after upgrade to 2010
    By jmckee in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-12-2011, 09:57 AM
  5. Why doesnt this line of code work???
    By joseclar in forum Excel General
    Replies: 1
    Last Post: 05-05-2009, 02:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1