VBA help

Emmily

Well-known Member
Joined
Oct 5, 2008
Messages
676
Hi, i need a vba code that will look in Col A5:A, if no data is found under neath the header "Group" then delete entire row. The desired result is shown in the second screenshot

Excel Workbook
A
5Group
6BHO
7BHO
8BHO
9BHO
10
11Group
12
13Group
14RIO
15RIO
16RIO
17
18Group
19
20Group
21
22Group
23CBA
24CBA
25CBA
26CBA
Sheet1


Excel Workbook
A
5Group
6BHO
7BHO
8BHO
9BHO
10
11Group
12RIO
13RIO
14RIO
15
16Group
17CBA
18CBA
19CBA
20CBA
21
Sheet1
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
See if this does what you want:

<font face=Calibri><SPAN style="color:#00007F">Sub</SPAN> foo()<br>    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> i = Cells(Rows.Count, "A").End(xlUp).Row <SPAN style="color:#00007F">To</SPAN> 2 <SPAN style="color:#00007F">Step</SPAN> -1<br>            <SPAN style="color:#00007F">If</SPAN> Cells(i, "A").Value = "Group" And LenB(Cells(i, "A").Offset(1).Value) = 0 <SPAN style="color:#00007F">Then</SPAN><br>                Cells(i, "A").Resize(2).EntireRow.Delete<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> i<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

HTH,
 
Upvote 0
Smitty, can the code be expanded to do the same in two other worksheets (Test2,Test3) so in total there are 3 worksheets.


See if this does what you want:

Sub foo()
Dim i As Long
For i = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
If Cells(i, "A").Value = "Group" And LenB(Cells(i, "A").Offset(1).Value) = 0 Then
Cells(i, "A").Resize(2).EntireRow.Delete
End If
Next i
End Sub


HTH,
 
Upvote 0
Just add another loop.

E.G.

Code:
For x = 1 to 3
  With Sheets("Test"&x)
     ' other code
  End with
Next x
 
Upvote 0
Test1,2 and 3 are just sheet examples, actual naming convention of sheets is "

"BHP", "RIO" and "CBA"
 
Upvote 0
How about...

Code:
Sub Test2()
    Dim myWs As Worksheet
    For Each myWs In Sheets(Array("BHP", "RIO", "CBA"))
     ' other code
    Next myWs
End Sub
 
Upvote 0
Ok, i have this, but when i run the macro from another sheet which are not the sheets mentioned in the code then the code does nothing

Code:
Sub foo()
    Dim i As Long
    Dim myWs As Worksheet
    
    For Each myWs In Sheets(Array("BHP", "RIO", "CBA"))
        For i = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
            If Cells(i, "A").Value = "Group" And LenB(Cells(i, "A").Offset(1).Value) = 0 Then
                Cells(i, "A").Resize(2).EntireRow.Delete
            End If
        Next i
    Next myWs
End Sub
 
Upvote 0
It is most likely because you have to set your qualifiers by adding the .

Code:
Sub foo()
    Dim i As Long
    Dim myWs As Worksheet
    
    For Each myWs In Sheets(Array("BHP", "RIO", "CBA"))
        For i = [COLOR="Red"].[/COLOR]Cells([COLOR="red"].[/COLOR]Rows.Count, "A").End(xlUp).Row To 2 Step -1
            If [COLOR="red"].[/COLOR]Cells(i, "A").Value = "Group" And LenB([COLOR="red"].[/COLOR]Cells(i, "A").Offset(1).Value) = 0 Then
                [COLOR="red"].[/COLOR]Cells(i, "A").Resize(2).EntireRow.Delete
            End If
        Next i
    Next myWs
End Sub
 
Upvote 0
I get a compile error "Invalid or unqualified reference

For i = .Cells(.Rows.Count, "A").End(xlUp).Row To 2 Step -1
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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