Computing.Net > Forums > Office Software > user form in excel

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

user form in excel

Reply to Message Icon

Name: mherber2
Date: October 10, 2007 at 09:46:42 Pacific
OS: xp
CPU/Ram: unknow
Product: unknown
Comment:

i created a form using vba in excel, what i want to do is if the cell that is clicked on is blank i would like excel to open the form...please help



Sponsored Link
Ads by Google

Response Number 1
Name: DerbyDad03
Date: October 10, 2007 at 12:02:20 Pacific
Reply:

Assuming the UserForm is in the same workbook, try this:

Right click the sheet tab for a sheet, select View code and paste this code into the VBA editor. Change the name of UserForm1 to match the name of your UserForm.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target = "" Then UserForm1.Show
End Sub


0

Response Number 2
Name: mherber2
Date: October 10, 2007 at 14:05:43 Pacific
Reply:

That worked but is there a way to make it so that is comes up with a double click instead of a single click


0

Response Number 3
Name: Razor2.3
Date: October 10, 2007 at 17:24:42 Pacific
Reply:

Change SelectionChange to BeforeDoubleClick


0

Response Number 4
Name: DerbyDad03
Date: October 11, 2007 at 06:29:24 Pacific
Reply:

Just to clarify Razor2.3's excellent suggestion, use the BeforeDoubleClick event instead of the SelectionChange event.

IOW, don't just replace the string SelectionChange with BeforeDoubleClick. I don't believe that will work.

Instead, replace the entire first line with this:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Or choose the BeforeDoubleClick event from the dropdown and insert the If Target = "" Then UserForm1.Show line into the procedure.


0

Response Number 5
Name: mherber2
Date: October 11, 2007 at 07:24:19 Pacific
Reply:

Thank you

i also want the cell to lock if the cell has any value


0

Related Posts

See More



Response Number 6
Name: DerbyDad03
Date: October 11, 2007 at 08:06:29 Pacific
Reply:

A friendly suggestion...

It's helpful to those of us trying to come up with solutions if you include all of your requirements in a single post as opposed to adding things after solutions have been posted. In some cases, the additional requirements can make earlier suggestions unusable, thereby being a waste of everyone's time.

I know that it is not always possible to know what new situations might arise based on a given solution, but the more we know up front, the easier it is to come with an all-inclusive solution.

Is there anything else you want to add to your requirements before we try to answer this latest question?


0

Response Number 7
Name: mherber2
Date: October 11, 2007 at 08:17:37 Pacific
Reply:

ok i also want to date stamp the cell after information is put in and have the clear 14 days after the date of the stamp...and thats it


0

Response Number 8
Name: DerbyDad03
Date: October 11, 2007 at 08:29:29 Pacific
Reply:

Let me see if I understand this:

If a user double clicks a cell and it is empty, a userform should pop up. I assume this userform will populate the cell with some data.

What should happen if the user single clicks or right clicks the cell?

Once a cell contains data, it should be locked for 14 days and then unlocked, cleared and available for use.

Did I miss anything?


0

Response Number 9
Name: mherber2
Date: October 11, 2007 at 08:46:00 Pacific
Reply:

thats it...when the user single clicks or right clicks it should do nothing...and i dont think this is possible but is there a way to bypass the enable macros question


0

Response Number 10
Name: mherber2
Date: October 11, 2007 at 09:26:58 Pacific
Reply:

I did think of one more thing...it would be amazing if i could remove the tab of the worksheet...is that at all possible?


0

Response Number 11
Name: DerbyDad03
Date: October 11, 2007 at 09:30:44 Pacific
Reply:

There is no simple way to "bypass" the enable macros question, although vulnerabilities do exist. See
http://www.microsoft.com/technet/se...
and
http://www.microsoft.com/technet/se...

If there was an easy way, then anyone could attach code to a worksheet and do all sorts of bad things intentionally or by mistake.

There is, however a fairly common way to deal with the question. You set up a workbook where only an instruction sheet is visible if macros are disabled. This sheet tells users to enable macros if they want to use the workbook.

See this site for one such method:

