SpecialCells Loop not working

WindsorKnot

Board Regular
Joined
Jan 4, 2009
Messages
160
Hi,

I'm getting an error in my code below.

I basically want to determine if the values in column F on the "50000 sheet are > 50000 and if so to copy them into the "Storage" sheet in column A and loop until finished

The error is highlighted in red below- Object Variable or with Block Variable not set




Code:
Sub GetValue2()
    Dim I As Integer
    Dim Spcell As Range
    I = 2
    Dim R As Worksheet
    Set R = Sheets("50000")
     Do Until R.Range("F1").Value = "Total"
        [COLOR=red]If Spcell.SpecialCells(xlCellTypeConstants, xlValues) > 50000 Then[/COLOR]
            Sheets("storage").Cells(I, 1).Value = Spcell.Value
        End If
        I = I + 1
    Loop
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try

Code:
Sub GetValue2()
    Dim I As Integer
    Dim Spcell As Range
    Dim cell As Range
    I = 2
    Dim R As Worksheet
    Set R = Sheets("50000")
    Set Spcell = R.Range("F;F")
    For Each cell In Spcell.SpecialCells(xlCellTypeConstants, xlValues)
        If cell.Range("F1").Value = "Total" Then Exit For
        If Spcell > 50000 Then
        
            Sheets("storage").Cells(I, 1).Value = Spcell.Value
            I = I + 1
        End If
    Next cell
End Sub
 
Upvote 0
See if this is better

Code:
Sub GetValue2()
    Dim I As Integer
    Dim Spcell As Range
    Dim cell As Range
    I = 2
    Dim R As Worksheet
    Set R = Sheets("50000")
    Set Spcell = R.Range("F:F")
    For Each cell In Spcell.SpecialCells(xlCellTypeConstants)
        If cell.Range("F1").Value = "Total" Then Exit For
        If cell.Value > 50000 Then
        
            Sheets("storage").Cells(I, 1).Value = Spcell.Value
            I = I + 1
        End If
    Next cell
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,378
Messages
6,124,604
Members
449,174
Latest member
ExcelfromGermany

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