I am using excel 2010.
I want to clear the content of a combo box in my sheet(clear to blank like when it's not selected), but I don't know how to select and clear. I tried to select a combo box like this:
Sheet1.ComboBox1.Clear
But no 'ComboBox' is under the Sheet1 object. The only to select my combo box is use this:
Sheet1.Shapes("Drop Down 24")
I don't know how to select and clear the content, can anyone help me?
What about
ActiveSheet.Shapes.Range(Array("Drop Down 24")).Select
With Selection
.ListFillRange = ""
End With
I assume you actually want to make the displayed value of your control blank. In that case, for a Drop Down Object, as you indicated you would do this:
Sheet1.Shapes("Drop Down 2").OLEFormat.Object.Value = 0
Where 0 indicates which element from the list is selected I.E. none.
If that doesn't work, then you're probably actually dealing with a ComboBox in which case you want to use this:
Sheet1.Shapes("Drop Down 2").OLEFormat.Object.Object.Value = ""
Note this code was created and tested in Excel 2003 (what I have on this machine) so the path to reaching the actual object might vary slightly on Excel 2010.)
As an autentic programmer I would rather prefer this way. That don't depends on excel "range memory selection" on the sheet.
Set oCombo = Sheets("SheetName").Shapes("cmbComboName").ControlFormat
For I = oCombo.ListCount To 1 Step -1
oCombo.RemoveItem (I)
Next
To reset a Drop Down List to a blank cell but still maintaining the list for future use.
Create a Macro to clear the cells. During recording simply select the cell and select "clear contents". This will set the selection to a blank cell but still keeps the drop down list in place.
It would look like this for example.
Range("H3").Select
Selection.ClearContents
Related
I have the following code to simulate some cells behave like buttons
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Application.ScreenUpdating = False
If Target.Cells.Count = 1 Then
'~~~~~~ pseudocode ~~~~~~
If {select cell is one of the chose ones}
{do some stuff}
End If
'~~~~~~ pseudocode ~~~~~~
End If
Range("A1").Select
'Application.ScreenUpdating = True
End Sub
It doesn't matter whereas I use the ScreenUpdating code, the "Selection box" is flying around from A1 to the selected cell and back and it make the sheet much slower.
Can this flying (animation) selection box be stoped?
So far I have found this, not possible to hide, but noting about the flying efect:
Hide the cell selection box in Excel
Edit:
I need (I think so) edit capabilities on the sheet, therefore the option of not changing selection cell is not applicable. Due to:
most of the sheet is informative, and should be available for copy (not edited)
some cells are input forms (free text thing), selection as usual
some cells should behave like buttons (plus/minus for a numeric value, sclaes, simple stuff, but thousand of then, so much easier do/maintain by code), and user must not edit them
grouping should be available, (so that's complicate protecting the sheet)
I am not closed to the option : Range("A1").Select after each (most) of user interaction, but no other method comes into mind to me now.
An example:
I know some would say: "you should make this out from excel", and I agree with you, but this is a mandatory thing, I do not have the power to raise this question
As you can see, I got the "flying selection" that I try to get rid off
cell A1 is already hodden, that will do most of the trick
final version sure will go with hidden gridlines and headlines
rows groups exist, and are important, so no protection possible
all the functionality, I can do easy with vba, just problem with the animation
Maybe this is not the answer that you have been waiting for, but as Mathieu mentioned in his comment, please try to avoid using Selection.
It does make things slower and often causes errors (in example try selecting cell from hidden sheet). Instead just do something with the range that you define with your if statements directly. Every property of Cell or Range can be accessed directly.
Hope it helps.
Not sure how you can achieve you "flying select box" problem, but at least you could add this code, so opening/closing groups are available on protected sheets:
'Password Protect Current Sheet
ActiveSheet.Protect Password:="Add_here_your_password", UserInterfaceOnly:=True
'Enable Group Collapse/Expand Capabilities
ActiveSheet.EnableOutlining = True
How about trying to remove the "back to A1" as much as possible?
Maybe do it only on absolutely necessary, or move back to the changed value (the 33 in your example), or to the question title (in your multi-option example)
Is there any way in Excel to make it so that a particular tab isn't included when you print the entire workbook? As in, Sheet1, Sheet2, and Sheet3 print, but not Sheet4.
So like when someone goes to File -> Print or whatever, and hit OK, etc, everything prints out except for a particular designated tab (which I assume I have to "hide from printing" somehow).
I'm sending this workout out to a bunch of people so I'm trying to avoid forcing them to enable/run macros and all that stuff. Is there just a property of a sheet somewhere like "exclude from print"?
These answers will require the use of macros.
Hide the sheet before printing.
sheets("WorksheetToNotPrint").visible = xlSheetHidden 'or 0
or
sheets("WorksheetToNotPrint").visible = xlSheetVeryHidden 'or 2
'used if you don't want people to unhide the worksheet without knowing code
and
sheets("WorksheetToNotPrint").visible = xlSheetVisible 'or -1
when done
another option is to print the sheets you want printed (may be useful if you only want a few sheets printed:
Sub Print_Specific_Sheets()
Sheets(Array("Sheet1", "Sheet2", "Sheet4")).Select
ActiveWindow.SelectedSheets.PrintOut
Sheets("Sheet1").Select
End Sub
Actually the answer seems to be surprisingly simple.
If you hide the sheet, it will not be printed when you print the entire workbook.
You won't need a macro, just
right click on the sheet name
choose hide
If needed you or other users can unhide it as well.
If you use the BeforePrint event then you can alter what the built in print function can do. The BeforePrint macro is inside 'ThisWorkbook' under 'Workbook'.
For example you can hide sheet 4 so it doesn't print like this:
Private Sub aWorkbook_BeforePrint(Cancel As Boolean)
Sheets(4).Visible = xlSheetHidden
End Sub
The downside to this method is there is no AfterPrint method to unhide the sheets. So you need to circumvent the print dialog. There are several ways to do this but here is one very simplistic method. (Note: This might not be a good solution if you have many other printing scenarios that you need to account for)
Here I am telling exactly what sheets to print (only sheet 1 and 4). You can use names instead of numbers and you can even create a loop and look for variables to determine what sheets you want to print.
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Application.EnableEvents = False
Sheets(Array("1", "4")).PrintOut , , 1
Application.EnableEvents = True
'//prevent the default print
Cancel = True
End Sub
Just set the print area on the tab you don't want to print to a single blank Cell, This will mean that it will print a single blank page rather than the reams of data that I presume you are trying to avoid printing.
To do this manually just select all the sheets you want to print by clicking on the first sheet tab label, then hold down the 'Shift' key and click on the last sheet tab label. (If you have a large number of sheets use the small arrow keys to navigate)
If you need to unselect a sheet in the middle hold down the CTRL key and click that sheet.
Alternately you can hold the CTRL key and click on each sheet tab label.
The sheet colour will be white (with a gradient on the none active sheet) to show they have been selected.
WARNING: When multiple sheets are selected any change on one will be repeated in the same cells on all of them.
If you do not want to use VBA or hide the sheet, you could just set print area on an empty cell of the sheet you do not want to print.
The tab will still be printed but the result will be an empty sheet.
Using an easy Macro:
Just put this lines inside a new Macro, and replace the names of the sheets yo DO want to be printed:
Sheets(Array("Name_of_Sheet_1", "Name_of_Sheet_2")).Select
Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
Why all the crazy coding?
Just click on the first tab you want to print and make it active.
Then hold down control, and click on each additional tab you want to print, excluding the tab, or tabs you don't want to print.
Then go to "file" and then "print" like you normally would. Make sure "print active sheets" is selected, then print.
It prints only the tabs you selected.
Simple, and no need to complicate things to ridiculous levels of unnecessary silliness.
I have made two active X combo boxes for a list of 220 sites. Before making the active x combo box i have made a sheet that searches through my data and finds the sites that match with what i am typing as i go.
I then used the name manage, refering to the formula in the first cell of the list
=Sheet1!$G$2:INDEX(Sheet1!$G$2:$G$220,COUNTIF(Sheet1!$G$2:$G$220,"?*"))
I have then writen this in the ListFillRange in the properties of my combo box.
It works fine for one, but once i had made the second one and selected the site the first one will no longer let me search through.
I have used the same formulas on both but they originate from different sheets to see if this fixed the problem however that was unsuccessful. (the boxes are on different sheets) When i click on the next sheet after using the box on the first sheet, it still shows part of it as if it is crashing.
The boxes are independent so I'm not sure what to do next as i need to add another 3 on separate sheets.
I am also using this code for each box
Private Sub ComboBox1_Change()
ComboBox1.ListFillRange = "MList"
Me.ComboBox1.DropDown
End Sub
and similar for the other button but with a different range.
There is no need to use VBA for this, the Change Events specifically, if you just want to use and fill the combo boxes with Named Ranges.
The scenario I think you try to do is:
Create Named Ranges that will be the source of your combobox:
Fill the range with your data, select the range, Right Click, Select Define Name and give the range a name. MList in your case I believe.
Create Combobox:
Goto Developer Tab, Insert in your case ActiveX ComboBox, Draw it on your sheet, right click the ComboBox, select properties, find ListFillRange in properties and enter the name of the Named Range you created in step one
Repeat for Combobox 2, with the same or a different Named Range depending on what you try to do
Leave Design Mode
Boths Comboboxes can now be used to type in what you are looking for.
If this is not what you tried to do, please try edit your question and in detail try to explain what you try to do and what you like to accomplish by doing so.
Is there any way in Excel to make it so that a particular tab isn't included when you print the entire workbook? As in, Sheet1, Sheet2, and Sheet3 print, but not Sheet4.
So like when someone goes to File -> Print or whatever, and hit OK, etc, everything prints out except for a particular designated tab (which I assume I have to "hide from printing" somehow).
I'm sending this workout out to a bunch of people so I'm trying to avoid forcing them to enable/run macros and all that stuff. Is there just a property of a sheet somewhere like "exclude from print"?
These answers will require the use of macros.
Hide the sheet before printing.
sheets("WorksheetToNotPrint").visible = xlSheetHidden 'or 0
or
sheets("WorksheetToNotPrint").visible = xlSheetVeryHidden 'or 2
'used if you don't want people to unhide the worksheet without knowing code
and
sheets("WorksheetToNotPrint").visible = xlSheetVisible 'or -1
when done
another option is to print the sheets you want printed (may be useful if you only want a few sheets printed:
Sub Print_Specific_Sheets()
Sheets(Array("Sheet1", "Sheet2", "Sheet4")).Select
ActiveWindow.SelectedSheets.PrintOut
Sheets("Sheet1").Select
End Sub
Actually the answer seems to be surprisingly simple.
If you hide the sheet, it will not be printed when you print the entire workbook.
You won't need a macro, just
right click on the sheet name
choose hide
If needed you or other users can unhide it as well.
If you use the BeforePrint event then you can alter what the built in print function can do. The BeforePrint macro is inside 'ThisWorkbook' under 'Workbook'.
For example you can hide sheet 4 so it doesn't print like this:
Private Sub aWorkbook_BeforePrint(Cancel As Boolean)
Sheets(4).Visible = xlSheetHidden
End Sub
The downside to this method is there is no AfterPrint method to unhide the sheets. So you need to circumvent the print dialog. There are several ways to do this but here is one very simplistic method. (Note: This might not be a good solution if you have many other printing scenarios that you need to account for)
Here I am telling exactly what sheets to print (only sheet 1 and 4). You can use names instead of numbers and you can even create a loop and look for variables to determine what sheets you want to print.
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Application.EnableEvents = False
Sheets(Array("1", "4")).PrintOut , , 1
Application.EnableEvents = True
'//prevent the default print
Cancel = True
End Sub
Just set the print area on the tab you don't want to print to a single blank Cell, This will mean that it will print a single blank page rather than the reams of data that I presume you are trying to avoid printing.
To do this manually just select all the sheets you want to print by clicking on the first sheet tab label, then hold down the 'Shift' key and click on the last sheet tab label. (If you have a large number of sheets use the small arrow keys to navigate)
If you need to unselect a sheet in the middle hold down the CTRL key and click that sheet.
Alternately you can hold the CTRL key and click on each sheet tab label.
The sheet colour will be white (with a gradient on the none active sheet) to show they have been selected.
WARNING: When multiple sheets are selected any change on one will be repeated in the same cells on all of them.
If you do not want to use VBA or hide the sheet, you could just set print area on an empty cell of the sheet you do not want to print.
The tab will still be printed but the result will be an empty sheet.
Using an easy Macro:
Just put this lines inside a new Macro, and replace the names of the sheets yo DO want to be printed:
Sheets(Array("Name_of_Sheet_1", "Name_of_Sheet_2")).Select
Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
Why all the crazy coding?
Just click on the first tab you want to print and make it active.
Then hold down control, and click on each additional tab you want to print, excluding the tab, or tabs you don't want to print.
Then go to "file" and then "print" like you normally would. Make sure "print active sheets" is selected, then print.
It prints only the tabs you selected.
Simple, and no need to complicate things to ridiculous levels of unnecessary silliness.
I am creating a form in excel (not a userform) and I am populating the listbox using cells. However, these cells are sometimes A1:10 and sometimes they are A1:A4. Is there a way to dynamically change what is shown in the listbox?
Right now, when I use A1:10 and there are only 4 cells populated, I get the list of 4 populated cells followed by 6 blank entries. I'd like to get rid of the 6 blanks when there are only 4.
Unfortunately, this is more of a workaround than a solution. However, it may be able to do what you need it to do. I've been hitting the same wall you are with trying to make ranges dynamic.
Without seeing some code to know exactly what you're doing, try something like this.
Private Sub ListBox1()
x = 1
'Add items to listbox until you reach an empty cell.
Do while Cells(x,1) <> ""
ListBox1.AddItem Cells(x,1)
Loop
I'm not very familiar with listboxes outside of userforms but this should do approximately what you want to do.
Edit your original post with your code so we can try and get a better understanding of what you've tried and what you're trying to do.
You can create a named range using a dynamic formula. Either of the below formulas will work.
=A1:INDEX($A$1:$A$10,MATCH("",$A$1:$A$10,0)-1)
=OFFSET($J$7,0,0,MATCH("",$J$7:$J$32,0)-1)
To create the named range, click ctrl+F3 then click new, insert one of the two options above into the "refers to:" section, then label the new range whatever you would like. in the "row source" section of the listbox simply type in the name you selected for your new named range.