Allow user to make entries on worksheet during run-time

两盒软妹~` 提交于 2020-01-25 01:08:16

问题


All:

Thank you in advance, you all have been a tremendous resource!!!

I have a couple of spreadsheets where the sheet is protected, but users can still use filters. I'm processing most of the sheets automatically, but what I need to do, is present the user with the sheets that need to be filtered, then have them select a "Finish" type button or toolbar entry, which I already have.

What I need to be able to do, is to bring this sheet up, pause the macro, if possible, while they make their changes (could be up to 5 filters that they select before the sheet is ready.

Then, copy the visible cells only to a specific sheet and then resume the macro.

I don't think that Worksheet change event will do this.

I'm thinking more on the lines of maybe setting a flag on a spare sheet, firing up the next macro and then see if it can find the original macro and pick up where it is flagged?

I thought about a modeless userform that the user could click OK on and then call the next macro, but that does not work.

The calling code is:

UserForm3.Show
CopyToDisplay "AEP"
LastPos = LastPos + 1

Where AEP is the sheet name to copy the filtered rows from.

Userform displays, but clicking ok does nothing and of course, the macro keeps on going.

Any suggestions would be greatly appreciated!

Thanks,

Jeff


回答1:


Jeff let's try this. Your current code:

UserForm3.Show
CopyToDisplay "AEP"
LastPos = LastPos + 1

When we display a UserForm, the default behavior is vbModal, which essentially freezes the application and the user cannot interact with anything but the UserForm, that is not what you want. What you need is a way to display the form, and then just wait for the user to signal that s/he is finished with the input.

So we need to modify a few things:

The UserForm needs to effectively "pause" while also allowing the user to interact with the worksheet. A vbModal form can't do this (it pauses, without interaction), and really neither can a vbModeless (it continues execution AND allows interaction).

Conundrum? No. we can simulate a pause with the vbModeless form, and preserve the user's ability to interact with the sheet. The best of both worlds!!

We will show the form with the optional vbModeless, this allows the user to interact with the rest of the Application /worksheets/etc. Since a modeless form continues code execution, we need to simulate a pause and we can do this with a Loop. The loop will run indefinitely, and only break once the UserForm is closed, at which point the rest of the code will continue to execute.

UserForm3.Show vbModeless 

Do While UserForm3.Visible
    DoEvents
Loop

LastPos = LastPos + 1

'You MAY need to reset some variables, if the Filter/Autofilter has affected these/etc.

Design-wise, give your form a single Label control and set its .Caption property (and the form's .Caption property) in some useful/instructive way. You could add a command button but that seems unnecessary, since all the button would do is invoke the Terminate event (which can always be done with the red "X")

For your issue with copying (apparent failure to paste), try changing this:

Sheets("AEP").Select  

With ActiveSheet      
    .UsedRange.SpecialCells(xlCellTypeVisible).Copy _
        Destination:=Sheets("Display").range("A" & LastPos)     
    .AutoFilterMode = False     
    Application.CutCopyMode = False  
End With

To this:

With ActiveSheet      
    .UsedRange.SpecialCells(xlCellTypeVisible).Copy 
     Sheets("Display").range("A" & LastPos).PasteSpecial
    .AutoFilterMode = False     
    Application.CutCopyMode = False  
End With


来源:https://stackoverflow.com/questions/26002837/allow-user-to-make-entries-on-worksheet-during-run-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!