i'm wondering if someone could help me with a little issue.
I have a textbox in an userform where people can add numbers for example 3. I need an macro when a checkbox is on then a few rows needs to be hidden. For example total range is B3 to B50 and if people typ 3 in the textbox B3 + 3rows needs to be visibele and the rest needs to be hidden.
Sub CommandButton1_Click()
If UserForm12.CheckBox2.Value = True Then
Rows("[B4+Karel]:B55").Hidden = True
End Sub
Could some one help me to fix this?
if people typ 3 in the textbox B3 + 3rows needs to be visibele and the rest needs to be hidden.
First hide all rows in the range and then show only the relevant rows. I am assuming that the name of the textbox is TextBox1. Change as applicable.
Is this what you are trying? (Untested). Also I have not done any error handling. I am sure you can take care of that?
Rows("3:50").Hidden = True
Rows("3:" & 3 + Val(TextBox1.Text)).Hidden = False
Note: When you are hiding/showing rows, you do not need the column names. You can directly work with row numbers as shown above.
Related
I am trying to count all blank cells in a row while ignoring hidden columns but I can't find any formula that returns the right answer. The SUBTOTAL function only works on hidden rows but I cannot change my data to hide rows instead of columns.
For example, I wan to count blank cells from B2:BA2 but need to ignore any blank cells from hidden columns between that range.
Appreciate any help!
You can try the following VBA function:
Function CntBlnk(Rng As Range)
Dim Cell As Range
Application.Volatile
For Each Cell In Rng
If Cell.EntireColumn.Hidden = False And Len(Trim(Cell)) = 0 Then
CntBlnk = CntBlnk + 1
End If
Next Cell
End Function
Then call the function CntBlnk in the required cell.
A VBA solution is probably the best option here. A set-up using worksheet formulas alone is possible, viz:
=SUMPRODUCT(N(CELL("width",OFFSET(B2,,COLUMN(B2:BA2)-MIN(COLUMN(B2:BA2))))>0),N(B2:BA2=""))
or, Office 365:
=SUMPRODUCT(N(CELL("width",OFFSET(B2,,SEQUENCE(,COLUMNS(B2:BA2),0)))<>0),N(B2:BA2=""))
though it suffers three drawbacks:
It's volatile
Despite said volatility, changes to the column widths in the range passed will not trigger a recalculation of this formula; the user will need to perform a manual recalculation
Columns having a column width of less than 0.5 will be treated as hidden
If you have Excel 365 and are open to using a Lambda, you could also try:
=LAMBDA(range,index,IF(index>COLUMNS(range),0,ISBLANK(INDEX(range,index))*(#CELL("width",INDEX(range,index))>0)+CountVisBlanks(range,index+1)))
where the Lambda is named as CountVisBlanks in the name manager.
As with the other answer using Cell, it suffers from the issue that Cell doesn't update until you force the sheet to re-calculate.
Called as:
=CountVisBlanks(b2:ba2,1)
I have an userform with a few texbox and i want that when you put the value on the first one scan the data sheet and if it found the same value it fill the rest of textboxes, i think i can manage to do it if i do it in two steps. First take the value of textbox1 as variable and with it scan the data an generate the second userform with the data already paste on it. But is there a way to do it live? at the moment i put data in textbox1 shows the data of the rest of the columns on the others textboxes?
Also i was trying to do more or less the same thing in a sheet with an vlookup formula (VLOOKUP(A27,BDD!A:B,2,FALSE)) and it worked but the problem is that i want the formula to change the value of the cells only in case it found a mach in the data and also i don't want the formula in the cells i want to change so i can put new data without problem.
Lets say i have in the sheet "bdd" numbers in the first columns and names in the second. I want in another sheet to put a number and if that number already exist in the bdd i want to have the name near to it but i dont want the formula near to the numbers because i want to be able to put new numbers and names if necessary.
bdd:
101 Antonio
102 Luis
sheet
101 Antonio (At the moment i finish writing 101)
103 Peter (nothing happens because it isn't in the bdd yet and i have to type peter to complete that line or put 103 peter in the bdd)
Sorry if it wasn't clear i tried :P Thanks in advance
If data is in this range
Private Sub TextBox1_Change()
'Skip if value is not there
On Error Resume Next
'take any random cell to store textbox1's value
Range("C4").Value = TextBox1.Text
'Vlookup to get the value
TextBox2.Text = Application.WorksheetFunction.VLookup(Range("C4").Value,Sheet1.Range("A:B"), 2, 0)
End Sub
I can click on my ComboBox and see the values of Column1 and Column2, but after I click on off the ComboBox, the value in Column1 is always displayed and I want the value of Column2 displayed.
I tried this:
With ComboBox2
.Value = "None"
.ColumnHeads = True
.ColumnCount = 2
.ColumnWidths = "50;100"
.RowSource = "SetupQuestions!A42:B48"
.BoundColumn = 2
End With
That didn't set the value as I thought it would.
I tried this:
Private Sub ComboBox2_AfterUpdate()
ComboBox2.Value = ComboBox2.Column(2)
End Sub
That didn't set the value as I thought it would.
How can I force the ComboBox to display the value in Column2 after a selection is made?
The DropList of a ComboBox can show multiples columns, but after selecting a row, it can show only one column as Text. To show the second column use the property TexColumn.
Me.ComboBox1.TextColumn = 2
If you are only concerned with appearances, there is a workaround
Private Sub ComboBox2_Click()
With ComboBox2
.Text = .List(.ListIndex, 0) & " | " & .List(.ListIndex, 1)
End With
End Sub
Argh! It's not .Value
It's .Text
Private Sub ComboBox2_AfterUpdate()
Me.ComboBox2.text = Me.ComboBox2.Column(1)
End Sub
I just came across here because I was looking to solve this too. but other people's response helped me find the answer.
If you haven't gotten it yet, here is my solution.
Private Sub ComboBox_AfterUpdate()
ComboboxList.Text = ComboboxList.Column(0) & " " & ComboboxList.Column(1)
End Sub
An alternative you can use without VBA.
Combo row source (adjust for your situation):
SELECT Adults.aID, Trim([Adults].[LastName]) & ", " & Trim([Adults].[FirstName]) AS Expr1
FROM Adults WHERE ((Not (Adults.LastName)=("isNull")))
ORDER BY Adults.LastName, Adults.FirstName;
Basically, make your second column a composite single field via SQL.
Bound column: 1, Column count: 2, Column widths: 0cm;4cm
You can use this technique to display whatever you want by building the string representation as a single field.
The reason is in your setting of ColumnWidth. Your combobox shows two columns. The second one can't be displayed because the total width of your box is insufficient. Therefore you see the first column only. Set the ColumnWidth to "0;100" and you will see the second column. Make sure that there is a working relationship between the width of the box and that of the columns to be displayed within it.
Use a text field ond the right side of the combo box. Set the number of columns in the combo box to 2.
Set the list width the size of both combo and text together (a+b)
Set the columns width for the size of the combo and the size of the text (a;b)
Since the columns property is an 0 based array of columns, set the source of the text field to "=[MyCombo].Columns(1)" to display the second field.
When you drop down, the first column should be exactly under the combo box and the second column under the text box.
When you update the combo, the text box should update its value accordingly.
Additionally you may want to set the text box properties locked to true and enabled to false.
Open Design View
Open Property Sheet
Under the "Data" tab click on the three dots to the right of the "Row Source" item
In the Query Builder that opens, move the desired column you want to view in the combo box field where in the place where the currently viewed column is
Change any VBA code accordingly. For Example: If Column(1) was showing in the combo box field and you moved Column(2) to the place of Column(1), then Column(2) becomes Column(1) and Column(1) becomes Column(2). This will affect any VBA Code referring to Column numbers and also may affect the default value assessed for that Combo Box when a column number is not specified in the VBA code.
I guess this needs a VB Macro. I need to know how to do this. I have an excel sheet,lets say with four columns - NAME, AGE, GENDER and BIRTHDAY. Suppose I want to ensure that the AGE column is always filled for all the records and is always positive and above zero. Number of records in the file are not fixed so file can have 5 rows or 500 rows. I need to make sure that if any of the columns NAME, GENDER or BIRTHDAY is filled in a row and AGE is blank, 0 or negative then excel should prevent the user from closing the file.
you can hack this solution a little to suit your needs
VBA Excel action when closing workbook
https://stackoverflow.com/a/27923799/6868389
Since the particular details of your question are so vague i can propose a solution to the closing part. The individual conditions you will have to add based on what exactly you have in the file and want but you can add them in between to set MyValidations to 1. As mentioned above this is based on Workbook_BeforeClose event.
https://msdn.microsoft.com/en-us/library/office/ff194765.aspx
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If MyValidations = 1 Then
Cancel = False
Else
MsgBox "Nope'"
Cancel = True
End If
End Sub
I have formulas in column B which subtotal every other column starting with column E. So column E, G, I, K, M, O, Q, S, and eventually more. The formula in row 4 is
=SUBTOTAL(109,E4,G4,I4,K4,M4,O4,Q4,S4)
When I hide one of these columns the total in column B does not adjust to reduce the value.
Any ideas what might be causing this. It seems that this should be straightforward. I've used subtotal before and it works in my other workbooks. This is a simple work book or so it seems. 55 rows on the sheet with 52 of them having this formula. None of them work. This will be simple I'm sure and I will be embarrased but I have tried a number of things unsuccessfully.
For SUBTOTAL pls read This
The SUBTOTAL function is designed for columns of data, or vertical ranges. It is not designed for rows of data, or horizontal ranges. For example, when you subtotal a horizontal range using a function_num of 101 or greater, such as SUBTOTAL(109,B2:G2), hiding a column does not affect the subtotal. But, hiding a row in a subtotal of a vertical range does affect the subtotal.
The only non-VBA way to do this (which I know) is using CELL like this:
=SUM(E4*(CELL("width",E4)>0),G4*(CELL("width",G4)>0),I4*(CELL("width",I4)>0),K4*(CELL("width",K4)>0),M4*(CELL("width",M4)>0),O4*(CELL("width",O4)>0),Q4*(CELL("width",P4)>0),S4*(CELL("width",S4)>0))
And the downside is, that you need to do any real action to recalculate (while subtotal does a recalculation by hiding a row, CELL does not). Simply enter a cell and hit enter (or just del while selecting an empty cell) or hit "calculate now" in the "formulas" tab.
It uses the behavior that hidden rows return a width of 0 (same for rows and height), but it checks for the whole column here (hiding a cell by row, doesn't change the width).
Also you can't use it in an array like this and the formula also doesn't look pretty nice. But at least, you can simply copy it down.
I suggesting to create helper row such as E12 - S12
=CELL("Width",E4)
put this code in every single column and hide entire row
Then in in sum cell, let's say T4 write this code
=SUMIF($E$12:$S$12,">0",E4:S4)
The only problem with this formula is, that particular event "Hide", "Unhide" Is not registered as event and sum cells are not recalculated. This could be fixed with VBA and UIeditor.
You will need UIeditor to add custom ribbons for your sheet to catch this specific event such as hide / unhide columns to do that you need editor downloadable here: http://www.rondebruin.nl/win/s2/win001.htm
Then add this code for your sheet:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" >
<commands >
<command
idMso="ColumnsHide"
onAction="Column_Hide_Macro"/>
<command
idMso="ColumnsUnhide"
onAction="Column_UnHide_Macro"/>
</commands >
So now when someone hit hide or unhide column macros "Column_Hide_Macro" or "Column_UnHide_Macro" are activated
Then add into your spreadsheet this
Sub column_hide_Macro(control As IRibbonControl, ByRef cancelDefault)
cancelDefault = False
alertTime = Now + TimeSerial(0, 0, 0.1)
Application.OnTime alertTime, "reCalc"
End Sub
Sub column_unhide_Macro(control As IRibbonControl, ByRef cancelDefault)
cancelDefault = False
alertTime = Now + TimeSerial(0, 0, 0.1)
Application.OnTime alertTime, "reCalc"
End Sub
Sub reCalc()
Application.CalculateFullRebuild
End Sub
Add this code into the module not sheet or workbook code!
Now you should get sheet recalculated every single time when you hide or unhide column.
Cheers...
In row 12 for example put "H" in every column you do not want to sum.
The formula in cell B4 then is
=SUMIFS(E4:S4,E12:S12,"<>H")
SUBTOTAL filters hidden rows, not hidden columns or cells.
This behavior is closely tied to the auto-filter feature that allows hiding rows using a dropdown control.
The simplest solution is to restructure your sheet and use hidden rows.