VBA help needed urgently

asad

Well-known Member
Joined
Sep 9, 2008
Messages
1,434
Hi Guys,

I have recorded the following macro
Code:
Sub Macro1()    Cells.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("C1").Select
    Range("$A$1:$C$500").AutoFilter Field:=3, Criteria1:="="
    Rows("3:3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlUp
    Range("$A$1:$C$449").AutoFilter Field:=3
    Range("D2").Select
End Sub

Can I please get this to work on all the sheets in the workbook?

Thanks for your help
Asad
 
Thanks for that Mark.

One more favour, if may please.
Can I say in code - all sheets except for Master and Info?
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Thanks for that Mark.

One more favour, if may please.
Can I say in code - all sheets except for Master and Info?

Code:
Sub Macro2()
    Dim ws As Worksheet
    Application.ScreenUpdating = False

    For Each ws In ActiveWorkbook.Worksheets
        [COLOR="#FF0000"]If ws.Name <> "Master" Or ws.Name <> "Info" Then[/COLOR]
            On Error Resume Next
            ws.Range("C3:C" & ws.Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(12).EntireRow.Delete
            On Error GoTo 0
        [COLOR="#FF0000"]End If[/COLOR]
    Next

    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Michael and Mark,

I have been testing all the different codes given by both of you. I am not getting any of those to do exactly what is intended.

Code:
[COLOR=#333333]Sub Macro1()[/COLOR]Dim ws As Worksheet
For Each ws In Worksheets
ws.Activate
    With Cells
    .Value = .Value
    End With
    Range("C1").Select
    Range("$A$1:$C$500").AutoFilter Field:=3, Criteria1:="="
    Rows("3:3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlUp
    Range("$A$1:$C$449").AutoFilter Field:=3
    Range("D2").Select
Next ws [COLOR=#333333]End Sub[/COLOR]
This one does not replace formulas with values, and .Value = .Value gets highlighted.

Code:
[COLOR=#574123]Sub Macro2()[/COLOR]    Dim ws As Worksheet
    Application.ScreenUpdating = False

    For Each ws In ActiveWorkbook.Worksheets
        [COLOR=#FF0000]If ws.Name <> "Master" Or ws.Name <> "Info" Then[/COLOR]
            On Error Resume Next
            ws.Range("C3:C" & ws.Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(12).EntireRow.Delete
            On Error GoTo 0
        [COLOR=#FF0000]End If[/COLOR]
    Next

    Application.ScreenUpdating = True [COLOR=#574123]End Sub[/COLOR]
This one is deleting all the rows in range, even the ones that have data.

My spreadsheet is set up so that formula in column B is looking at column C, and if blank then returns the value from next row in column A.
Then there are formulas in columns D, E, F and G as well. All of them look at Column C and return the values accordingly. Now I want all of these formulas in columns B, D, E, F, and G to be changed to values so that I do not lose the results. And then, I want to delete the rows that have blank cells in column C.

Thanks for your help.
Asad
 
Upvote 0
Try
Code:
Sub MM1()
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In Worksheets
    If ws.Name <> "Master" Or ws.Name <> "Info" Then
        ws.Activate
            With ws.UsedRange
                .Value = .Value
            End With
            With Range("C1", Cells(Rows.Count, "C").End(xlUp))
                .Replace "", "#N/A", xlWhole, , False, , False, False
            Columns("C").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
        End With
    End If
Next ws
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try
Code:
Sub MM1()
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In Worksheets
    If ws.Name <> "Master" Or ws.Name <> "Info" Then
        ws.Activate
            With ws.UsedRange
                .Value = .Value
            End With
            With Range("C1", Cells(Rows.Count, "C").End(xlUp))
                .Replace "", "#N/A", xlWhole, , False, , False, False
[COLOR=#ff0000]            Columns("C").SpecialCells(xlConstants, xlErrors).EntireRow.Delete[/COLOR]
        End With
    End If
Next ws
Application.ScreenUpdating = True
End Sub

Hi Michael,

This also resulted in an error. The line with the red fonts above gets highlighted and the formulas are still there.

Sorry for all this. But if you can get this to work, then that will be a big help.

Thanks
Asad
 
Upvote 0
Hi Michael,

I have changed your macro a little bit and now it looks like this
Code:
Sub Data()    Dim ws As Worksheet
    Application.ScreenUpdating = False


    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "Master" Or ws.Name <> "Info"  Then
        ws.Activate
            With ws
                ws.Range("A1:F500").Value = ws.Range("A1:F500").Value
                Range("C1").Select
                Range("$A$1:$C$500").AutoFilter Field:=3, Criteria1:="="
                Rows("3:3").Select
                Range(Selection, Selection.End(xlDown)).Select
                Application.CutCopyMode = False
                Selection.Delete Shift:=xlUp
                Range("$A$1:$C$449").AutoFilter Field:=3
                Range("D2").Select
            End With
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub

And it worked.
But I am sure, you can make it better. I will wait for your solution before I do anything to all the files.

Thanks
Asad
 
Upvote 0
If you step through my code...the lines
Code:
With ws.UsedRange
 .Value = .Value
End With
will remove all formulas and replace them with the values....and it works fine for me, remembering that Master and Info sheets will be unaffected

When that is done are the cells to be removed in col "C" blank or Zeros

If you are still having issues please upload a copy of the worksheet in question to Dropbox or similar and I'll look into it further
 
Last edited:
Upvote 0
If you step through my code...the lines
Code:
With ws.UsedRange
 .Value = .Value
End With
will remove all formulas and replace them with the values....and it works fine for me, remembering that Master and Info sheets will be unaffected

When that is done are the cells to be removed in col "C" blank or Zeros

If you are still having issues please upload a copy of the worksheet in question to Dropbox or similar and I'll look into it further

The cells in column C are blanks. No zeros.
 
Upvote 0
In that cese the code in Post #14 should work fine...it does for me !!
Upload to dropbox if you have any further problems
 
Upvote 0
Thanks Michael

Got it to work now.

I had a corrupt tab in the file. I deleted that and it works fine now. Sorry about the unnecessary botheration.

Thanks for your help.

Regards
Asad
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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