Please correct the code

hsandeep

Well-known Member
Joined
Dec 6, 2008
Messages
1,214
Office Version
  1. 2010
Platform
  1. Windows
  2. Mobile
I am using a code in a Worksheet.
The code:
Rich (BB code):
Private Sub FreezeValues()
    
    Dim sheetName As String, targetRange As Range, targetCell As Range
    sheetName = "l1" ' The name of your sheet

    Application.ScreenUpdating = False

    With ThisWorkbook.Sheets(sheetName)

        Dim col As Integer
        For col = 178 To 208 ' Column numbers for FV to GZ
            If .Cells(10, col).Value = 1 Then
            
                Set targetRange = .Range(.Cells(14, col).Address & ":" & .Cells(28, col).Address)
            
                For Each targetCell In targetRange
                    If Not targetCell.HasFormula Then
                        Application.EnableEvents = False
                        targetCell.Value = targetCell.Value2
                        Application.EnableEvents = True
                    End If
                Next targetCell
        
                Set targetCell = .Cells(65, col)

                If Not targetCell.HasFormula Then
                    Application.EnableEvents = False
                    targetCell.Value = targetCell.Value2
                    Application.EnableEvents = True
                End If
            End If
        Next col

    End With

    Application.ScreenUpdating = True

End Sub

The code line
Rich (BB code):
If .Cells(10, col).Value = 1 Then
Should check if the value in the 10th row and col column of the current worksheet (as defined by the With statement) is equal to 1.
If the condition is true, then the code inside the If block should get executed. If the condition is false, then the code inside the If block should be skipped.
But the code is not entering the code lines after the If statement & jumps to
Rich (BB code):
End If

Next targetCell
PLEASE RECTIFY THE CODE. Also, please check whether the code line is correct or not for column FV:GZ
Rich (BB code):
For col = 178 To 208 ' Column numbers for FV to GZ

Thanks
 
Doesn't for me with
Book1
FVFWFXFY
100001
l1
FV10:GZ10 contains formulas which generates either 0 or 1 like in FV10=
Excel Formula:
=IFERROR(IF(AND(FV4<$FK$4, FV4>$FL$4, FV4>$FM$4), 1, 0),0)
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I ran the code below
VBA Code:
Private Sub FreezeValues()
    
    Dim sheetName As String, targetRange As Range, targetCell As Range
    sheetName = "l1" ' The name of your sheet

    Application.ScreenUpdating = False

    With ThisWorkbook.Sheets(sheetName)

        Dim col As Integer
        For col = 178 To 198 ' Column numbers for FV to GZ
        Debug.Print col
            If .Cells(10, col).Value = 1 Then
            Debug.Print " entered the loop " & .Cells(10, col).Address
            
                Set targetRange = .Range(.Cells(14, col).Address & ":" & .Cells(28, col).Address)
            
                For Each targetCell In targetRange
                    If Not targetCell.HasFormula Then
                        Application.EnableEvents = False
                        targetCell.Value = targetCell.Value2
                        Application.EnableEvents = True
                    End If
                Next targetCell
        
                Set targetCell = .Cells(65, col)

                If Not targetCell.HasFormula Then
                    Application.EnableEvents = False
                    targetCell.Value = targetCell.Value2
                    Application.EnableEvents = True
                End If
            End If
        Next col

    End With

    Application.ScreenUpdating = True

End Sub

with the data
Book1
FKFLFMFNFOFPFQFRFSFTFUFVFWFXFYFZGAGBGCGDGEGFGGGHGIGJGKGLGMGNGOGP
48443538698158107554697687
5
6
71
8
9
10010010001001110101101
l1
Cell Formulas
RangeFormula
FV10:GP10FV10=IFERROR(IF(AND(FV4<$FK$4, FV4>$FL$4, FV4>$FM$4), 1, 0),0)


and got the result
Rich (BB code):
 178 
 179 
 entered the loop $FW$10
 180 
 181 
 182 
 entered the loop $FZ$10
 183 
 184 
 185 
 186 
 entered the loop $GD$10
 187 
 188 
 189 
 entered the loop $GG$10
 190 
 entered the loop $GH$10
 191 
 entered the loop $GI$10
 192 
 193 
 entered the loop $GK$10
 194 
 195 
 entered the loop $GM$10
 196 
 entered the loop $GN$10
 197 
 198 
 entered the loop $GP$10

So the code is definitely entering the loop
 
Upvote 0
If it is going to this line
VBA Code:
Next targetCell
it is not the column number that is the issue, it is the block beginning
VBA Code:
                For Each targetCell In targetRange
                    If Not targetCell.HasFormula Then
 
Upvote 0
So the code is definitely entering the loop
There was mistake in the code (the way I had interpreted the 2 code lines). I changed the code as below, which now works.
Rich (BB code):
Private Sub FreezeValues()
    
    Dim sheetName As String, targetRange As Range, targetCell As Range
    sheetName = "l1" ' The name of your sheet

    Application.ScreenUpdating = False

    With ThisWorkbook.Sheets(sheetName)

        Dim col As Integer
        For col = 178 To 208 ' Column numbers for FV to GZ
            If .Cells(10, col).Value2 = 1 Then
            
                Set targetRange = .Range(.Cells(14, col).Address & ":" & .Cells(28, col).Address)
            
                For Each targetCell In targetRange
                    If targetCell.HasFormula Then
                        Application.EnableEvents = False
                        targetCell.Value = targetCell.Value2
                        Application.EnableEvents = True
                    End If
                Next targetCell
        
                Set targetCell = .Cells(65, col)

                If targetCell.HasFormula Then
                    Application.EnableEvents = False
                    targetCell.Value = targetCell.Value2
                    Application.EnableEvents = True
                End If
            End If
        Next col

    End With

    Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,840
Members
449,193
Latest member
MikeVol

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