Answered and collected by Igor Kolupaev
last Updated:
01.04.98 9:06:43
Q: | I'd like to customize the menu which is displayed when depressing the right button of the mouse, How does one access this menu from VBA? (i.e. what is the name of the menu ?) |
A: |
You can acsess to menus toolbars through commandBars collection. RightClickMenu
name--"cell". Something like follows code. Also you can get more if type "BeforeRightClick" anywhere in vba-editor, press F1 and click on example.
Dim cbButt As CommandBarButton Dim cb As CommandBar Set cb = Application.CommandBars("cell") Set cbButt = cb.Controls.Add(msoControlButton) cbButt.Caption = "Your Item" cbButt.OnAction = "YourMacro"Thanks to: |
Go Top | |
Q: | it is possible to call the database oriented functions (i.e. DGET, DSUM, ...). |
A: |
After some plaing with dsum i get worked follows code:
Dim a a = Application.DSum(Range("a1:c10"), "b", Range("F1:H2"))Thanks to: |
Go Top | |
Q: | I would like to know how is it possible to disable the exit button on the top right side of a dialog box (X button). |
A: |
in Excel 97 you can use the QueryClose event Thanks to: Jennifer A. Campion |
Go Top | |
Q: | I'm sometimes using the RND generator function in an Excel program, but it bugs me how poor it is. |
A: |
1. Have you used the Randomize statement? Here is a small copy from the Help on RND. Check out Help for other options. Hope this helps.
2. I also prefer to use the random power of a random number
(Rnd ^ Rnd) to ensure that the chances of reoccurance is nil. Thanks to: Dana De, Pbucciol |
Go Top | |
Q: | How can i format cells to display numbers with lead zeros like 001, 002 |
A: |
You just need to setup cells format as "00" or "0000" (what leader zeros you want) by [Ctrl+1] menu or format | cells menu. Thanks to: |
Go Top | |
Q: | How to determinate numbers of rows and columns that contains any data in the worksheet |
A: |
rows number -- Worksheet.UsedRange.Rows.Count
columns number -- Worksheet.UsedRange.Columns.Count
UsedRange returns a Range object that represents the used range on the specified worksheet. Read-only.
Notes by Bob Umlas: You specify that to find the # of rows/columns which contain data you should use UsedRange. This is not correct. First of all, if you start a blank sheet & enter =a300 in cell A1, then enter "X" in A1, the UsedRange says there are 300 rows. That's not true...it's 1. The better way is to use Range("A1").Find("*",SearchDirection:=xlprevious,SearchOrder:=xlbyrows).row to get the "real" last row containing data, then again by xlbycolumns to get the "real" last column containing data. Thanks to: Nick Hodge, Laurent Longre, Bob Umlas. |
Go Top | |
Q: | How to print selection range? |
A: |
i think your wonder how it's easy:
Sub PrintSelection Selection.PrintOut End SubThanks to: John Green |
Go Top | |
Q: | How to get substring from 8th through 4th characters from the right end of a string? |
A: |
Hope this helps
=MID(A1,LEN(A1)-7,4)Thanks to: Jonathan Dieterman |
Go Top | |
Q: | How can I display a dialog box in Excel 97 containing only the directories tree so that the user can select a directory? |
A: |
See the Excel tip at:
http://www.j-walk.com/ss/excel/tips/tip29.htm Thanks to: John Walkenbach |
Go Top | |
Q: | How to determining whether a cell contains a function |
A: |
Below functions can be used as worksheet function
Function IsFormula(c) IsFormula = c.HasFormula End FunctionThanks to: Ziff-Davis Tips |
Go Top | |
Q: | How to reduce size of .xls file? |
A: |
There is an Excel Code Cleaner that was developed by Rob Bovey of
Baarns Consulting. It's a free downloadable. Here is the URL:
http://www.baarns.com/DevOnly/. In addition, we have a free download
that cleans styles used in a workbook. This URL is:
http://www.payneconsulting.com/freestuff.htm. --- Not sure what "code" you're removing. The biggest factor I've found in increasing the size of an Excel file is formatting in areas of the sheet you aren't even using. If you format entire columns or rows, there is extra code in the file to cover formatting of the empty cells all the way to the bottom or right. If you have this, try unformatting the entire column, then formatting only the cell range actually used and deleting all the empty columns and rows. Then save. The file can shrink dramatically. --- Microsoft has the following article you may find interesting: XL: Workbook File Size Increases While Editing Procedure Last reviewed: September 2, 1997 Article ID: Q123684 5.00 5.00c 7.00 7.00a 97 | 5.00 5.00a Thanks to: Donna, Don and Dana |
Go Top | |
Q: | How can i save chart to any graphics file? |
A: |
Here's just checked working example which export first chart in first worksheet
to c:\shart.gif file
Sub export() Worksheets(1).ChartObjects(1).Chart.export "c:\chart.gif", "GIF" End SubThanks to: |
Go Top | |
Q: | How can I dismiss a Userform when the user terminates data entry in a text box with a carriage return? |
A: |
set the default property of your OK button to true.. Thanks to: Mike Chidsey |
Go Top | |
Q: | You wished to create Add-ins using Excel 8/97 |
A: |
Easy...
File - Save As - Save As Type: MS Excel Add-In (last in the list) Add-ins in previous versions of Excel were always protected and there was no way (kind of) to see the code of an add-in. In Excel 8, this is different. The modules in the add-in are fully visible in the Visual Basic Editor, unless you tell Excel that you want them protected before you save your add-in. Thanks to: Ture Magnusson |
Go Top | |
Q: | So how do you protect the add-in? |
A: |
Easy...
While in VBE, right-click on your VBAproject in the Project window, select VBAProject properties, click on the Protection tab, chech the "Lock project for viewing" checkbox and enter passwords. Thanks to: Ture Magnusson |
Go Top | |
Q: | You also wanted to create a custom menu |
A: |
Easy...
Right-click on a menu or toolbar and select "Customize". Menus and toolbars are both handled in the same way in Excel 8, there is no longer a Menu Editor, because there is no need for one. While in "Customize mode" you can click on a menu to see its menu items, and right-click on a menu item to see or change its properties. Menu items and toolbar buttons are drag-and-droppable, and if you can easily copy by pressing Ctrl while dragging and dropping Thanks to: Ture Magnusson |
Go Top | |
Q: | How to loop through a range of cells and do something. |
A: |
This should work from version 5 and forward. It loops through all selected areas and all cells in each area and clears the cell if it isn't locked.
Sub due() Dim a, c For Each a In Selection For Each c In a If Not c.Locked Then c.ClearContents Next c Next a End SubThanks to: Ture Magnusson |
Go Top | |
Q: | Is there a method to make accelerator keys run macros from a regular worksheet |
A: |
Once you create your macro, choose Tools | Macro | Macros, then select your macro, then choose Options. From there you can specify a shortcut key for your macro. Thanks to: David Ringstrom |
Go Top | |
Q: | How to get the row number and column number for the active cell? |
A: |
ActiveCell.Row – row number;
ActiveCell.Column – column number;
ActiveCell.Address – literal like “B2”. Thanks to: Ture Magnusson |
Go Top | |
Q: | Is it possible (in XL '97) to password protect a VB module? |
A: |
You can protect your VBA project by right mouse clicking on the module or one of the excel objects, select VBAProject Properties, select the protection tab...… Thanks to: Cheers Andrew |
Go Top | |
Q: | How to set the focus on a particular button in a dialog (XL7) by code. |
A: |
The Focus property of the dialogsheet is what you need. Set it to the name of the control you wish to give focus, like this:
Sub GiveAButtonFocus() ActiveDialog.Focus = ActiveDialog.Buttons(3).Name End SubThanks to: Ture Magnusson |
Go Top | |
Q: | How to delete and add buttons to worksheet by codes? |
A: |
see example
Sub kill_button() Worksheets("sheet1").Activate ActiveSheet.DrawingObjects("Button 2").Delete End Sub Sub add_button() Worksheets("sheet1").Activate ActiveSheet.Buttons.Add(81, 109, 82, 23).Select Selection.Name = "Button 2" Selection.Caption = "Print" ' or whatever it says Selection.OnAction = "Print_Table" End SubThanks to: Andrew Weinstein |
Go Top | |
Q: | How to define array constant? |
A: |
No way. But you can use array() to assign whole array like this:
Dim MyWeek, MyDay MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun") MyDay = MyWeek(2) ' MyDay contains "Tue". MyDay = MyWeek(4) ' MyDay contains "Thu".Thanks to: Bernie Deitrick |
Go Top | |
Q: | How to get path to active workbook? |
A: |
ActiveWorkbook.Path -- path without filename
ActiveWorkbook.FullName -- full path to workbook including path and filename. Thanks to: |
Go Top | |
Q: | How let CalendarControl 8.0 to display the current date when the user displays the UserForm? |
A: |
Private Sub UserForm_Initialize() Calendar1.Day = Day(Date) Calendar1.Month = Month(Date) Calendar1.Year = Year(Date) End SubThanks to: |
Go Top | |
Q: | How to get the network name of the current user? |
A: |
Here’s the function which returned network username.
Private Declare Function GetUserName Lib "advapi32.dll" _ Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long Function NTDomainUserName() As String Dim strBuffer As String * 255 Dim lngBufferLength As Long Dim lngRet As Long Dim strTemp As String lngBufferLength = 255 strBuffer = String(255, 0) lngRet = GetUserName(strBuffer, lngBufferLength) strTemp = UCase(Left(strBuffer, lngBufferLength - 1)) NTDomainUserName = strTemp End FunctionThanks to: |
Go Top | |
Q: | How to find out if a certain file is open, and in case it is not - open it? |
A: |
Follow sub check if 1.xls is opened and if not -- open it:
Sub openfile_1() Dim x As Workbook For Each x In Workbooks If x.Name = "1.xls" Then Exit Sub End If Next Workbooks.Open FileName:="1.xls" End SubThanks to: |
Go Top | |
Q: | How to close several files at a time and ignore the question of saving or not? |
A: |
To close any workbook use close metod with false argument as follows:
Workbooks("1.xls").Close FalseThanks to: |
Go Top | |
Q: | How to setup password to printing worksheet? |
A: |
You can just handle beforePrint Event as follows:
Private Sub Workbook_BeforePrint(Cancel As Boolean) Dim password As String password = Application.InputBox("enter password") Cancel = Not password = "right_password" End SubThanks to: |
Go Top | |
Q: | How to let Excel to show the text of a certain cell in black or in red, according to the content of the cell. |
A: |
something this:
Private Sub Worksheet_Change(ByVal Target As Excel.Range) Dim r, c As Long For r = 1 To Target.Rows.Count For c = 1 To Target.Columns.Count If Target.Cells(r, c).Value = "one" Then Target.Cells(r, c).Font.ColorIndex = 1 elseif Target.Cells(r, c).Value = "two" Then Target.Cells(r, c).Font.ColorIndex = 2 End If Next Next End SubThanks to: |
Go Top | |
Q: | How to run a macro when a cell value changes |
A: |
Worksheet_Change(ByVal Target As Excel.Range) is trigered. You can handle its like follows:
Private Sub Worksheet_Change(ByVal Target As Excel.Range) if target.address="$A$3" then 'your macto end if End SubThanks to: |
Go Top | |
Q: | Is there function in Excel which returns range of range object as string. |
A: |
Range.Address Thanks to: |
Go Top | |
Q: | How to freeze screen updating – make all changes and then redraw it: |
A: |
Application.ScreenUpdating=false ‘your code Application.ScreenUpdating=trueThanks to: |
Go Top | |
Q: | Where the custom menu information is stored? |
A: |
in excel 7 -- personals.xls. (i think it is somwhere in excel dir)
in excel 97 -- windows\user_name8.xlb (in my case it is igor8.xlb) Thanks to: |
Go Top | |
Q: | Finally, you miss the feature of attaching a menu to a workbook.It's still there, but in another place. |
A: |
Real easy, In "Customize mode", if you have created a new toolbar (yes…menus are toolbars too), click on the "Attach" button. You have now found where to attach one or more of your own toolbars to the workbook. Thanks to: Ture Magnusson |
Go Top | |
Q: | has anyboy on the list experience with programming to tcp/ip directly from vba within excel? |
A: |
if you have office 97 developer edition, you can use winsock activex control (or internet transfer control depending on the application you want to write). these controls also come with vb 5.0. refer to chapter 15 of office 97 vb programmer's guide (available online at ms website).
i have used internet transfer control and it worked fine for my need. i am working on using winsock control. hope this helps and good luck! Thanks to: keyuan jiang |
Go Top | |
Q: | How to create a range object with non-contiguous areas? |
A: |
You can use the Union method to create a range object with non-contiguous areas. Something like: (The only trick is that Union will not accept a null range, so you must set
the first area before using it).
Sub SelectCols() First = True For i = 1 To 10 If Cells(1, i).Value = "X" Then If First = True Then Set R = Columns(i) First = False End If Set R = Union(R, Columns(i)) End If Next i R.Select End SubThanks to: John Green |
Go Top | |