simple for next loop wont work

fry

Active Member
Joined
Apr 25, 2007
Messages
411
Hi all, and yes this is pretty basic, but then so am I!

I'm trying to insert some code into a purchase order sheet I have so that it changes the tab colour depending on the contents of one of three cells.

I have code already that does several things (thanks to help from peeps on here) so I thought I'd just pop this code in the appropriate point.............no such luck.

Can anyone help?? :(


Code:
for r = 11 to 13
if Cells(B,r) = "boc" Then
sh.color="blue"   
End if 
Next
 
Hi jonmo

I tried your revision and I still dont get a blue tab???????????

I tried boc in B11, B12 & B13 but to no avail.....there's gremlins in the works somewhere!! :)
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try just changing this line

Sheets("intNewPO").Tab.ColorIndex = 35

to

Activesheet.Tab.ColorIndex = 35
 
Upvote 0
Hi jonmo

semi-success!

It does now turn blue but unfortunately it doesn't matter whats in the cell, it turns blue anyway????????? :)
 
Upvote 0
Code:
Dim intNewPO As Integer 
Dim strActiveSheet As String 
intNewPO = Sheets(4).Name + 1 
Sheets(1).Copy After:=Sheets(1) 
ActiveSheet.Name = intNewPO 
strActiveSheet = ActiveSheet.Name 
ActiveSheet.Tab.ColorIndex = 43 

For r = 11 To 13 
On Error Resume Next
x = WorksheetFunction.Search("boc", Cells(r, "B"))
if x > 0 Then
ActiveSheet.Tab.ColorIndex = 35 
End If 
Next
 
Upvote 0
Works a treat!!

Thanks a lot Jonmo!!

Have a great day...

P.S. I don't suppose you know the code for closing a workbook by any chance. My worksheet is only used by about 6 people but sometimes someone leaves it open therefore stopping other people from using it so I'm thinking of automatically shutting it down. Thanks very much once again. :)
 
Upvote 0
Look at the Macro Recorder. tools - Macros - Record New Macro..


about 1/3 of the VBA code I know, I learned with that...

Of coarse, make sure you start recording the macro in a workbook OTHER THAN the one you close...
 
Upvote 0
Cheers jonmo, a good idea..........speaking of which did you use any particular book to learn further???

:)
 
Upvote 0
Nope, just the Macro Recorder and this Forum.

Another great way to learn is to challenge yourself. Try to make a game.

I made a Chess Game and a Sudoku Game. That helped me learn alot.
 
Upvote 0
Hi Jonmo

I just tried the workbook shutdown like you suggested but upon choosing FILE|CLOSE it asks if I want to save the workbook (which I don't want) and I can't get rid of that.

Regardless of that I carried on and then went back to the macro to find this...

Code:
Sub shutdown()
'
' shutdown Macro
' Macro recorded 24/07/2007 by Pete
'

'
End Sub
 
Upvote 0
activeworkbook.close - will close whichever workbook is currently active.

workbooks("boookname.xls").close - will close a specified workbook...


adding
application.displayalerts = false before that will make it not prompt for save.

so

Code:
application.displayalerts = false
workbooks("bookname.xls").close
application.displayalerts = true
 
Upvote 0

Forum statistics

Threads
1,224,798
Messages
6,181,038
Members
453,014
Latest member
Chris258

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