Need a macros to delete rows with some different sn

I want to delete some rows with about 1000 different sn like: 1-20220901, 15-20220901, 20-20220920.
Please give a macro then I can run it, thank you so much!

I just started to learn the macros, does this allowed for multiple condition:
if cell in col A = 1-20220901, 15-20220901, 20-20220920

Hallo
IHMO instead »learn the macros«… you should just start how to filter, search, regular expressions ectpp.

1 Like

How often are you going to repeat this operation? Maybe it’s not worth writing this macro, perhaps the already existing toolkit will be enough?

DeleteSN

1 Like

Thanks, I have thousands of sn to select, so using filter can hurt my finger, :slight_smile:

Thanks, I have thousands of sn to select,so I saw some code like this, but not sure, if I can copy and paste my thousands sn in it:


sub DeleteRows()

dim oSheet as object
dim oCellCursor as object, oCellRangeAddress as object, oCell as object
dim lRowNumber as long, lEndRow as long

oSheet = thiscomponent.currentController.activesheet
oCellCursor = oSheet.createCursor()

' Find the last used row '

oCellCursor.gotoEndOfUsedArea(True)
oCellRangeAddress = oCellCursor.getRangeAddress()
lEndRow = oCellRangeAddress.EndRow

'from the bottom to the top, one row at a time '

for lRowNumber  = lEndRow to 1 step -1
'	if cell in col D = 5 '
	oCell = oSheet.getCellByPosition(3, lRowNumber)
	if oCell.value = 5 then

'	if cell in col H = empty '    	
		oCell = oSheet.getCellByPosition(7, lRowNumber)
		if oCell.Type = com.sun.star.table.CellContentType.EMPTY then
			oSheet.rows.removeByIndex(lRowNumber, 1)
		end if
	end if	
next lRowNumber  

print "done"

end sub
1 Like

Are you saying that if you write all these numbers into your macro code from the keyboard, then your fingers will be safe? :wink:

:grinning:
I just copy and paste them,

That’s exactly what I’m trying to ask! Do you have TWO lists? Cleanup list and exclusion list? Tell us more about this and it will be useful to attach a file with sample data to the message.

You might be interested in reading this discussion

1 Like

thanks, checked.
The code I post above is working, I am testing it to make sure 100%

Sorry, I didn’t understand this sentence. Are you talking about the DeleteRows subroutine? And how are you going to stick the 1-20220901, 15-20220901, 20-20220920 values into the line if oCell.value = 5 then?

thanks, it is only works with one value for now, I am looking for a solution to make a one go for all the value together

@alan20alan Please watch your fingers! (By the way, putting all these values in the macro code is not the right way. Will you show us a spreadsheet with sample data?)

1 Like

It might pay to base a solution on the fact that the contents of the “sn” column are made up from a natural number followed by a dash and by something most likely being a textual representation of a date in ISO 8601 “not extended” standard.
Filtering by dates (partly at least) should rarely require to type a lot.
Reconsider your situation. You may find a RegEx actually selecting what you need. Even better may be a solution based on numerical eqautions/inequations. From 15-20220901 you can easily get by formulas in helper columns the number 15 and the ordinary numeric spreadsheet-representation of the date 2022-09-01.

Thanks, I actually want to delete 1000s rows which are in the sn list, it is not typing date,

I am not sophisticated so if I have an exclusion list then I’ll add a column to my data sheet and enter something like =VLOOKUP(A2;Exclusion.$A$1:A3;1;0), AutoFilter out all the #N/A and delete the remaining rows as in attached spreadsheet. Note that the TRIM() function is sometimes needed to remove leading and trailing spaces in one or both lists.
SelectDataOnExclusionList.ods (31.3 KB)