Calc macro to hide multiple rows based on a cell's value?

@KamilLanda has hard-coded the task. Very bad, because it is “hard-coded”. Let’s thank him for the fact that he still wanted to do it.
@Bort, we can all do it, as he did, but I would not do it, because we are not only answering questions, but also engaged in self-development, and we want to move forward on the right path, not sideways, not backwards. And we don’t want to increase the level of entropy in the universe. :slightly_smiling_face:
I reasonably assumed that if the row numbers are known, and the associative values are also known (in this case, the names of countries), then let machines work with the numbers, and it is easier for people to work with meaningful information.
Procedural, hard-coded solutions are absolutely inflexible, difficult to maintain, and so on. This should be avoided if possible. I would like it to be less. After all, we work in LibreOffice Calc, and a lot has already been done before and for us.
@Bort, we don’t need “pants inside out” (as you once put it). Just don’t scold me here, please.

Alas, I still “insist” (I’m joking, of course) on changing the filtering criteria from row numbers to meaningful values. We are people. And, without seeing a specific example, it is difficult for me to say what needs to be changed in your example. Obviously, I shouldn’t have said that…

On the other hand, my example is focused on the work of the user, not the program. Maybe I guessed wrong. Should I have to guess? But someone makes changes: Private Sub Worksheet_Change
Please formulate the task in such a way that it is also interesting to us (and not “exactly” and in no other way).

@karolus, thank you for appreciating the example.

You’re free to have your opinion of course. I don’t want your opinion and especially don’t want anyone’s ridiculous rants in my questions as a discussion on a Q&A site. I’m not your mom and this site isn’t your shrink. Get some help, call a hotline.

Ignored forever. Toxic. :radioactive: Farewell.

I enjoy this case of someone having absolutely odd ideas about who they are talking to. Being on a user-to-user Q&A site, with everyone here most likely being just another (possibly experienced) fellow user, who may help, or may have other ideas that usually meets warm thanks from askers, here we have someone who comes with requirements to those who may dare to answer. Hey you miserable regulars here! Don’t even think to suggest your solutions, until you read and comprehend all the list of requirements that this asker has imposed to us. I like this attitude - let’s put it as #1 to our “how to use this site”!

@mikekaganski I asked a specific, detailed, clear, question for a VBA code to be made work for Libre Calc. I tried other suggestions. They didn’t work or were difficult to get to work or wouldn’t work because they changed a bunch of things that cannot change in the project file. Why is that so difficult for you to understand?

It’s quite showing how disgusting this community really is when a developer/moderator who should be a role model to the community is immaturely taking whacks at a user that asked a question like a kid in grade school. FFS grow the hell up!

@Bort You are the one here who shows a strange immature behavior!

@karolus So when a person asks a specific, detailed, clear question of what they want and they’re given things that don’t answer the question and aren’t a solution because they don’t work, rants insulting the software that don’t even attempt answering the question, insults on their character (that continues, now by multiple users), and politely says it doesn’t work for them that’s immature behavior but the ranting and carrying on and insults are completely okay.


What planet are you from???

This carrying on with this, I got a solution that works several hours ago, is utter and complete nonsense! And I’m immature??? LMFAO!!!

Thank god for an ignore feature!

I’m not a native speaker, but I’m not sure that the word “disgusting” should be used so often in written speech. Correct me if I’m wrong.
Note: LMFAO means “Хохочу до упаду, черт возьми”.

And now its beyond your capabilities to do anything on yourself in this trivial task?? :sob:

I wrote this Python-code and it (hopyfully) does exactly what you want if you bind it to sheet-event Content Changed

def change_on_country(event):
    if event.AbsoluteName.endswith("$C$5"):
        sheet = event.Spreadsheet
        get_range = sheet.getCellRangeByName
        country = event.String
        india,canada = map(get_range, ("A7:A18","A19:A25"))
        
        if country in ('India','Canada'):            
            india.Rows.IsVisible, canada.Rows.IsVisible = country=="India",  country=="Canada"
        else:
            india.Rows.IsVisible, canada.Rows.IsVisible = True, True