http://www.dotxls.com/excel-securit...


0

Response Number 12
Name: mherber2
Date: October 11, 2007 at 09:32:55 Pacific
Reply:

Ok what about everything esle?


0

Response Number 13
Name: DerbyDad03
Date: October 11, 2007 at 10:06:56 Pacific
Reply:

I figured I'd give it a day so you could add a few more requirements.

In response 7 you said "and thats it".

In 9 you added "bypassing the macros question".

In 10 you added "removing the sheet tab".

I'm sure there's more...


0

Response Number 14
Name: mherber2
Date: October 11, 2007 at 10:08:39 Pacific
Reply:

Lol i am 95% sure thats all...i cant think of anything else i want it to do...


0

Response Number 15
Name: mherber2
Date: October 11, 2007 at 10:49:30 Pacific
Reply:

yep thats all


0

Response Number 16
Name: DerbyDad03
Date: October 11, 2007 at 14:05:20 Pacific
Reply:

Here are some pieces and suggestions.

I'm going to have to leave it up to you to put them all together since only you know your workbook/code is laid out and how you want it all to interact.

I'll supply the tools, you put them to use...

To turn off Sheet Tabs:

ActiveWindow.DisplayWorkbookTabs = False

To turn off Right Click:

Application.CommandBars("column").Enabled = False
Application.CommandBars("cell").Enabled = False
Application.CommandBars("row").Enabled = False

The follow sets of code have to be used together to get the desired results. All of the testing I've done was with text, not formulae, so you might have to modify the code to make it work for every situation.

Copy/Paste all the code below into a sheet module. Note: The ResetEvents code at the bottom is just a little sub to use in case your testing crashes some code after Events have been disabled. In order to get the Events enabled again, just run that little sub.

The only way I was able to "have nothing happen when a cell was single clicked" and "lock cells that already contain data" was to save the contents of a cell when it is selected and put it back in the cell if it gets changed. There may be a way to accomplish the same thing with sheet protections and cell locking, but I couldn't get it to work.

Public OldAdd, OldVal As String

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'If a single cell empty, show the UserForm
If Target = "" Then UserForm1.Show
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'If a single cell is selected save current address and contents
If Selection.Cells.Count = 1 Then
OldVal = Target
OldAdd = Target.Address
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
'If a change is made, other than by the UserForm, replace the old contents
On Error Resume Next
Application.EnableEvents = False
Range(OldAdd) = OldVal
Application.EnableEvents = True
End Sub

Sub ResetEvents()
Application.EnableEvents = True
End Sub



0

Response Number 17
Name: mherber2
Date: October 11, 2007 at 14:26:06 Pacific
Reply:

I was really hoping for something with the protection and locks...any advice on the clearing of the cell after 14 days


0

Response Number 18
Name: DerbyDad03
Date: October 11, 2007 at 14:55:39 Pacific
Reply:

The only advice I have is to set up your UserForm macros to record the date and location of each entry on a separate sheet or in a comment tied to the cell. Then have a macro that periodically checks the current date against the recorded date for each entry. Once an entry_date + 14 equals todays_date, clear the cell. Could be easily done with On Open macro or a SheetActivate macro.

BTW...feel free to chuck in a "Thanks for your help" every now and then. I posted a rather lengthy list of suggestions and all I got was "I was hoping for something else" and "What about my other question".


0

Response Number 19
Name: Razor2.3
Date: October 11, 2007 at 17:03:00 Pacific
Reply:

Locking the data is surprisingly simple. As an added bonus, the data should remain protected if macros aren't enabled, assuming you pick a good password: (Note: this does not disable the context menus, nor does it hide the sheet tabs)

Location: ThisWorkbook
Option Explicit

Const strPasswd As String = "Some Password"

Private Sub Workbook_Open()
If Not ActiveWorkbook Is ThisWorkbook Then _
Exit Sub

Dim sheet As Worksheet
Dim cell As Range

