+ Reply to Thread
Results 1 to 4 of 4

Conditional Macros

Hybrid View

  1. #1
    Registered User
    Join Date
    05-14-2015
    Location
    South Daktoa, United States
    MS-Off Ver
    365 version: 15.0.4711.1003
    Posts
    10

    Question Conditional Macros

    Hello everyone!

    I have a seemingly very challenging problem that I can't seem to find the answer to.

    I have two Tables that need to be worked with.

    The first table (Table 1) will have the following headers: "Member", "Rank", and currently "Comment" (though I want to get rid of this).
    The second table (Table 2) will have the following headers: "Rank" and "Flags" (among other things)

    The way I want to enter my data is: Type the Member name, Rank, and Insert a Comment to the cell of the Member name.
    I then want the cells in the "Flags" column of Table 2 checking two criteria per each row in Table 1...
    1) Is there a comment in the Member's Cell?
    2) Does the Rank of that Member match the Rank for this row?
    ...and to then to do +1 in "Flags" if both criteria are met.

    Currently my "Comment" header in Table 1 is being used as a work around method that still doesn't fully work the way I want it to.

    It's purpose was to utilize the following code I found online...
    Function Has_Comment(mycell As Object) As Boolean
    
     If mycell.Comment.Visible = False Then
      Has_Comment = False
     Else
      Has_Comment = True
     End If
     
    End Function
    ...with the call to that function being
    =NOT(ISERROR(has_comment(E2)))
    But the problem I was having was that if I inserted a comment into a Member name cell - the program didn't detect the change and automatically update it.

    I tried changing the code to...
    Function Has_Comment(mycell As Object) As Boolean
    
     Application.Volatile
    
     If mycell.Comment.Visible = False Then
      Has_Comment = False
     Else
      Has_Comment = True
     End If
     
    End Function
    ...but this didn't seem to do anything for me. In both cases - the value in the Comment cell would change from TRUE or FALSE only after I inserted a comment, went to the cell, and then pressed Enter.

    I'm fine with trying to get this "work around" to work if there is a way to make the macro work like the rest of sheet - updating whenever anything gets changed, but ultimately i'd be more happy if there was just a simple conditional statement that checked my above criteria and effectively count the number of flags were issued per each rank.

    As is probably very obvious - my background in VB is basically non-existent - so I'm going to need a step by step walk through on what I should be doing to get this to work.

    I saved a non-Macro enabled worksheet before attempting any Macro related things so if starting from square one is a recommended thing - I can do that :D
    (since I probably screwed a lot of things up having never worked with the VBE before)

    I'd also really appreciate information and descriptions about what i'm doing throughout this process since I am going to school for programming - I'm just in the very early stages :/ .

    Anyways, I thank all of you who take the time out of your day to assist me in this well in advance!

    Thank You,
    DragonRulerX

  2. #2
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Conditional Macros

    Adding a comment is not considered to be an event that causes recalculations. A possibility would be to add a macro that uses the OnTime event to force a sheet calculation every x seconds. Or use the SelectionChange event. Not great solutions, but they could work.

    My recommendation is to not use a comment as actionable data. I understand it may look better, but that is not its purpose, and that is why you are running into this issue. As you learn more about programming, you will come to discover that the data structures you use are just as important as the algorithm; they are tied together and can help/hinder each other.
    Pauley
    --------
    If I helped with your issue, I'd appreciate a rep bump (hit the '*' icon to the bottom left of this post).

  3. #3
    Registered User
    Join Date
    05-14-2015
    Location
    South Daktoa, United States
    MS-Off Ver
    365 version: 15.0.4711.1003
    Posts
    10

    Re: Conditional Macros

    I'm all for adjusting my method to not use the comment as "actionable data", but I am curious about what you said here...

    A possibility would be to add a macro that uses the OnTime event to force a sheet calculation every x seconds. Or use the SelectionChange event. Not great solutions, but they could work.
    What would be the drawbacks of using the OnTime and/or SelectionChange event?

    From a quick Google search I found:
    1) https://msdn.microsoft.com/en-us/lib.../ff196165.aspx - OnTime
    2) https://msdn.microsoft.com/en-us/lib.../ff194470.aspx - SelectionChange

    From my understanding - using the OnTime command and setting it to run every 1-second could be an option? Or is this too taxing on the computer/program?
    Additionally - if this is a viable option - how would I approach using it?

    The SelectionChange code confuses me a little bit...
    Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
     With ActiveWindow 
     .ScrollRow = Target.Row 
     .ScrollColumn = Target.Column 
     End With 
    End Sub
    I'm not sure I'd understand how this works exactly? Is this limited to a single cell or can it be applied to a range of cells? Also, how does it identify adding/removing a comment to a cell?

    If we could make either of these work it'd be a very nice learning experience, but if you feel most of these won't work very well or consistently - I can always just add a column to the table for the comments (shrinking that column size to 0 pixels) and use a conditional checking to see if that cell is empty and "If Empty - No Comment | Else - Has Comment".

  4. #4
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Conditional Macros

    The drawbacks are that you have something running in the background, but I doubt a recalculation event is very taxing. I have also found that some macros may disrupt your Undo queue. Finally, the user must agree to use the macros when they open the spreadsheet. If they respond 'no macros' then your work is bypassed. Not terrible, but also not 'clean' (and maybe that is a personal view).

    With OnTime you would need to kickstart the code by using the Workbook_Open event. This would call your sub which would include the .Calculate method (which you can constrain to a specific range to reduce overhead) and then set the timer for let's say 5 seconds to run the sub again. After 5 seconds, the sub is called again, it .Calculates and sets the timer again, repeat.

    SelectionChange is triggered whenever a new selection is made by the user. It does not identify adding/removing a content to a cell, but since a person will probably click on a cell fairly soon, it will be fairly up to date. When you enter a comment, do you 'escape' out of it or click on the cell that you added the comment? If so, then this will not be triggered. If you click on a different cell, then this will be triggered. I think this is the better choice if you want to go down this route.

    Personally, I like your last suggestion. It keeps the data available for easy access. Say, for example, in the future you want to search for a keyword in the text string. Much easier if in a cell.

    However, I understand the desire to experiment, so feel free to try all methods.

+ 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] Conditional formatting or macros or vba?
    By lcartwright in forum Excel General
    Replies: 2
    Last Post: 08-07-2014, 06:28 PM
  2. [SOLVED] Conditional formatting + macros
    By helpplease344 in forum Excel General
    Replies: 4
    Last Post: 05-16-2014, 03:42 PM
  3. [SOLVED] Macros and conditional formatting
    By skalus in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-25-2013, 08:38 AM
  4. Conditional Formatting And Macros
    By Dominic_Hodgson in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-04-2007, 05:33 AM
  5. How to Put Conditional Sums in macros
    By Alan P in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-04-2005, 05:15 PM

Tags for this Thread

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