473,414 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

VBA Excel SheetChange event - nothing happens when i make the change

11
I'm trying to write a procedure that will enable button on a sheet (and colour it red), but ONLY when changes are made to cells in column E of that sheet (from row 10 to the bottom of the used-range).

However, at present, when I edit a cell in column E, nothing happens to the button (it is currently grey and disabled).

I've tried using Microsoft's VBA online help, and also my bible (Excel VBA for dummies) but haven't found any clues so far. Can anyone help?

I've written the code in the sheet, rather than a separate module. And I've done it as a sheet-change procedure, as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.  
  3. Dim FirstRow As Double
  4. Dim TotalRows As Double
  5. Dim Row As Double
  6.  
  7. TotalRows = Worksheets("Calculation").UsedRange.Rows.Count
  8. FirstRow = 10
  9.  
  10. For Row = FirstRow To TotalRows Step 1
  11.         If Target.Address = Worksheets("Calculation").Cells(Row, 5) Then
  12.            CaAdjustmentButton.Enabled = True
  13.            CaAdjustmentButton.BackColor = "&H0000000FF&"
  14.         Else: Exit Sub
  15.         End If
  16. Next Row
  17.  
  18. End Sub
  19.  
  20.  
Jan 24 '08 #1
4 5216
MikeTheBike
639 Expert 512MB
I'm trying to write a procedure that will enable button on a sheet (and colour it red), but ONLY when changes are made to cells in column E of that sheet (from row 10 to the bottom of the used-range).

However, at present, when I edit a cell in column E, nothing happens to the button (it is currently grey and disabled).

I've tried using Microsoft's VBA online help, and also my bible (Excel VBA for dummies) but haven't found any clues so far. Can anyone help?

I've written the code in the sheet, rather than a separate module. And I've done it as a sheet-change procedure, as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.  
  3. Dim FirstRow As Double
  4. Dim TotalRows As Double
  5. Dim Row As Double
  6.  
  7. TotalRows = Worksheets("Calculation").UsedRange.Rows.Count
  8. FirstRow = 10
  9.  
  10. For Row = FirstRow To TotalRows Step 1
  11.         If Target.Address = Worksheets("Calculation").Cells(Row, 5) Then
  12.            CaAdjustmentButton.Enabled = True
  13.            CaAdjustmentButton.BackColor = "&H0000000FF&"
  14.         Else: Exit Sub
  15.         End If
  16. Next Row
  17.  
  18. End Sub
  19.  
  20.  
Hi

There are maybe a couple of thing, but I think this line

If Target.Address = Worksheets("Calculation").Cells(Row, 5) Then

perhaps should be

If Target.Address = Worksheets("Calculation").Cells(Row, 5).Address Then


However, I think this may be better ??

Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.  
  3.     Dim FirstRow As Integer
  4.     Dim TotalRows As Long
  5.  
  6.     TotalRows = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
  7.     FirstRow = 10
  8.  
  9.     If .Column = 5 And Target.Row >= FirstRow And Target.Row <= TotalRows Then
  10.         CaAdjustmentButton.Enabled = True
  11.         CaAdjustmentButton.BackColor = 255
  12.     Else
  13.         CaAdjustmentButton.Enabled = False
  14.         CaAdjustmentButton.BackColor = -2147483633
  15.     End If
  16.  
  17. End Sub
Note the use of Integer/Long data types for Row counters !!

HTH


MTB
Jan 24 '08 #2
Lara1
11
Thanks again for helping out, Mike. Not quite there yet, but maybe you'll know why?

If Target.Address = Worksheets("Calculation").Cells(Row, 5) Then

perhaps should be

If Target.Address = Worksheets("Calculation").Cells(Row, 5).Address Then
I tried this but it didn't seem to do the trick, so I figured I'd abandon my code and try out yours.


However, I think this may be better ??

Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.  
  3.     Dim FirstRow As Integer
  4.     Dim TotalRows As Long
  5.  
  6.     TotalRows = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
  7.     FirstRow = 10
  8.  
  9.     If .Column = 5 And Target.Row >= FirstRow And Target.Row <= TotalRows Then
  10.         CaAdjustmentButton.Enabled = True
  11.         CaAdjustmentButton.BackColor = 255
  12.     Else
  13.         CaAdjustmentButton.Enabled = False
  14.         CaAdjustmentButton.BackColor = -2147483633
  15.     End If
  16.  
  17. End Sub
Line 6 appears to have a big space at the end of it, before the .Row. The VBE didn't like that so i ran .Row onto the end of SpecialCells(xlCellTypeLastCell)

But it still isn't quite working. If I make any changes in the sheet (not just in column E), I get an error message saying that .Column (line 9) is an invalid reference.

I've tried changing the reference to say Activesheet.Column. Now if I make any changes in the sheet (not just in column E), I get the error message "Run-time error 438. Object doesn't support this property or method".

Any clues?


Note the use of Integer/Long data types for Row counters !!
Point taken, I'll watch out for this in future. Thanks
Jan 24 '08 #3
MikeTheBike
639 Expert 512MB
Hi again

There seems to be something spooky going on

I havd noticed to large gap between the ')' and .Row but could not eliminate it inside the code tags !!

What I hadn't noticed was the missing Taget that should be (definitly was when I pasted it!!).

So this is what it should be (maybe!).
Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.  
  3.     Dim FirstRow As Integer
  4.     Dim TotalRows As Long
  5.     TotalRows = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
  6.     FirstRow = 10
  7.  
  8.     If Target.Column = 5 And Target.Row >= FirstRow And Target.Row <= TotalRows Then
  9.         CaAdjustmentButton.Enabled = True
  10.         CaAdjustmentButton.BackColor = 255
  11.     Else
  12.         CaAdjustmentButton.Enabled = False
  13.         CaAdjustmentButton.BackColor = -2147483633
  14.     End If
  15.  
  16. End Sub
No, the extra spaces are still there, but at least the missing Target is now there. That should eliminate the error, hopefully.

MTB
Jan 24 '08 #4
Lara1
11
Yup, the missing target was the problem. I'd even figured it out myself just before you posted (!!!). The code now runs perfectly, and you are a star.
Jan 25 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Ange T | last post by:
Hi there, I'm having pain with the VB behind an Access form. The form is used to create reports in Excel based on the details entered in the form. This has always worked without error on my...
1
by: Rick Brown | last post by:
After reading 30+ threads on the subject and implementing pertinent changes I still have an instance of Excel that won't close. I hope its due to my poor coding and someone can spot the error....
6
by: Matthew Wieder | last post by:
I have the following requirements: Build a stand-alone C# application that asks the user to click in a cell in an Excel spreadsheet, and then displays the address of that cell in the C#...
3
by: Rushi | last post by:
Hi Friends, I don't know how many times I have submitted this topic, but no one is trying to answer me....please some one try to help me. I have created an add in C#, which is popup a form on...
3
by: Boris Condarco | last post by:
Hi gurus, I'm using excel 2000 to show data that comes from datagrid. The problem is that for any reason the asp.net application maintains the excel open, even though, i do close it. Besides,...
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
2
by: ridawg | last post by:
Hey, I have some code where I open up Excel then loop through several cases to update several workbooks. Basically something like this (not showing all the code just basic structure). Try...
3
by: JohnM | last post by:
I can transfer from a query with DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "Filenam", CPath, True I would like to use a form for the user to select and order data then...
5
kadghar
by: kadghar | last post by:
Most of the times VBA is used with variables. Objects (such as worksheets, cells or databases) are only used when we read their properties (value, formula, font...) or we use a method (save,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.