Help with sheet code

ASBeard

New Member
Joined
May 7, 2024
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Good morning. I have an excel file and I have written code to display a default value in approximately 400 cells. The code works correctly, however whenever I several of these cells simultaneously it causes a critical error and the excel file closes. I have placed an image of just some of the code as the mini-sheet I don't think will copy inside visual basic (if this is incorrect, please let me know). If you need to see all of the code, please don't hesitate to let me know. And just so you know, the code is entered in the 'sheet' for which the default values are to be placed and not in a module. I am hoping that there is something I can do to simplify my code to prevent the critical error. Thank you in advance for any help you can provide!
 

Attachments

  • 2024-05-07_10-48-40.png
    2024-05-07_10-48-40.png
    70 KB · Views: 16

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Remember, this is a sample. You'll need to finish the rest. I forgot to set the minor rng to the intersect

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim rng As Range
  Dim cell As Range
 
  Application.EnableEvents = False
 
  Set rng = Intersect(Range("B27:B32"), Target)
  If Not rng Is Nothing Then
    For Each cell In rng
      If cell.Value = "" Then cell.Value = "(Enter Custom Label)"
    Next cell
  End If
 
  Set rng = Intersect(Range("F10"), Target)
  If Not rng Is Nothing Then
    For Each cell In rng
      If cell.Value = "" Then cell.Value = "MRN"
    Next cell
  End If
 
  Set rng = Intersect(Range("F11"), Target)
  If Not rng Is Nothing Then
    For Each cell In rng
      If cell.Value = "" Then cell.Value = "Visit Number"
    Next cell
  End If
 
  Application.EnableEvents = True
 
' . . . . . .

End Sub
 
Upvote 1
Solution
Thank you so much... so far so good but obviously I have a lot of code to add so it might be tomorrow before I can get back and let you know if it worked for all the code but its looking good so far. I cannot thank you enough!!
 
Upvote 0
Jeff I think you got it. I was able to clear the same number of cells that previously caused the fatal error. Thank you again sir!! Take care.
 
Upvote 0
Jeff, just wanted to follow up to let you know that the code worked perfectly for the entire spreadsheet! I really appreciate your help!!

One last question, if I wanted to use the same code on another sheet in the workbook how would I rename the code? I have tried a few things that did not work.
 
Upvote 0
When I tried that the code on the original sheet stopped working, I assumed it was a naming issue. Could it be too many lines of code?
 
Last edited:
Upvote 0
Could it be too many lines of code?
Excel would give you an error message stating it if it was.

Run the code below and then see if the code works again
VBA Code:
Sub ResetCode()
    Application.EnableEvents = True
End Sub
 
Upvote 0
Thank you Mark! I ran this as a separate macro and it did get everything running again, however I had to run it every time. I added it to my code from one of my sheets and it seems to have solved my issue. Thank you both for all your time and help!
 
Upvote 0
Sounds like the code you have added is making the code exit before it reaches the last line in Jeffrey's code.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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