Excel VBA cell comboBox (data validation list) set selected index? - excel

I am using Excel VBA to make an simple app, I have a CELL with Data Validation make it a "ComboBox", can I use VBA code to set the selectedIndex for it? and how?

If I understood correctly and you need to "put some value into CELL when worksheet with CELL on it is opened" then try to insert following code into ThisWorkbook module:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "%%SheetWithCELL%%" Then
Sh.Range("CELL").Value = "%%NeededValue%%"
End If
End Sub
Where
%%SheetWithCELL%% - Name of sheet where CELL is located;
%%NeededValue%% - Value, which you need to insert into CELL.
P.S. Code assumes that "CELL" is the actual name of some cell (named range)

Related

AutoFit on rows that are referenced from different sheet using function - VBA

I would like to make an autofit to the referenced cells from another sheet, so everytime when I add some infos on one sheet my row height expands, the code below makes that possible:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Target.WrapText = True
Target.EntireRow.AutoFit
End Sub
But as I say this is possible only if I manually typing something anywhere in my workbook, but what about when I have on another sheet (same workbook) some functions that are referencing those values? the fields are not expanding even though it is a right match...
For instance I am having this infos in one sheet (wrapped) as one value:
Column A
AAABBBCCCDDD
EEEFFFGGGHHH
and when using this function on another sheet to make them referenced:
=IFNA(IF(ISBLANK(INDEX(INDEX(Table1[Systembezeichnung];MATCH(Ausdruck!$A$5;Table1[Nummer];0)):INDEX(Table1[Systembezeichnung];MATCH(Ausdruck!$A$5;Table1[Nummer];0)+9);ROW(4:4)));"";INDEX(INDEX(Table1[Systembezeichnung];MATCH(Ausdruck!$A$5;Table1[Nummer];0)):INDEX(Table1[Systembezeichnung];MATCH(Ausdruck!$A$5;Table1[Nummer];0)+9);ROW(4:4)));"")
it gives me everything in one row but not expanding or wrapped as I want:
Column A
AAABBBCCCDDDEEEFFFGGGHHH
It has to be automatically wrapped for the sake of dynamic document that I am providing, without loss of infos or that some infos are missing or "hidden".
You can use Workbook_SheetCalculate event. A simple example will be like below.
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
With Worksheets("Sheet2")
.Range("D2:D5").WrapText = True
.Range("D2:D5").EntireRow.AutoFit
End With
End Sub
So, if you have any formula to range D2:D5 on sheet2 and when you will change any value to any sheet of workbook and if it changes any value to D2:D5 then that cell row will automatically fit.
Edit: For full sheet you can try below codes.
With Worksheets("Sheet2")
.Cells.WrapText = True
.Cells.EntireRow.AutoFit
End With
Microsoft reference here

How can I dynamically fill an Excel combobox with the content of a named table on a different sheet?

on one Excel sheet I have a combobox.
On another sheet, I have a table with a named column ("KontoNr") which should feed into the combobox. The table and column are named in the name manager and are shown as =tabKontenplan and KontoNr =tabKontenplan[KontoNr].
Now I am unsuccessfully trying to fill the comboxbox like this:
combobox.listfillrange = "=tabKontenplan![KontoNr]"
And
combobox.listfillrange = "=KontoNr"
also does not work.
There ist no error, the combobox just remains empty... why is that?
I added an empty ActiveX ListBox control on a worksheet, then typed a couple of random values in a Range on that worksheet, named that range VALUES, and then added this code in the worksheet's code-behind:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ListBox1.ListFillRange = "VALUES"
End Sub
And that's all I needed to do. Remove the = in front of your named range, and it "just works". Make sure the defined name is in scope, and all will go well.
If the named range is scoped to the other worksheet, it won't work. Delete the name and re-create it in workbook scope.
Assuming that "another sheet" is Sheet2,
no good: combobox.listfillrange = "=" & Sheet2.Range("KontoNr").Address
A corrected answer:
combobox.listfillrange = "=Sheet2!" & Sheet2.Range("KontoNr").Address

Excel enter formula into cell

