PDA

View Full Version : Put code WS modules not a General module??



omnibuster
02-25-2009, 03:07 PM
Why this code putten in General Module rather than to put in WorkSheet Change By... module?
:dunno
Sub AddMToSht1()
Dim VBP As VBProject
Dim VBM As VBComponent
Dim VBModule As CodeModule
Dim VBProc As VBComponent
Set VBP = ThisWorkbook.VBProject
Set VBModule = VBP.VBComponents.Item("Sheet1").CodeModule
VBModule.AddFromString ("Private Sub Worksheet_BeforeRichtClick(ByVal Target As Range, Cancel As Boolean)'" _
& vbCrLf & "Selection.Copy" & vbCrLf & "Sheets(""Sheet2"").Activate" _
& vbCrLf & "ActiveSheet.Range(""B3"").PasteSpecial Paste:=xlPasteValues" _
& vbCrLf & "'TAA2'" _
& vbCrLf & "End Sub")
End Sub

Bob Phillips
02-25-2009, 03:09 PM
Because you can?

omnibuster
02-25-2009, 03:17 PM
How?

Bob Phillips
02-25-2009, 03:54 PM
How what, you already have?

It may be a good idea to telkl us what you are on about, I can play cryptic messages all day long.

mikerickson
02-25-2009, 07:14 PM
Why is it in a normal module?
That code writes a Sheet_BeforeRightClick routine to Sheet1's code module.
A module can't modify itself, thus the normal module.

A further question would be, why have this code at all?

If the BeforeRightClick code is to be deleted later in the process, why would this routine put a comment in the RightClick code?
If the BRC code is not to be deleted later, why not just put the BeforeRightClick in the sheet module at design time?

Plus, if this routine is run twice, the worksheet will have two BeforeRightClick event codes.

Do you know the problem that this routine is designed to solve?

omnibuster
02-26-2009, 01:21 PM
Thanks mikericson, xld for response. (sry my bad english)

Sry.I excplaned not enough.
I put here sample : http://web.zone.ee/omnibuster/
File Head is major & should be rename, reffresh, download..etc..etc file Sample.
If in File Sample Sheet1 is completed (all columns is full) code should Add new Sheet-rename this to Sht1 and put in the Sht1-WorkSheet Before...code.(RightClick & DoubleClick "event".) Once at all.
Next time if Sheet1 full, this Sheet should be renamed Sheet2 & this code should be delete.

If I make New File.xls & put this code the New File& run -the code works , only DoubleClick event dont work, because she putted in the WS General Module.???

With File Head & this code Excel crashed???
Gived error: Sample(Version1)[Recovered]
MS OfficeExcel has encountered a problem.......Crash.