Hide Rows inbetween two cell in one coloumn

harry_ralphson

New Member
Joined
Feb 4, 2013
Messages
4
Hi,

I am writing a VBA code to restructure the multilevel BOM dump taken out from SAP. In this data the number of coloumn's are fixed and row numbers are dynamic depending upon the size of BOM data
I have finished with my code, except one part where i am stuck. I tried various things but its not working.
In my data, coloumn A indicates the BOM level, where the BOm levels are listed out. The sequence is random.
Here i want to hide the rows after each "..2" and before ".1" including the "..2" row itself.

I have tried For each, do until options, but its not working.

I am a newbee to VBA.

Any help would be appreciated, Thanks


BOM Level
Material
Qty
.1
..2
.1
..2
..2
..2
.1
..2
..2
..2

<TBODY>
</TBODY>
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hello hippiehacker,

Thanks for your reply!

Basically empty rows below each "..2" belongs to it and i want to hide them all. So in below sheet if i remove each "..2" and empty rows below each "..2" until next ".1" then it will look like as below. additionally, if you notice, the empty rows below each ".1" will remain as it is.

Hoep this is more clear now.
BOM LevelMaterialQty
.1
.1
.1


<tbody>
</tbody>
 
Upvote 0
like this?

Code:
Sub ralph()
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
Debug.Print Range("A" & Rows.Count).End(xlUp).Row
 If Cells(i, "A").Value = "..2" Then
 Set myr1 = Range(Cells(i, "A").Address)

 For j = myr1.Row To Range("A" & Rows.Count).End(xlUp).Row
  If Cells(j, "A").Value = ".1" Then
    Set myr2 = Range(Cells(j - 1, "A").Address)
    Exit For
 End If
 If j = Range("A" & Rows.Count).End(xlUp).Row Then
  Set myr2 = Range(Cells(j, "A").Address)
  Exit For
  End If
 Next j
 Range(myr1, myr2).Rows.Hidden = True
 i = myr2.Row
 End If
Next i
End Sub
 
Upvote 0
like this?

Code:
Sub ralph()
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
Debug.Print Range("A" & Rows.Count).End(xlUp).Row
 If Cells(i, "A").Value = "..2" Then
 Set myr1 = Range(Cells(i, "A").Address)

 For j = myr1.Row To Range("A" & Rows.Count).End(xlUp).Row
  If Cells(j, "A").Value = ".1" Then
    Set myr2 = Range(Cells(j - 1, "A").Address)
    Exit For
 End If
 If j = Range("A" & Rows.Count).End(xlUp).Row Then
  Set myr2 = Range(Cells(j, "A").Address)
  Exit For
  End If
 Next j
 Range(myr1, myr2).Rows.Hidden = True
 i = myr2.Row
 End If
Next i
End Sub

Hello hippiehacker

It works like a rocket!!!. Thanks a lot!

request you one more favour. could you please explain me the logic of the code?

Once again thanks!
Regards
Harry.
 
Upvote 0
with explanation

Code:
Sub ralph()
'first for loop to look in column A for '..2'
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
'if cell in column a = "..2" then set range myr1 to Cell address
 If Cells(i, "A").Value = "..2" Then
 Set myr1 = Range(Cells(i, "A").Address)
 'second for loop to look in column A for '.1' after '..2' has been found
 For j = myr1.Row To Range("A" & Rows.Count).End(xlUp).Row
  'if cell in column a = ".1" then set range myr2 to Cell address
  If Cells(j, "A").Value = ".1" Then
    Set myr2 = Range(Cells(j - 1, "A").Address)
    Exit For
 End If
 'checking for loop to not go further then lastrow in Column A  if lastrow is reached and Value_
 '".1" could not be found anymore we set the myr2 to the last cell
 If j = Range("A" & Rows.Count).End(xlUp).Row Then
  Set myr2 = Range(Cells(j, "A").Address)
  Exit For
  End If
 Next j
 'as ranges myr1 and myr2 has been set we can hide the rows
 Range(myr1, myr2).Rows.Hidden = True
 'set i for the first loop to the lastrow which has been checked in the second loop to not loop again the same cells
 i = myr2.Row
 End If
Next i
End Sub
 
Upvote 0
with explanation

Code:
Sub ralph()
'first for loop to look in column A for '..2'
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
'if cell in column a = "..2" then set range myr1 to Cell address
 If Cells(i, "A").Value = "..2" Then
 Set myr1 = Range(Cells(i, "A").Address)
 'second for loop to look in column A for '.1' after '..2' has been found
 For j = myr1.Row To Range("A" & Rows.Count).End(xlUp).Row
  'if cell in column a = ".1" then set range myr2 to Cell address
  If Cells(j, "A").Value = ".1" Then
    Set myr2 = Range(Cells(j - 1, "A").Address)
    Exit For
 End If
 [COLOR=#0000ff]'checking for loop to not go further then lastrow in Column A  if lastrow is reached and Value_
 '".1" could not be found anymore we set the myr2 to the last cell
[/COLOR] If j = Range("A" & Rows.Count).End(xlUp).Row Then
  Set myr2 = Range(Cells(j, "A").Address)
  Exit For
  End If
 Next j
 'as ranges myr1 and myr2 has been set we can hide the rows
 Range(myr1, myr2).Rows.Hidden = True
 'set i for the first loop to the lastrow which has been checked in the second loop to not loop again the same cells
 i = myr2.Row
 End If
Next i
End Sub

Thanks hippiehacker,

i was trying to do it using a Do loop and it was going for infinite loops as there was no limit of last row of table. With your explanation, now i know whats missing in my code.
its good learning for me.
Thank you!
Harry
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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