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
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Are you sure that your cell contains the real number 1 and not a number stored as text of 1

All you need to check the column number is to put the formula
Excel Formula:
=COLUMN(FV1)
in a cell
 
Upvote 0
Are you sure that your cell contains the real number 1 and not a number stored as text of 1
Yes. The cells contains the real number 1 or 0 and not a number stored as text
 
Upvote 0
I set up the sheet as below
Book1
FVFWFXFY
101001
l1

and stepped through the code below
VBA Code:
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
           
  MsgBox .Cells(10, col).Address

            End If
        Next col

    End With

    Application.ScreenUpdating = True

End Sub

and the code entered the loop and gave the message box at the correct cell address when stepping through the code
 
Upvote 0
Sir, what errors are in my code. 1 more: you have used Sub instead of Private Sub..should I also use Sub
 
Upvote 0
I can't see any error in the code, I think it is something wrong with the cells.
Change the FV10 in the formulas below to one of the cells with a 1 in it.
What do the formulas return?
Excel Formula:
=LEN(FV10)
Excel Formula:
=ISNUMBER(FV10)

I removed the Private for my testing, it makes no difference to the running of your code
 
Upvote 0
I can't see any error in the code, I think it is something wrong with the cells.
Change the FV10 in the formulas below to one of the cells with a 1 in it.
What do the formulas return?
Excel Formula:
=LEN(FV10)
Excel Formula:
=ISNUMBER(FV10)

I removed the Private for my testing, it makes no difference to the running of your code
I will report you back with these. Plus, should I use Clear Formats in FV10:GZ10?
 
Upvote 0
should I use Clear Formats in FV10:GZ10?
Formats make no difference to the underlying value, having said that if you are using a format to hide decimal values then that would be your issue
 
Upvote 0
I will report you back with these. Plus, should I use Clear Formats in FV10:GZ10?
=LEN(FV10)=1 & =ISNUMBER(FV10) returns TRUE.
I think, the code is not forced to check all cells in FV10:GZ10 fro 1. It is probably only checking FV10 & if FV10<>1 it jumps out.
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,826
Members
449,190
Latest member
rscraig11

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