On Error Resume Next
For Each sheet In Me.Worksheets
UnlockSheet sheet
For Each cell In sheet.Range(sheet.Cells(1), sheet.Cells.SpecialCells(xlCellTypeLastCell))
If cell.Comment Is Nothing Then
If Not cell.Formula = "" Then _
cell.Clear
ElseIf CLng(CDate(cell.Comment.Text)) <= CLng(Now - 14) Then
cell.Clear
ElseIf Err.Number Then
cell.Clear
Err.Clear
End If
Next
LockSheet sheet
Next
Me.Protect strPasswd, True, True
End Sub


Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
If Target = "" Then
Me.Unprotect strPasswd
UnlockSheet Sh
UserForm1.Show
Target.AddComment CStr(Now)
LockSheet Sh
Cancel = True
End If
End Sub

Public Sub UnlockSheet(sheet As Worksheet)
sheet.Unprotect strPasswd
End Sub

Public Sub LockSheet(sheet As Worksheet)
sheet.Protect strPasswd, True, True, True
End Sub

Public Sub helpMe()
Dim sheet As Worksheet
For Each sheet In Me.Sheets
sheet.Unprotect strPasswd
Next
Me.Unprotect strPasswd
Application.Update = True
End Sub

Man, that's far too big for a "surprisingly simple" solution.

EDIT: Hey, I think this is longer than DerbyDad03's post! Do I win something?


0

Response Number 20
Name: DerbyDad03
Date: October 11, 2007 at 17:47:08 Pacific
Reply:

Nice Job!

Now let's see if you win a Thank You from the OP.


0

Response Number 21
Name: mherber2
Date: October 12, 2007 at 06:41:35 Pacific
Reply:

ok this might help some one else...this is what i have been able to do with locks and protection to lock a cell after data is put in

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, CANCEL As Boolean)
If Target = "" Then bulletin.Show
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target = (("*")) _
Then Selection.Locked = True
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=False
End Sub


0

Response Number 22
Name: mherber2
Date: October 12, 2007 at 06:52:56 Pacific
Reply:

ok now the last thing i need to do is delete a cell 14 days after it is filled, it auto locks after there is data in it so the cell wont change at all


0

Response Number 23
Name: DerbyDad03
Date: October 12, 2007 at 06:59:14 Pacific
Reply:

Have you tried Razor2.3's code? It appears to do *everything* you've asked for, including locking the cells and clearing them after 14 days.

Oh I see - you've been too busy thanking everyone for their help to test his code.


0

Response Number 24
Name: mherber2
Date: October 12, 2007 at 08:15:01 Pacific
Reply:

It didnt work for me...i tried...thank you though....all i need now though is the code to clear a cell after 14 days


0

Response Number 25
Name: mherber2
Date: October 13, 2007 at 05:16:01 Pacific
Reply:

anybody know a way?


0

Response Number 26
Name: Razor2.3
Date: October 13, 2007 at 12:30:10 Pacific
Reply:

Well, everyone here (excluding you) has already agreed the code provided works, so you have a choice of explaining what's happening, pay someone a lot of money to write your VBA for you, or use a database application.

And even if you do explain, you've provided no incentive for us to continue to care.


0

Response Number 27
Name: mherber2
Date: October 13, 2007 at 12:50:02 Pacific
Reply:

ok sorry for being a pain in the neck


0

Response Number 28
Name: mherber2
Date: October 13, 2007 at 14:25:46 Pacific
Reply:

the problem i am having with that code is that it is deleting every thing in the sheet regardless of when the data was entered


0

Response Number 29
Name: Razor2.3
Date: October 14, 2007 at 15:38:05 Pacific
Reply:

That's by design.

When you double-click, it adds a comment to the cell that contains the date data was added. When loading the workbook, we scan each cell, looking for data. If we find something in the cell, but not the date comment, we delete the errant data.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Office Software Forum Home


Sponsored links

Ads by Google


Results for: user form in excel

how to make adding form in excel. www.computing.net/answers/office/how-to-make-adding-form-in-excel/334.html

Drop Down on User Form www.computing.net/answers/office/drop-down-on-user-form/9481.html

Print button in excel www.computing.net/answers/office/print-button-in-excel/8213.html