Thanks! Do I have to do anything special with the macro libraries or anything to use phython code?
And how would I add more ranges and key words? There are 12 selections in the list box that would have to hide random ranges of rows, it’s not just 2 ranges from 2 selections.


For example if 1 is selected rows 200:225 have to be hidden.
if 2 is selected I’d like it to hide rows 4:18, 34:97, and 104:169.
I may be able to work with python easier than calc basic.
I’ll play around and see if I can figure that out. Thanks!!

may I recommend install APSO.oxt from here and use it for Management of Python-scripts for Libreoffice.

Additionally … you need to bind the function above to SheetEvent Content changed
(→Right-click on SheetTab→Sheetevents… )

I’ve been out of the practice of coding for over a decade. I’m not good with it anymore and I don’t have a clue how to use those tools. I’ll give it a try but I’m not confident I can get any of it to work. I have no idea how to read a debugger. No clue how to even install it.


Testing your python script I used the code you wrote to replace a recorded macro. It’s throwing a syntax error asking for “then” constantly. I can’t do anything in the document or the code because of the error.


Nope! I’m not able to work with python scrips. Saw the wiki here and that’s way too much digging and editing etc just for one macro. I also don’t have a python editor and have no clue how to write python code at all. If an editor is needed other than a text editor that’s another thing I don’t have.

Thank you very much! I’m not able to use that though.

python is not Basic, why do you expect it works by magic within BasicIDE executed by Basic-Interpreter??

LOL I don’t expect it to work like magic. I gave it a shot the way I know how.
Python’s not for me. I’ve just barely started getting a slight but still baffled beyond confused understanding of Basic.

I had already suggested you to install APSO. It does most of the stuff behind the scene, but in any case you need to write code yourself (either in Basic or Python) or to find some guy who write it for you.

Coding takes forever to learn and even longer to get good at. I’m old. I just want to get this sheet working.

Sub hideRows
	dim oDoc as object, oSheet as object, oCell as object, p(), s$, oRange as object, oCur as object, i&, j&, a1(), a2()
	
	rem your data
	const cCell="D1" 'name of the decision cell
	p=array( 	_
					array("Bohemia", "3:7"), _
					array("Canada", "22:25", "30:38", "48:52"), _
					array("India", "17:21", "60:61") _
					)


	oDoc=ThisComponent
	
		rem ********** if you have many formulas in your sheet, then this can be faster
		dim bAutomatic as boolean
		bAutomatic=oDoc.isAutomaticCalculationEnabled 'value of the: Data/ Calculate/ AutoCalculate
		if bAutomatic then oDoc.enableAutomaticCalculation(false) 'deactivate AutoCalculate
		oDoc.lockControllers() 'no screen actualization
		oDoc.addActionLock() 'lock
					
	oSheet=oDoc.CurrentController.ActiveSheet 'active sheet
	oCell=oSheet.getCellRangeByName(cCell) 'decision cell
	s=oCell.string 'string from decision cell
	
	rem view all rows
	oCur=oSheet.createCursor
	oCur.goToEnd(false)
	oRange=oSheet.getCellRangeByPosition(0, 0, 0, oCur.RangeAddress.EndRow) 'used rows in column A
	oRange.Rows.isVisible=true 'set all rows to visible
	
	rem hide rows
	for i=lbound(p) to ubound(p) 'loop all arrays
		a1=p(i)
		if a1(0)=s then 'array with needy name
			for j=1 to ubound(a1) 'loop all ranges in array
				a2=split(a1(j), ":")
				oRange=oSheet.getCellRangeByPosition(0, CLng(a2(0))-1, 0, CLng(a2(1))-1 ) 'rows in column A
				oRange.Rows.isVisible=false 'hide the rows in actual range
			next j
			exit for 'exit the loop
		end if
	next i

		rem **********
		oDoc.removeActionLock() 'no lock
		oDoc.unlockControllers() 'screen actualization
		oDoc.enableAutomaticCalculation(bAutomatic) 'set AutoCalculate to the original value
End Sub

Example
hideRows.ods (13.8 kB)

Perfect! Words can’t express Thank you enough!