VBA code takes long to excute

asher_nk

New Member
Joined
May 8, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am very new to VBA. I would like my code add a comment box to the active cell based on the text in the active cell. As the user inputs code it should add a comment. However when users move down the sheet and populate the rows it seems that the code takes longer to execute. Any help would be much appreciated.

Thanks.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
If you have some Excel Vba code that is running slow then past your code here so we can see it.
And define slow. I have seen some people on the Forum who thinks 1.1 seconds is slow
 
Upvote 0
Hi of course below is the code. I have coded to a private sub and then calls a module. Thanks for your help.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("AMMS")) Is Nothing Then  
   
  Call popupnotesAMMS
 
ElseIf Not Intersect(Target, Range("ES")) Is Nothing Then
  
  Call popupnotesES
 
End If

End Sub


VBA Code:
Sub popupnotesAMMS()

ActiveCell.ClearComments

    If ActiveCell = "text" Then
        ActiveCell.AddComment "text"
    
        ElseIf ActiveCell = "text" Then
        ActiveCell.AddComment "text"
       
        ElseIf ActiveCell = "text" Then
        ActiveCell.AddComment "text"
       
    End If

ActiveCell(1, 3).ClearComments

    If ActiveCell(1, 3) = "text" Then
        ActiveCell(1, 3).AddComment "text"
   
        ElseIf ActiveCell(1, 3) = "text" Then
        ActiveCell(1, 3).AddComment "text"
       
    End If
   
    Dim xcomment As Comment
    For Each xcomment In Application.ActiveSheet.Comments
    xcomment.Shape.TextFrame.AutoSize = True
    Next
 
End Sub


VBA Code:
Sub popupnotesES()

ActiveCell.ClearComments

   
 If ActiveCell = "text" Then
        ActiveCell.AddComment "text"
    
        ElseIf ActiveCell = "text" Then
        ActiveCell.AddComment "text"
       
        ElseIf ActiveCell = "text" Then
        ActiveCell.AddComment "text"
       
    End If

ActiveCell(1, 2).ClearComments

    If ActiveCell(1, 2) ="text" Then
        ActiveCell(1, 2).AddComment "text"
   
 
    If ActiveCell(1, 2) ="text" Then
        ActiveCell(1, 2).AddComment "text"
       
    End If

    Dim xcomment As Comment
    For Each xcomment In Application.ActiveSheet.Comments
    xcomment.Shape.TextFrame.AutoSize = True
    Next

End Sub


I have taken out the text for some clarity as it is really wordy.
 
Last edited by a moderator:
Upvote 0
Can you please explain in words what your attempting to do.
And you said:
If Not Intersect(Target, Range("AMMS")) Is Nothing Then

There is no Range("AMMS")
 
Upvote 0
In the worksheet I have named a range as AMMS (its cells C9:C171) by using the name box. I would like for the code to first check
1. If the activecell is in that range, if it is
2. ,then for a note to be added to that activecell based user inputs (from a dropdown box)

The problem is it takes to long for the code to run. I am assuming it recalculates the comments
 
Upvote 0
You do have the appropriate NamedRanges , "AMMS" & "ES" ??
 
Upvote 0
What happens if you use this
AND Can you please use code tags when posting code....so much easier to read AND debug
Simply click on the VBA button on the reply toolbar, and paste you code where the cursor is flashing
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("AMMS")) Is Nothing Then
Application.EnableEvents = False
Call popupnotesAMMS
ElseIf Not Intersect(Target, Range("ES")) Is Nothing Then
Call popupnotesES
End If
Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top