Log in

View Full Version : Solved: Macro that cheats!



DeadKing
10-27-2011, 07:03 AM
Hi.

I know that computer program can not cheat, but my macro does! :banghead:

I have lets' say 10000 lines of text. I wrote some macro which (among others things) delete some parts of this text as they are useless for me. The macro seems to work almost perfectly. Almost.
Among those 10000 lines are parts (6-8 lines) I want to delete. In the middle of this part is static text (same word in every part in whole text).
So, I Selection.Find.Execute this static text, move cursor 3 lines up (static position), check if I can delete and delete line by line this part of text. When I find text which is not to be deleted I Exit this loop and move to next Selection.Find.Execute

Now how I know my macro cheats - in random parts of text (in test it happens 2-5 times per macro run) the static text is detected, everything under this static text is deleted exactly as it should be. But the static text and those 3 lines befor it remains in place.
BUT - when I try to debug this part of code, going step by setp, line by line trough my code, the macro deletes everything as it should. Therefore I'm not able to find out what cause the problem.

An interesting thing about this "cheat" is that macro usualy skips same pieces of text. Lets say I want to delete lines 5,6,7,12,13,14,21,22,23,30,31 and 32, there is good chance I end up with deleted 5,6,7,14,21,22,23,30,31 and 32. (12 and 13 skipped) But of course, if I try step by step debug, it will delete 5,6,7,12,13,14,21,22,23,30,31 and 32 as it should.

Another thing I've noticed (and I'm not sure if it have anything to do with this problem) is that after some listing (Page Up/Down), my cursor disappear and appear again after writing a thing or after some time. (if it disapper on line 3451, I can write and delete a character or just list a bit more and on 3524 it appears again (example)).

Have anyone of you ever experienced this? Can anyone help me out?

Frosty
10-27-2011, 10:52 AM
Can you post some code? Using the Selection object is often the least efficient way to program something. I have a couple of guesses (which is the best I can give when you're simply describing something).

1. Try going backwards instead of forwards. If you are relying on things like line numbers, deleting as you go might cause an issue when you're going forwards (if you aren't re-articulating your entire index). That's programmatic-speak for the following concept: if you're deleting stuff, and you are occasionally getting failures, then you should try going a different "direction".... it may pay dividends.

2. Try something other than the selection object. Much like #1, relying on the actual selection could be an issue. Although it's highly unlikely you're encountering a bug in the actual Selection.Find object.

macropod
10-31-2011, 03:51 AM
Hi DeadKing,

From what you've described, you may not even need a macro - a wildcard Find/Replace might do the job just as well. If, for example, the 'lines' you refer to a paragraphs, this is even more likely to be the case. So, if you post some representative examples, a different solution might be forthcoming.

DeadKing
11-01-2011, 06:00 AM
Frosty: You have good point with the Selection object, but I'm pretty new in VBA. I'm trying to learn it a bit by writing macros like this one (which is to be usefull, not just plain "try and delete example"). There is good chance I'm using unnecessary or even inappropriate commands and just not see an easier and dependable way.

The problem why I didn't post some pieces of the code before is that with it I should post an "exemplar" file on which the code should be used on. But this file contains some sensitive datas and with it's size I was trying to avoid a lot of editing to change those datas to something harmless.

I'll definitly try the backward way and see if it changes anything.
AD wildcards - I want the macro to do more things than just this deleting. And since it will be used by a third person, it should work as automaticly as possible.

Thank you both for tips. I'll try to look into it a bit more and post my findings here.

Frosty
11-01-2011, 09:22 AM
DeadKing,

Don't discount the possibility of a wild-card search. It's a lot more powerful than you think, especially when used in conjunction with programming (i.e., you wouldn't need to teach an end-user all the possibilities of a wild-card search in order to use that methodology).

And yes, never post sensitive data. But if you post sample text (and it never needs to be that long... if you can do it twice with programming, you can do it a 1000 times), then you can see a couple of different programming methodologies which might help push you more in the right direction.

If you were able to use a wildcard search and a replace all, you would amazing by how fast and simple the code is. In addition, Paul is a master of wild card searches... so I wouldn't walk away from that quite so quickly ;)

DeadKing
11-02-2011, 08:45 AM
You misunderstood me. I'm not saying that wildcards are useless. I'm saying that I see no use for them in my case since I have no problem with finding the text and I'm never sure how much lines I have to remove.

AD posting a piece of sample text - yes, program that works for two things usualy works for 1000 of them. But not in my case, which is why I was asking here in the first place.

Anyway - the backward way you recomanded is working flawlessly, thank you once again.

Please consider this thread as solved.

Frosty
11-02-2011, 05:15 PM
Glad the backwards way of doing it worked. Just a quick followup... sophisticated wildcard searches can replace programming. Just because you don't know the number of lines to delete, unless you're asking your user mid-process how many lines to delete, there may very well be a way of using a wildcard search to delete the variable amount of lines.

i.e., if you programmed the logic on variable amount of line deletion... there may very well be a way of doing the exact same thing with an actual wildcard search. It's a lot more powerful than its typical basic usage, although the syntax can be a bear.

And, if you've already got it working... then you don't need to investigate that anyway :)