Looping through multiple worksheet - Not working properly

Madgik

New Member
Joined
Oct 9, 2018
Messages
6
Dear Expert,


I am a newbie to VBA and wanted a bit of code to help me out with some excel files.


It nearly works. It processes and updates the 1st worksheet perfectly but does not process any other worksheets and I have no idea why!


My excel file has 4 columns and lots of worksheets (the layout is the same across all worksheets) :-


Column A - Contains a text description
Column B - Contains the =Len(A1) to give me the length of the string
Column C - Contains a French version of the English text in column A
Column D - Contains the =Len(C1) to give me the length of the French string




What I am trying to achieve is to compare the length of the French strings against the length of the English strings. If the French string is longer than the English then highlight the cell in Column D that is longer in red.


If anyone can help me workout what is wrong that would be great.


The code is as follows :-


Code:
Private Sub CommandButton1_Click()


Dim trans_len As Range
Dim Cell As Range


Dim I As Integer


WS_Count = ActiveWorkbook.Worksheets.Count




For I = 1 To ActiveWorkbook.Worksheets.Count


    Set currentsheet = ActiveWorkbook.Worksheets(I)
    Worksheets(I).Activate


    Set trans_len = Range("D2:D50")
    
    For Each Cell In trans_len
        If Cell.Value > Cell.Offset(, -2).Value Then
            Cell.Interior.ColorIndex = 46
        End If
    Next
    
    MsgBox ActiveWorkbook.Worksheets(I).Name
    
Next I


MsgBox "Finished", vbInformation




End Sub




Cheers
Madgik
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi & welcome to MrExcel
How about
Code:
Private Sub CommandButton1_Click()
   Dim Cell As Range
   Dim I As Long
   
   For I = 1 To ActiveWorkbook.Worksheets.Count
      With ActiveWorkbook.Worksheets(I)
         For Each Cell In .Range("D2:D50")
            If Cell.Value > Cell.Offset(, -2).Value Then
               Cell.Interior.ColorIndex = 46
            End If
         Next Cell
      End With
      MsgBox ActiveWorkbook.Worksheets(I).Name
   Next I
   
   MsgBox "Finished", vbInformation
End Sub
 
Upvote 0
Hi,

Thank you very much for looking at this.

I copied and pasted your code into the excel file but it does not work. It displays the worksheet names but does not perform any of checking to see if one strings is greater then the other or highlight any longer versions in red.

I have no idea why.

Thanks again.
 
Upvote 0
What are the values in col B & D?
 
Upvote 0
This is what is contained in each column :-

Column A - Contains an English text description
Column B - Contains =Len(A1) to give me the length of the string in Column A.
Column C - Contains a French version of the English text.
Column D - Contains =Len(C1) to give me the length of the French string Column C.

I hope this helps.
 
Upvote 0
Are the sheets protected and do you have any merged cells?

As I get


Excel 2013/2016
ABCD
2AL1 5EG7St Albans9
3AL10 9WX8Welwyn Hatfield15
4AL6 0PB7East Hertfordshire18
5AL6 9TU7North Hertfordshire19
6B14 7TW7Birmingham10
7B36 9UQ7Solihull8
8B43 7AN7Walsall7
9B43 7TB7Sandwell8
10B45 9XE7Bromsgrove10
11B46 1UG7North Warwickshire18
12B50 4QF7Stratford-on-Avon17
13B61 9HQ7Wychavon8
14B63 4PS7Dudley6
15B74 4UT7Lichfield9
Sheet1
Cell Formulas
RangeFormula
B2=LEN(A2)
D2=LEN(C2)
 
Upvote 0
The sheets are not protected and there are no merged cells.

There are lots of worksheets, each worksheet is formatted in exactly the same way.
 
Upvote 0
Try this:
Code:
Sub Length()
'Modified 10/9/2018 12:18 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim b as long
Dim Lastrow As Long
For b = 1 To Sheets.Count
With Sheets(b)
    Lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
        For i = 1 To Lastrow
            .Cells(i, 2).Value = Len(.Cells(i, 1))
            .Cells(i, 4).Value = Len(.Cells(i, 3))
        Next
End With
Next
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Do you have any conditional formatting on the sheets?

@MAIT
The OP already has the length in Cols B & D & is trying to highlight some.
 
Upvote 0
Thanks for this. I don't really understand the code but i was about to say that i need to highlight some rows.

What I am trying to achieve is to compare the length of the French strings (column d) against the length of the English strings (Column B). If the French string is longer than the English then highlight the cell in Column D in red.

There is no conditional formatting. This a pain to setup and you would have to do it manually for every row in every worksheet.
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,580
Members
449,089
Latest member
Motoracer88

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