Function bd()
Sheets("sheet1").Range("A1").value=1
End Function
This is my function.
Why is it that when i enter =bd() into any cell in sheet1, the data in A1 does not change to 1? I do not want to use the button to change the value.
Why it does not work:
If you're calling your function from an Excel formula, your function becomes a User-Defined-Function (=UDF).
UDF have to follow special rules - the main one being that it cannot by any means change anything in Excel - it shall only return a value! Your function clearly violates this rules.
(For reference: the other main restriction is that it shall also not access any data outside the parameters that were passed to it when calling the function. Thus, you cannot use MyUDF=Range("A1").Value - but only something like MyUDF=rngParam1.Value*2!
Alternative approach:
If you want to change the worksheet but don't want to use a button, you need to think of some kind of trigger event. Look at the possible Worksheet and workbook events - you'll find a list of events here - and detailed instruction how to use them here.
For instance, you could use the Activate of Sheet1. For this place this code in the Sheet1 code module:
Private Sub Worksheet_Activate()
Range("A1").Value = 1
End Sub
Now every time that sheet1 gets activated, A1 will be reset!
Try This
Copy this code on your 'Thisworkbook' on vba
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If ActiveSheet.Range("A1").Value = "bd()" Then
Sheets("sheet1").Range("A1").Value = 1
End If
End Sub
Now if you enter 'bd()' (without Quotes) on cell A1 on Any Sheet and press enter the cell A1 of Sheet one will Change to 1
OR
Copy this code on your 'Thisworkbook' on vba
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Sheets("sheet1").Range("A1").Value = "bd()" Then
Sheets("sheet1").Range("A1").Value = 1
End If
End Sub
Now if you enter 'bd()' (without Quotes) on cell A1 on Sheet one and press enter the cell A1 of Sheet one will Auto Change to 1
Hope this works

Transfer values from one sheet of a workbook to another by clicking on the cell

I have 5,000 part numbers contained on one sheet of an Excel workbook (Part Numbers), ranging from cell A1:A5000. I want to find a way to click on any one of those part numbers and have it automatically populate into cell D7 on another sheet of the same workbook (Price Sheet). What is the easiest way to accomplish this task?
To do it that way, you will have to write VBA code to catch every SheetSelectionChange event to see if the new selection range is in your cells A1:A5000. And then if it is, execute the VBA code to fill OtherSheet!D7.
If I recall correctly, the VBA code to do this would look something like this:
Private Sub WorkSheet_SelectionChange(ByVal Target As Range)
'Check if it is in the range A1:A5000
If Target.Column = 1 And Target.Row <= 5000 Then
'get the target worksheet
Dim TargetWS As Worksheet
Set TargetWS = Sheets("OtherSheetsName")
'copy the value of the just selected cell to D7 on the target WS
TargetWS.Cells(7, 4).Value = Target.Value
End If
End Sub
(Oops, forgot about the need for "SET" in VBA.)
You can do this without VBA:
Select the partnumbers A1:A5000 and type PartNumbers in the Name Box (to the left of the formula bar) and press carriage return (OartNumbers should now be visible in the Name Box whenever you select a1:a5000
Now go to cell D7 on Price Sheet, and use Data Validation-->List and enter =PartNumbers in the Source box
Now you can select any of the 5000 part numbers from the dropdown in cell D7

How can I "remind" users to fill in cells on a worksheet

Hello: I have a set of cells on a worksheet called "Docs". The cell range is (B13:C23).
When users get taken to this page, they are meant to fill out each of these cells with a value from 0 through 6. My Question: Is there some code that I can attach to this sheet where, if a user does not fill in a cell with anything (ie. leaves it blank) and tries to leave the sheet or close the workbook, they are somehow reminded to fill it in? Or is there a way to not let them leave the sheet until it's completed? Thanks.. Allan
You can attach a macro to the change event of the form. Excel comes with built in validation but it does not work that well. For instance if someone pastes a value into the cell it does not validate what is pasted.
Start by creating a range by selecting the range of cells to be validated, right click and select "Name a Range". Note that I am testing this with Excel 2007. Say you call your range "InputRange".
Then open the VBA editor and create a procedure for the change event.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim vrange As Range, cell As Range
Set vrange = Range("InputRange")
If Intersect(vrange, Target) Is Nothing Then Exit Sub
For Each cell In Intersect(vrange, Target)
If cell.Value < 1 Or cell.Value > 6 Then
MsgBox "Invalid Entry", vbCritical
Application.EnableEvents = False
cell.ClearContents
cell.Activate
Application.EnableEvents = True
End If
Next cell
End Sub
Note you can attach to any event that suits you.
You could give these cells conditional formatting, making them red if empty.
Try writing a vba macro. Alt + F11 opens the VB Editor. Check out this SO post for VBA tutorials.
There are worksheet and workbook events that you can use. For example, Workbook_BeforeClose or Workbook_SheetChange. If you create methods for those events you can put code inside that checks that the required cells are filled.

Resources