I would like please to assign a value to a cell by using 4 multiple choice check boxes which their value is 1 per each box if their condition is true. I would like to sum up their value in the linked cell so that cell value can vary. If for instance:
all checkboxes condition is true the value in the linked cell is 4
A few of them are true the value in the linked cell can vary from 1-3
All of them are false the value in the linked cell is 0
If CheckBox1.Value = True Then Range("D2").Value = 1
If CheckBox1.Value = False Then Range("D2").Value = 0 etc.
I wish to solve this problem by using a vba macro.
Just set the macro foreach checkbox before clicking it.
Public count As Integer
Public Sub btn_Click()
Dim cbName As String
If (count = Null) Then
count = 0
End If
cbName = Application.Caller
If (Sheets("Tabelle1").Shapes(cbName).ControlFormat.Value = xlOn And count < 4) Then
count = count + 1
ElseIf (count > 0) Then
count = count - 1
End If
Range("A1").Value = count
End Sub
you can do this way. based on linked cells get the 1 if true or Zero if false. and then sum all values.
for VBA solution
Private Sub CheckBox1_Click()
Dim str As Integer
str = 0
If CheckBox1.Value = True Then str = str + 1
If CheckBox2.Value = True Then str = str + 1
If CheckBox3.Value = True Then str = str + 1
If CheckBox4.Value = True Then str = str + 1
Range("D2").Value = str
End Sub
This is the answer to my problem:
Option Explicit
Sub CheckBox1_Click()
Dim count As Integer
If (count = Null) Then
count = 0
End If
count = 0
If ActiveSheet.Shapes("Check Box 1").ControlFormat = xlOn Then count = count + 1
If ActiveSheet.Shapes("Check Box 2").ControlFormat = xlOn Then count = count + 1
If ActiveSheet.Shapes("Check Box 3").ControlFormat = xlOn Then count = count + 1
If ActiveSheet.Shapes("Check Box 4").ControlFormat = xlOn Then count = count + 1
Range("A1").Value = count
End Sub
Related
I want to delete a row from InDatagridview that contains textbox.tex and transfer it to OutDatagridview
if i change the value of textbox it will search to InDatagridview and delete a row from InDatagridview that contains textbox.tex and transfer it to OutDatagridview.
If i change again the textbox value it will search to OutDatagridview and delete a row from OutDatagridview that contains textbox.text
and transfer it to InDatagridview.
Thanks!!!! a lot
Dim found, x, z As Boolean
Dim lookfor As String = Student_numberTextBox.Text
For dgv As Integer = In_TableDataGridView.Rows.Count - 1 To 0 Step -1
If In_TableDataGridView.RowCount > 0 Then
If lookfor = In_TableDataGridView.Rows(dgv).Cells(0).Value.ToString Then
found = True
End If
Else
found = False
End If
Next
If found = False Then
'In_TableBindingSource.AddNew()
If Out_tableDataGridView.RowCount > 0 Then
x = True
Else
x = False
End If
If x = True Then
Dim textout As String = Student_numberTextBox.Text
For inrow As Integer = Out_tableDataGridView.Rows.Count - 1 To 0 Step -1
If textout = Out_tableDataGridView.Rows(inrow).Cells(0).Value.ToString Then
Dim row2 As DataGridViewRow
row2 = Out_tableDataGridView.Rows(inrow)
Out_tableDataGridView.Rows.Remove(row2)
Else
'Exit For
End If
Next
End If
End If
If found = True Then
'Out_tableBindingSource.AddNew()
If In_TableDataGridView.RowCount > 0 Then
z = True
Else
z = False
End If
If z = True Then
Dim textin As String = Student_numberTextBox.Text
For outrow As Integer = In_TableDataGridView.Rows.Count - 1 To 0 Step -1
If textin = In_TableDataGridView.Rows(outrow).Cells(0).Value.ToString Then
Dim row1 As DataGridViewRow
row1 = In_TableDataGridView.Rows(outrow)
In_TableDataGridView.Rows.Remove(row1)
Else
'Exit For
End If
Next
End If`enter code here`
End If
End If
Between columns F and BM of the sheet, if any value within those columns is equal to "NULL" then don't hide, otherwise hide that column, the column before and column after. The loop should evaluate every other 3rd starting at column G and ending at column BM.
For example, if column G contains the value "NULL" then do nothing and go to column J (three columns forward). If column J now has no cells with value NULL then hide that column, the column before (column I), and the column after (column K).
This is what Im having trouble with. I am able to hide a column based on if the column contains the value NULL or not.
This is the code variations that I have attempted.
Sub SuspenseReport()
Dim allColumns As Range
Dim cell As Range
Dim col As Range
Dim x As Integer
Dim i As Integer
Application.ScreenUpdating = False
Set allColumns = Columns("C:E")
allColumns.Hidden = True
Set allColumns = Columns("BN:DY")
allColumns.Hidden = True
Set allColumns = Columns("EB:EU")
allColumns.Hidden = True
Dim rng1 As Range: Set rng1 = Application.Range("G2:BO8") 'maybe limit the range to just one column and range.offet at the end?
For Each col In rng.Columns
If cell.Value = "NULL" Then
cell.EntireColumn.Hidden = False
GoTo ExitIfStat
Else: cell.EntireColumn.Hidden = True
End If
Next col
ExitIfStat:
Next x
'below is another variation I attempted but the for loop would iterate on cell not column
'Dim i As Integer
'i = -1
'For Each col In Range("G1:BO8")
' i = i + 1
' If i Mod 3 = 0 Then
' If col.Value = "NULL" Then
' col.EntireColumn.Hidden = False
' Else: col.EntireColumn.Hidden = True
'col.Offset(0, -1).EntireColumn.Hidden = True
'col.Offset(0, 1).EntireColumn.Hidden = True
' End If
Application.ScreenUpdating = True
End Sub
Maybe something like:
Sub HideColumnWithoutNullString()
Dim range, colCount, rowCount, hasNull, rowsToCheck
Dim firstColumn, currentColumn, lastColumn
Set range = Application.range("G:BM")
firstColumn = range.Columns(0).Column
lastColumn = range.Columns(range.Columns.Count).Column
currentColumn = 0
rowsToCheck = 1
For colCount = firstColumn To lastColumn Step 1
hasNull = False
For rowCount = 1 To range.Rows.Count Step 1
If Application.Cells(rowCount, colCount).Value = "NULL" Then
hasNull = True
Exit For
End If
If rowCount >= rowsToCheck Then
Exit For
End If
Next
If Not hasNull Then
range.Columns(currentColumn).Hidden = True
Else
range.Columns(currentColumn).Hidden = False
End If
currentColumn = currentColumn + 1
Next
End Sub
Where rowsToCheck is the number of rows the script has to check for "NULL" on each column, if it only has to check the first row set its value to 1.
This one follows the same logic as Octavio's answer, but will check for an empty column or the value of "NULL".
Sub SuspenseReport()
Dim col As Range
Application.ScreenUpdating = False
Set Rng = Application.Range("G2:BO8")
vLr = ActiveCell.SpecialCells(xlLastCell).Row
For Each col In Rng.Columns
vFlag = False
For vrow = 2 To vLr
vX = Cells(vrow, col.Column).Value
If vX = "" Or vX = "NULL" Then
vFlag = True
End If
Next
If vFlag Then
col.EntireColumn.Hidden = False
Else
col.EntireColumn.Hidden = True
End If
Next col
Application.ScreenUpdating = True
End Sub
I'm creating a workbook, which tracks available rentals per month. It is divided into 12 sheets, one for each month. The first three columns of each sheet track the type of accommodation, number of bedrooms and what's included in the rental price. The concept there is that there will be a drop-down combo box that allows the user to fill in with a point-and-click option rather than typing things out in order to reduce input errors.
I set up a fixed array, the contents in which changes depending on what column that active cell is in, and then the array is assigned to the combo box. The code lives in the Sheet1 Module under the combo box code and the ThisWorkbook module calls it under SheetSelectionChange, so as to avoid repeating the code in each sheet.
A Standard Module makes the array public
All 12 combo boxes share the same name, cboOptions, and they populate correctly, regardless of what sheet is chosen. My problem is that none of the combo boxes return the listindex value of the choice that's made, regardless of the code telling it to do so. I've been testing to see the value of the position returned against the value of the position chosen, but I have not been able to establish a pattern. I thought about clearing the variables and arrays, thinking that might be what's messing with the code, but it seems to be having no effect. I've read what I could on the issue, but I'm out of ideas on what might be the problem...thank you in advance!
Code in Sheet1 module:
Private Sub cboOptions_Change()
Erase myarray()
cboOptions.Visible = True
cboOptions.Enabled = True
cboOptions.Clear
n = ActiveCell.Row
If n >= 3 And n < 10000 Then
If ActiveSheet.Range(ActiveCell.Address).Address = Range("A" & n).Address Then
myarray(1) = "Apartment"
myarray(2) = "Room"
myarray(3) = "Townhouse"
myarray(4) = "House"
ElseIf ActiveSheet.Range(ActiveCell.Address).Address = Range("B" & n).Address Then
myarray(1) = "1"
myarray(2) = "2"
myarray(3) = "3"
myarray(4) = "4"
myarray(5) = "5"
ElseIf ActiveSheet.Range(ActiveCell.Address).Address = Range("C" & n).Address Then
myarray(1) = "Heat & Water"
myarray(2) = "All-inclusive"
Else
cboOptions.Enabled = False
cboOptions.Visible = False
End If
End If
'ActiveSheet.cboOptions.ListIndex = 0
'Dim x As Long
'MsgBox ActiveSheet.Name
With ActiveSheet
.cboOptions.Left = .Range(ActiveCell.Address).Left
.cboOptions.Top = .Range(ActiveCell.Address).Top
.cboOptions.List = myarray()
With .cboOptions
'the problem is that x needs to get assigned a value from the combo box before it continues to execute
x = .List(.ListIndex)
'MsgBox x
End With
.Range(ActiveCell.Address) = x 'myarray(x)
.Columns(ActiveCell.Column).ColumnWidth = cboOptions.Width * 0.18
x = 0
Erase myarray()
End With
End Sub
Code in ThisWorkbook:
Sub Workbook_SheetSelectionChange(ByVal sh As Object, ByVal Target As Range)
Application.Run "Sheet1.cboOptions_Change"
End Sub
Code in Module1:
Option Explicit
Public myarray(0 To 5) As String
The nature of the problem seems to be that using more than one array for one combo box breaks down how the listindex values are calculated. I broke down the code to its component features to see if the issue persisted
1) Made a new file and put the code in Sheet1
2) Made separate fixed arrays for each kind of input
3) Created a separate routine for each kind of input
Using ON ERROR RESUME NEXT at the beginning of each routine overlooks the error and the code works properly. Alternatively, putting in a break where the integer variable is given the listindex value of the combo box allows the user to make a choice and assign a value to the integer variable, before continuing. Otherwise, its default value is -1 and returns an error; using .list(.listindex) did not make any difference, suggesting that the code needs to wait for user input (using a combobox event other than Change?).
May just need to establish a separate combo box for each column. Anyway, the code below is the sticks-and-stones version of the above, for a single sheet, and it will do the job if applied to each sheet module in the workbook:
Sub monthnames()
'add month names to the first cell of each sheet
Dim n As Integer
'Sheets(1).Activate
For n = 1 To 12
Sheets.Add After:=ActiveSheet
ThisWorkbook.Sheets(n).Cells(1, 1) = MonthName(n)
Next
End Sub
Private Sub cboOptions_Change()
Dim myarray(1 To 4) As String
Dim myarray2(1 To 5) As String
Dim myarray3(1 To 2) As String
cboOptions.Enabled = True
cboOptions.Visible = True
Dim n As Integer
n = ActiveCell.Row
If n >= 3 And n < 10000 Then
If Range(ActiveCell.Address).Address = Range("A" & n).Address Then
myarray(1) = "Apartment"
myarray(2) = "Room"
myarray(3) = "Townhouse"
myarray(4) = "House"
cboOptions.List = myarray()
inputdata myarray(), n
ElseIf Range(ActiveCell.Address).Address = Range("B" & n).Address Then
myarray2(1) = "1"
myarray2(2) = "2"
myarray2(3) = "3"
myarray2(4) = "4"
myarray2(5) = "5"
cboOptions.List = myarray2()
inputdata2 myarray2(), n
ElseIf Range(ActiveCell.Address).Address = Range("C" & n).Address Then
myarray3(1) = "Heat & Water"
myarray3(2) = "All-inclusive"
cboOptions.List = myarray3()
inputdata3 myarray3(), n
Else
cboOptions.Enabled = False
cboOptions.Visible = False
End If
End If
End Sub
Sub inputdata(myarray, n) 'myarray3, )
On Error Resume Next
Dim x As Integer
cboOptions.Left = Range(ActiveCell.Address).Left
cboOptions.Top = Range(ActiveCell.Address).Top
Columns(ActiveCell.Column).ColumnWidth = cboOptions.Width * 0.18
If Range(ActiveCell.Address).Address = Range("A" & n).Address Then
x = cboOptions.ListIndex + 1
Range(ActiveCell.Address) = myarray(x)
Else
Exit Sub
End If
End Sub
Sub inputdata2(myarray2, n)
On Error Resume Next
Dim y As Integer
cboOptions.Left = Range(ActiveCell.Address).Left
cboOptions.Top = Range(ActiveCell.Address).Top
Columns(ActiveCell.Column).ColumnWidth = cboOptions.Width * 0.18
If Range(ActiveCell.Address).Address = Range("B" & n).Address Then
y = cboOptions.ListIndex + 1
Range(ActiveCell.Address) = myarray2(y)
Else
Exit Sub
End If
End Sub
Sub inputdata3(myarray3, n)
On Error Resume Next
Dim z As Integer
cboOptions.Left = Range(ActiveCell.Address).Left
cboOptions.Top = Range(ActiveCell.Address).Top
Columns(ActiveCell.Column).ColumnWidth = cboOptions.Width * 0.18
If Range(ActiveCell.Address).Address = Range("C" & n).Address Then
z = cboOptions.ListIndex + 1
Range(ActiveCell.Address) = myarray3(z)
Else
Exit Sub
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call cboOptions_Change
End Sub
I am working on a sheet that has sections which hides/shows a number of rows based on a cell value (between 1-10). At the moment, I have a handful of nested if statements. This has made my workbook painfully slow. Is there a way to shrink this code? Thanks.
If Range("B87").Value = 10 Then
Rows("88:98").EntireRow.Hidden = False
Else
If Range("B87").Value = 9 Then
Rows("98").EntireRow.Hidden = True
Rows("88:97").EntireRow.Hidden = False
Else
If Range("B87").Value = 8 Then
Rows("97:98").EntireRow.Hidden = True
Rows("88:96").EntireRow.Hidden = False
Else
If Range("B87").Value = 7 Then
Rows("96:98").EntireRow.Hidden = True
Rows("88:95").EntireRow.Hidden = False
Else
If Range("B87").Value = 6 Then
Rows("95:98").EntireRow.Hidden = True
Rows("88:94").EntireRow.Hidden = False
Else
If Range("B87").Value = 5 Then
Rows("94:98").EntireRow.Hidden = True
Rows("88:93").EntireRow.Hidden = False
Else
If Range("B87").Value = 4 Then
Rows("93:98").EntireRow.Hidden = True
Rows("88:92").EntireRow.Hidden = False
Else
If Range("B87").Value = 3 Then
Rows("92:98").EntireRow.Hidden = True
Rows("88:91").EntireRow.Hidden = False
Else
If Range("B87").Value = 2 Then
Rows("91:98").EntireRow.Hidden = True
Rows("88:90").EntireRow.Hidden = False
Else
If Range("B87").Value = 1 Then
Rows("90:98").EntireRow.Hidden = True
Rows("88:89").EntireRow.Hidden = False
Else
If Range("B87").Value = 0 Then
Rows("88:98").EntireRow.Hidden = True
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
You have a whole lot of basically the same code. I took a look and tried to make it more arithmetical, which shortens the code. See if this works:
Sub t()
Dim myVal As String
Dim mainRow As Long, tweakRow As Long
Dim hideRange As Range, showRange As Range
Dim row1 As Long, row2 As Long
mainRow = 98
myVal = Range("B87").Value
If myVal = 10 Then
Rows(mainRow - 10 & ":" & mainRow - 10 + myVal).EntireRow.Hidden = False
ElseIf myVal >= 1 And myVal <= 9 Then
tweakRow = mainRow - 10
row1 = (mainRow - (9 - myVal))
row2 = (mainRow - (10 - myVal))
Set hideRange = Rows(row1 & ":" & mainRow).EntireRow
Set showRange = Rows(tweakRow & ":" & row2).EntireRow
Debug.Print "For a value of " & myVal & ", we will hide range: " & hideRange.Address & ", and show range: " & showRange.Address
hideRange.Hidden = True
showRange.Hidden = False
ElseIf myVal = 0 Then
Rows(mainRow - 10 & ":" & mainRow).EntireRow.Hidden = True
End If
End Sub
I might try a case statement.
Oh, or even use the ElseIf option which would reduce the amount of EndIf statements at the very least.
I think the case code looks something like this:
Select Range("B87").value
Case "1"
Case "2"
...
End Select
You don't need to use EntireRow when using Rows or 'EntireColumnwhen usingColumns`.
Rows("88:98").Hidden = True
If Range("B87").Value > 0 Then
Rows(88).Resize(1 + Range("B87").Value).Hidden = False
End If
I have a listbox with multiple (3 columns) selected items and I would like to move only the selected ones to another listbox, but this listbox should also contain 3 columns.
Before selecting new items, I've added the other items to the listbox1 with this code:
Private Sub UserForm_Initialize()
ListBox1.MultiSelect = 2
ListBox2.MultiSelect = 2
Dim RowsNumber As Integer
Dim RowsNumberOnly As Integer
Dim i As Integer
Dim Customers As Long
Dim customersloop As Long
Dim test As String
RowsNumber = FunctionCount.calculateRows
RowsNumberOnly = FunctionCount.calculateRowsValue
ListBox1.ColumnCount = 3
ListBox1.ColumnWidths = "50;50;50"
ListBox2.ColumnCount = 3
ListBox2.ColumnWidths = "50;50;50"
Customers = 10
i = 0
For customersloop = Customers To RowsNumber
ListBox1.AddItem
ListBox1.List(i, 0) = Sheets("Test").Range("J" & customersloop).Value
ListBox1.List(i, 1) = Sheets("Test").Range("K" & customersloop).Value
ListBox1.List(i, 2) = Sheets("Test").Range("L" & customersloop).Value
i = i + 1
Next
End Sub
After that the selected item need to be transferred to the other listbox2
This is my code:
Private Sub SelectItems_btn_Click()
Dim SelectedItems As String
Dim i As Integer
Dim ListBox2i As Integer
ListBox2i = 0
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
Me.ListBox2.Additem
Me.ListBox2.List(ListBox2i, 0) = ListBox1.List(i, 0).Value
Me.ListBox2.List(ListBox2i, 1) = ListBox1.List(i, 1).Value
Me.ListBox2.List(ListBox2i, 2) = ListBox1.List(i, 2).Value
ListBox2i = ListBox2i + 1
End If
Next
End Sub
I hope that you could help. I always get the error message that am object is missing.
Best regards
Matthias
If you want to move items from list1 to another list2 you can use the columns property from vba.
this is the solution code after I added the values to my first list1 with 3 columns.
Private Sub SelectItems_Click()
Dim i As Integer
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
ListBox2.AddItem
ListBox2.Column(0, (ListBox2.ListCount - 1)) = ListBox1.Column(0, i)
ListBox2.Column(1, (ListBox2.ListCount - 1)) = ListBox1.Column(1, i)
ListBox2.Column(2, (ListBox2.ListCount - 1)) = ListBox1.Column(2, i)
End If
Next
End Sub
Best regards