Chat with a LIVE Microsoft Access Expert!
 
       
 

         

   

Go Back   Access World Forums > Apps and Windows > Excel

 
 
Chat with a LIVE Microsoft Access Expert!
Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 11-21-2009, 06:23 PM
Tanya Tanya is offline
Access Novice
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 165
Tanya is on a distinguished road
Update format of cells to reflect change

Hi
I have a spreadsheet and need to archive the records on several worksheets without removing them. To do this I would like to format the text on each record to strikethrough. I have written this code but it isn't working right:

Sub Archive_Calculate()
Dim r As Range, cell As Range
On Error GoTo ErrHandler

'Look up range C5:C44
Set r = Me.Range("C5:C44")
Application.ScreenUpdating = False
Application.EnableEvents = False

'Wherever the Letter A occurs, select entire row
'and format Font as Strikethrough
For Each cell In r
If cell.Value = "A" Then
cell.EntireRow.Select
.Font.Strikethrough
End If
End With

ErrHandler:
Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub

I then need to update 5 other sheets; Sheet names are Grades, Term1, Term2, Term3, Term4

Any suggestions appreciated as I am very new at vba.

Tanya
Reply With Quote
Sponsored Links
  #2  
Old 11-22-2009, 07:21 AM
Brianwarnock's Avatar
Brianwarnock Brianwarnock is offline
Retired
 
Join Date: Jun 2003
Location: Merseyside England
Posts: 7,596
Brianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the rough
Re: Update format of cells to reflect change

If cell.Value = "A" Then
cell.EntireRow.Font.Strikethrough = True
End If


Oh and remove the me. from range

Set r = Range("C5:C44")

I don't like to use cell as a variant as it is too close to CELLS, a keyword ,and can confuse debugging, also lead to typo errors, I would just use c.



Brian
__________________
What is this life if, full of care,
We have no time to stand and stare
Reply With Quote
  #3  
Old 11-22-2009, 10:17 AM
Tanya Tanya is offline
Access Novice
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 165
Tanya is on a distinguished road
Re: Update format of cells to reflect change

Thank you Brian,
It worked perfectly.
Reply With Quote
  #4  
Old 11-23-2009, 04:47 AM
Brianwarnock's Avatar
Brianwarnock Brianwarnock is offline
Retired
 
Join Date: Jun 2003
Location: Merseyside England
Posts: 7,596
Brianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the rough
Re: Update format of cells to reflect change

Thanks for coming back, its amazing how rare that is on the Ecxel forum.
BTW if you want to do all of the sheets in one go and the tange is the same you could loop through the sheet names stored in an array


Code:
Dim myarray As Variant
myarray = Array("sheet1", "sheet2", "sheet3")
Dim n As Integer

n=0
Do While n < 3
Set r = Sheets(myarray(n)).Range("C5:C44")
Application.ScreenUpdating = False
Application.EnableEvents = False

'Wherever the Letter A occurs, select entire row
'and format Font as Strikethrough
For Each cell In r
If cell.Value = "A" Then
cell.EntireRow.Font.Strikethrough = True
End If
Next
n = n + 1
Loop
approach, obviously altering code as necessary for your situation.
Just a thought.

Brian
__________________
What is this life if, full of care,
We have no time to stand and stare

Last edited by Brianwarnock; 11-24-2009 at 05:35 AM.. Reason: code correction
Reply With Quote
  #5  
Old 11-24-2009, 12:20 AM
Tanya Tanya is offline
Access Novice
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 165
Tanya is on a distinguished road
Re: Update format of cells to reflect change

Hi Brian
I really appreciate your support with this project.
I'm struggling though with getting the code to run when text is changed and have attached a copy of my project for your consideration. The main sheet is Grades and Term 1, Term 2 etc get updated information from this main sheet.
Perhaps I have used a wrong expression? I placed the code you gave me in Module1 and if I run it from within VBA it works perfectly.
Thank you in advance.
Kind regards
Tanya
Attached Files
File Type: zip Attendance & Grades.zip (139.6 KB, 8 views)
Reply With Quote
  #6  
Old 11-24-2009, 06:09 AM
Brianwarnock's Avatar
Brianwarnock Brianwarnock is offline
Retired
 
Join Date: Jun 2003
Location: Merseyside England
Posts: 7,596
Brianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the rough
Re: Update format of cells to reflect change

Hi

There was an error in my air code, as N starts at 0 then the Do While n < 3 not 4, presumably it did not cause an error or maybe you corrected the "Senior Moment" in your code.

I don't really understand what you are asking, and I am not on 2007 and the file conversion is apparently not working properly so not sure that I can help.

Brian
__________________
What is this life if, full of care,
We have no time to stand and stare
Reply With Quote
  #7  
Old 11-24-2009, 10:57 AM
Tanya Tanya is offline
Access Novice
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 165
Tanya is on a distinguished road
Re: Update format of cells to reflect change

Hi Brian

Thanks for the quick response.

I had noticed the 'senior moment' but it was easily fixed.
I have saved my file as an xls 2003 file so that you can take a closer look at my code. My first question is 'do I need to set up a 'change event' to envoke this code?

Since we often have movement with class numbers, the students that leave must be archived and not deleted, hence I am trying to use strikethrough.

Regards
Tanya
Attached Files
File Type: xls Attendance & Grades.xls (453.5 KB, 7 views)
Reply With Quote
  #8  
Old 11-24-2009, 02:32 PM
chergh's Avatar
chergh chergh is online now
Registered User
 
Join Date: Jun 2004
Location: Newbury
Posts: 1,414
chergh will become famous soon enoughchergh will become famous soon enough
Re: Update format of cells to reflect change

I think you actually need to use 2 events here, the workbook sheetchange event and the workbook sheetcalculate event.

You need the sheetchange event for when you change the value to "A" and the sheetcalculate event to update other cells which are linked to where you are changing the values.

Put the following in the ThisWorkbook module:

Code:
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)

Dim rng As Range

For Each rng In Intersect(Sh.Columns("C"), Sh.UsedRange)

    If rng.Value = "A" Then
        
        rng.EntireRow.Font.Strikethrough = True
    
    End If
    
Next rng

 
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If CBool(InStr(1, Sh.Name, "Term")) Or _
   Sh.Name = "Grades" Then

    If Not Intersect(Target, Sh.Range("C5:C44")) Is Nothing Then
    
        If Target.Value = "A" Then
            
            Target.EntireRow.Font.Strikethrough = True
        
        End If
    
    End If
    
End If
Hopefully that should do the job for you.
__________________
In the town where I was born, Lived a man who went to work
And he told us of his life, Building spaceships on £40 a week

Prag Vec at the Melkweg
Half Man Half Biscuit
Reply With Quote
  #9  
Old 11-24-2009, 08:24 PM
Tanya Tanya is offline
Access Novice
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 165
Tanya is on a distinguished road
Re: Update format of cells to reflect change

Hi
Thank you for your response to my post. Unfortunately it didn't work, but that may be because I didn't understand your code and hence didn't replace anything.
Regards
Tanya
Reply With Quote
  #10  
Old 11-25-2009, 03:33 AM
chergh's Avatar
chergh chergh is online now
Registered User
 
Join Date: Jun 2004
Location: Newbury
Posts: 1,414
chergh will become famous soon enoughchergh will become famous soon enough
Re: Update format of cells to reflect change

I think this is what you were wanting.
Attached Files
File Type: xls Copy of Attendance & Grades.xls (454.5 KB, 8 views)
__________________
In the town where I was born, Lived a man who went to work
And he told us of his life, Building spaceships on £40 a week

Prag Vec at the Melkweg
Half Man Half Biscuit
Reply With Quote
  #11  
Old 11-25-2009, 06:30 AM
Brianwarnock's Avatar
Brianwarnock Brianwarnock is offline
Retired
 
Join Date: Jun 2003
Location: Merseyside England
Posts: 7,596
Brianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the rough
Re: Update format of cells to reflect change

Hi Tanya

Didin't appreciate that you wanted the change refected on change, I had assumed that a block of changes would be done and then the code run from a button either on thetoobar or in the workbook.

Chergh's code is as usual good, he is better than me.
However if this is a 3D workbook with each sheet having the same ref on each row then the following code in Grades module will work, however it requires change if more TERM sheets added.

Code:
Private Sub worksheet_change(ByVal target As Range)

If Not Intersect(target, Range("C5:C44")) Is Nothing Then
    r = target.Row
        If target.Value = "A" Then
        
            
            Sheets("Grades").Rows(r & ":" & r).EntireRow.Font.Strikethrough = True
            Sheets("Term 1").Rows(r & ":" & r).EntireRow.Font.Strikethrough = True
            Sheets("Term 2").Rows(r & ":" & r).EntireRow.Font.Strikethrough = True
            'etc
        Else
            Sheets("Grades").Rows(r & ":" & r).EntireRow.Font.Strikethrough = False
            Sheets("Term 1").Rows(r & ":" & r).EntireRow.Font.Strikethrough = False
            Sheets("Term 2").Rows(r & ":" & r).EntireRow.Font.Strikethrough = False
            'etc
        End If
 End If
 
        End Sub

Brian
__________________
What is this life if, full of care,
We have no time to stand and stare
Reply With Quote
  #12  
Old 11-25-2009, 09:28 AM
Tanya Tanya is offline
Access Novice
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 165
Tanya is on a distinguished road
Re: Update format of cells to reflect change

Thank you Chergh, it works first time.
However, I now need to be able to protect the document and would usually refer to module1.unprotect_worksheet & module1.protect_worksheet but can't work out why I keep getting error when code realises that column H doesn't have the value A. I have tried placing these two references to module one in various stages of your code with no luck. Sorry for the hassle, could you take a peek at this for me please.

Here is the code for Module1
Sub UnProtect_Workbook()
'Unprotect workbook
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = True Then
ws.Unprotect ("BBHS")
End If
Next
ActiveWorkbook.Unprotect (["BBHS"])
Application.ScreenUpdating = True
End Sub
Sub Protect_Workbook()
'Protect workbook
Dim ws As Worksheet
Const PWORD As String = "BBHS"
For Each ws In ActiveWorkbook.Worksheets
With ws
If ws.ProtectContents = False Then
.EnableSelection = xlUnlockedCells
.Protect Password:=PWORD
End If
End With
Next ws
ActiveWorkbook.Protect Password:=PWORD
End Sub

Thank you

Kind regards
Tanya
Reply With Quote
  #13  
Old 11-25-2009, 09:31 AM
Tanya Tanya is offline
Access Novice
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 165
Tanya is on a distinguished road
Re: Update format of cells to reflect change

Hi Brian
Thank you for this code, it worked a treat, however as with Chergh's code I keep getting a halt when column C has a value other than "A".
I feel like I am being a pain here, but I would really appreciate it if you could take a look at how I am trying to unprotect then protect my workbook and see if there is a way to get around this.
Kind regards
Tanya
Reply With Quote
  #14  
Old 11-25-2009, 09:46 AM
Tanya Tanya is offline
Access Novice
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 165
Tanya is on a distinguished road
Re: Update format of cells to reflect change

I found the answer

By removing this line:
rng.EntireRow.Font.Strikethrough = False
I have eliminated any errors and it works a treat. Now if the status of a student is changed to "A" the row is formatted to strikethrough and if I change it back to "M" or "F" the strikethrough is changed.

Thank you soooo much chergh and Brian, I couldn't have done it without you both.

Kind regards
Tanya
Reply With Quote
  #15  
Old 11-25-2009, 11:15 AM
Tanya Tanya is offline
Access Novice
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 165
Tanya is on a distinguished road
Re: Update format of cells to reflect change

When I run Chergh's code I get the following error:
"Unable to set the "strikethrough property of the font Class" hence I removed that line and all appears to work, but I have just realised when I change "A" back to "F" or "M" it doesn't update Term 1, Term 2 etc

Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Dim rng As Range
Debug.Print Sh.Name
For Each rng In Intersect(Sh.Columns("C"), Sh.UsedRange)

If rng.Value = "A" Then
Module1.UnProtect_Workbook

rng.EntireRow.Font.Strikethrough = True


Else

rng.EntireRow.Font.Strikethrough = False 'This is the line that is causing the _
error

End If


Next rng
Module1.Protect_Workbook

End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Module1.UnProtect_Workbook
If CBool(InStr(1, Sh.Name, "Term")) Or _
Sh.Name = "Grades" Then
If Not Intersect(Target, Sh.Range("C5:C44")) Is Nothing Then

If Target.Value = "A" Then

Target.EntireRow.Font.Strikethrough = True


Else

Target.EntireRow.Font.Strikethrough = False

End If

End If



End If
Module1.Protect_Workbook
End Sub


Any suggestions?

Thanks in advance.
Reply With Quote
Sponsored Links
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to programmatically change the shortdate format in the regional settings genesis Modules & VBA 2 11-06-2009 12:42 AM
Update Query and Empty Cells batwings Queries 8 07-01-2009 09:33 AM
Unable to Format Cells MGumbrell Excel 2 03-05-2009 02:27 AM
Change the format of MS Excel Cells from MS Access hewstone999 Modules & VBA 1 03-07-2008 02:29 AM
how to change date format? chris_8 General 2 02-13-2008 06:39 PM


All times are GMT -8. The time now is 09:56 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
(c) copyright 2009 Access World