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>
 

hippiehacker

Well-known Member
Joined
Aug 2, 2011
Messages
1,911
can you post how it should look after the rows are hidden from your post its a bit unclear to me
 

harry_ralphson

New Member
Joined
Feb 4, 2013
Messages
4
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>
 

hippiehacker

Well-known Member
Joined
Aug 2, 2011
Messages
1,911
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
 

harry_ralphson

New Member
Joined
Feb 4, 2013
Messages
4
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.
 

hippiehacker

Well-known Member
Joined
Aug 2, 2011
Messages
1,911
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
 

harry_ralphson

New Member
Joined
Feb 4, 2013
Messages
4
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
 

Forum statistics

Threads
1,082,380
Messages
5,365,124
Members
400,824
Latest member
Themilkybarkid

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top