0

Newbie at vba here. I'm trying to apply a simple For Each loop (which nullifies cells < 0) to all worksheets in the workbook by nesting this inside another For Each loop.

When I try and run my code below I get an error and I'm not sure if it has anything to do with having worksheet as a variable within a Set statement.

Can't seem to figure this out/find a solution.

Thanks

Sub deleteNegativeValue()

Application.DisplayAlerts = False
Dim lastRow As Long
Dim ws As Worksheet
Dim cell As Range
Dim res As Range

For Each ws In Workbooks(1).Worksheets

Set res = ws.Range("1:1").Find("Value", lookat:=xlPart)
lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row

For Each cell In Range(ws.Cells(1, res.Column), ws.Cells(lastRow, res.Column))
If cell < 0 Then cell = ""
Next

Next

End Sub
7
  • What is the error and on which line? Commented Apr 5, 2016 at 14:08
  • Run-time error '91': Object variable or With block variable not set. Error seems to be on the second For Each bit Commented Apr 5, 2016 at 14:15
  • Besides the suggestion below, you will want to put in a test to make sure that "Value" is found, you are probably returning a range object with nothing in it. Commented Apr 5, 2016 at 14:27
  • Instead of Dim cell As Range I think it should be Dim cell As Cell Commented Apr 5, 2016 at 14:29
  • @PaulOgilvie Cell as range is correct. for excel vba. Commented Apr 5, 2016 at 14:32

3 Answers 3

1

Try this:

Sub deleteNegativeValue()

Dim lastRow As Long
Dim ws As Worksheet
Dim cell As Range
Dim res As Range

For Each ws In ThisWorkbook.Worksheets

    Set res = ws.Range("1:1").Find("Value", lookat:=xlPart)
    lastRow = ws.Range("A" & Rows.Count).End(xlUp).row
    If Not res Is Nothing Then
        For Each cell In ws.Range(ws.Cells(1, res.Column), ws.Cells(lastRow, res.Column))
            If cell < 0 Then cell = ""
        Next
    Else
        MsgBox "No Value found on Sheet " & ws.Name
    End If

Next

End Sub

There needs to be a check on the Find method, to ensure that something was found

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for this:)
The check on the Find was definitely needed.
1

you could try this

Option Explicit

Sub deleteNegativeValue()

Dim ws As Worksheet
Dim res As Range

For Each ws In ThisWorkbook.Worksheets
    Set res = Intersect(ws.Rows(1), ws.UsedRange).Find("value", LookAt:=xlPart)
    If Not res Is Nothing Then
        ws.Columns(res.Column).SpecialCells(xlCellTypeConstants, xlNumbers).Replace What:="-*", Replacement:="", SearchOrder:=xlByColumns, MatchCase:=False, LookAt:=xlWhole
    Else
        MsgBox "No Value found on Sheet " & ws.Name
    End If
Next

End Sub

which should run faster since it doesn't iterate through every cell of each column and restrict the Find method range to the used one instead of the entire row.

the only warning is that the first row of all searched in sheets must not be empty...

Comments

0

Try the second for-each this way:

ws.Range(ws.Cells(1, res.Column), ws.Cells(lastRow, res.Column))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.