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: -
Private Sub Worksheet_Change(ByVal Target As Range)
-
-
Dim FirstRow As Double
-
Dim TotalRows As Double
-
Dim Row As Double
-
-
TotalRows = Worksheets("Calculation").UsedRange.Rows.Count
-
FirstRow = 10
-
-
For Row = FirstRow To TotalRows Step 1
-
If Target.Address = Worksheets("Calculation").Cells(Row, 5) Then
-
CaAdjustmentButton.Enabled = True
-
CaAdjustmentButton.BackColor = "&H0000000FF&"
-
Else: Exit Sub
-
End If
-
Next Row
-
-
End Sub
-
-
4 5216
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: -
Private Sub Worksheet_Change(ByVal Target As Range)
-
-
Dim FirstRow As Double
-
Dim TotalRows As Double
-
Dim Row As Double
-
-
TotalRows = Worksheets("Calculation").UsedRange.Rows.Count
-
FirstRow = 10
-
-
For Row = FirstRow To TotalRows Step 1
-
If Target.Address = Worksheets("Calculation").Cells(Row, 5) Then
-
CaAdjustmentButton.Enabled = True
-
CaAdjustmentButton.BackColor = "&H0000000FF&"
-
Else: Exit Sub
-
End If
-
Next Row
-
-
End Sub
-
-
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 ?? - Private Sub Worksheet_Change(ByVal Target As Range)
-
-
Dim FirstRow As Integer
-
Dim TotalRows As Long
-
-
TotalRows = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
-
FirstRow = 10
-
-
If .Column = 5 And Target.Row >= FirstRow And Target.Row <= TotalRows Then
-
CaAdjustmentButton.Enabled = True
-
CaAdjustmentButton.BackColor = 255
-
Else
-
CaAdjustmentButton.Enabled = False
-
CaAdjustmentButton.BackColor = -2147483633
-
End If
-
-
End Sub
Note the use of Integer/Long data types for Row counters !!
HTH
MTB
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 ?? - Private Sub Worksheet_Change(ByVal Target As Range)
-
-
Dim FirstRow As Integer
-
Dim TotalRows As Long
-
-
TotalRows = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
-
FirstRow = 10
-
-
If .Column = 5 And Target.Row >= FirstRow And Target.Row <= TotalRows Then
-
CaAdjustmentButton.Enabled = True
-
CaAdjustmentButton.BackColor = 255
-
Else
-
CaAdjustmentButton.Enabled = False
-
CaAdjustmentButton.BackColor = -2147483633
-
End If
-
-
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
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!). - Private Sub Worksheet_Change(ByVal Target As Range)
-
-
Dim FirstRow As Integer
-
Dim TotalRows As Long
-
TotalRows = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
-
FirstRow = 10
-
-
If Target.Column = 5 And Target.Row >= FirstRow And Target.Row <= TotalRows Then
-
CaAdjustmentButton.Enabled = True
-
CaAdjustmentButton.BackColor = 255
-
Else
-
CaAdjustmentButton.Enabled = False
-
CaAdjustmentButton.BackColor = -2147483633
-
End If
-
-
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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....
|
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#...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |