I wanted to make a checkbox, calling a macro that hides and unhides columns on Excel worksheet with specific value in cell, but it is not working
I tried the following VBA script
Sub Hide_Forecasts()
Dim c As Range
For Each c In Range("E12:CF12").Cells
If c.Value = "Forecast" Then
c.EntireColumn.Hidden = True
End If
Next c
End Sub
Sub Unhide_Forecasts()
Dim c As Range
For Each c In Range("E12:CF12").Cells
If c.Value = "Forecast" Then
c.EntireColumn.Hidden = False
End If
Next c
End Sub
Sub CheckBox_For()
If CheckBox1.Value = True Then
Call Hide_Forecasts
Else
Call Unhide_Forecasts
End If
End Sub
Please help me out
You haven't said what type of checkbox you're using - Form Control or ActiveX Control.
For an ActiveX Control right-click the control and select View Code.
Use this code that sits behind the worksheet (CheckBox1 will be named after your checkbox).
Private Sub CheckBox1_Click()
Dim Cell As Range
For Each Cell In Me.Range("E12:CF12")
If Cell.Value = "Forecast" Then
'Checkbox returns TRUE/FALSE - Hidden takes TRUE/FALSE so just connect the two up.
Cell.EntireColumn.Hidden = Me.CheckBox1.Value
End If
Next Cell
End Sub
For a Form Control right-click the control and select Assign Macro.
Place this code in a normal module.
Sheet1 is the codename for the sheet (that's the name not in brackets in the Project Explorer).
'Form Control can have three values:
' 1 = Checked
' -4146 = Unchecked
' 2 = Mixed - ignoring that this value may occur.
Public Sub Checkbox_For()
Dim ChkValue As Boolean
'Is value different from -4146? Returns TRUE = Checked or Mixed / FALSE = Unchecked
ChkValue = Sheet1.Shapes("Check Box 1").OLEFormat.Object.Value <> -4146
Dim Cell As Range
For Each Cell In Sheet1.Range("E12:CF12")
If Cell.Value = "Forecast" Then
Cell.EntireColumn.Hidden = ChkValue
End If
Next Cell
End Sub
Related
I need some guidance how to create a selection for a excel userform that when the user selects CB1 which may be in A2, that it will also select the next cell B2 for the populated field and also select the next cell in C2 for the next populated field. I am not sure if my first selection should be a CB and the next fields maybe a list box or text field.
current code is:
Private Sub Userform_Initialize()
ComboBox_Combox5.List=Sheets("Sheet1").Range("A1:A650").Value
End Sub
Add two text boxes next to your combo-box.
I've left them with the default names of TextBox1 and TextBox2.
Code behind the form is:
Option Explicit
Private Sub Userform_Initialize()
ComboBox_Combox5.List = Sheets("Sheet1").Range("A1:A650").Value
End Sub
Private Sub ComboBox_Combox5_Change()
'Reference to selected value in column A.
Dim rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Cells(Me.ComboBox_Combox5.ListIndex + 1, 1)
Me.TextBox1 = rng.Offset(, 1)
Me.TextBox2 = rng.Offset(, 2)
End Sub
Always have Option Explicit at the top of your module.
I have populated a column range cells into a UserForm ListBox (below is the code bulk). Now I want to create a TextBox in the same Form to dynamically filter the contents of that ListBox as I type my entry. How can I utilize the AutoFilter method (or other solution) to call the ListBox content? Thanks for your help.
Set rSource = Sheets("Property").Range(Range("B5"), Range("B5").End(xlDown))
ListBox1.List = rSource.Cells.Value
May by something like this help you:
When TextBox is changed it clearing Listbox1 and check which value from list contain that string if yes then macro add it to ListBox1
Private Sub TextBox1_Change()
Dim cell As Range
Set rSource = Sheets("Property").Range(Range("B5"), Range("B5").End(xlDown))
ListBox1.Clear
For Each cell In rSource
If InStr(cell.Value, TextBox1.Text) <> 0 Then
ListBox1.AddItem cell.Value
End If
Next cell
End Sub
Private Sub UserForm_Activate()
Set rSource = Sheets("Property").Range(Range("B5"), Range("B5").End(xlDown))
ListBox1.List = rSource.Cells.Value
End Sub
I have a slicer (based on a table), I would like to create a macro in order that when I select any single value in my slicer, the macro selects and copies automatically the second visible cell in column D (for example if I select the value X13 in my slicer, I want that my macro selects and copies automatically the second visible cell in column D). The part of my VBA code to select and copy the second visible cell in column D works perfectly but it does not work when I select a single value in my slicer (I assigned the macro related to my slicer). I do not know which line of VBA code that I need to add add for my macro works when I select any single value in my slicer. Please find my VBA code below.
Sub NextVisiblecellassignedtomyslicer()
Dim r As Range
Set r = Range("D1")
For i = 1 To Rows.Count
Set r = r.Offset(1, 0)
If r.EntireRow.Hidden = False Then
r.Copy
Exit Sub
End If
Next
End Sub
I finally managed to find the solution to my problem by myself:
Sub NextVisiblecellassignedtomyslicer()
Dim slr As Slicer
Dim si As SlicerItem
Dim i As Long
Dim it As Object
Set sc = ActiveWorkbook.SlicerCaches("Slicer_WBS_element")
With sc
For Each it In .SlicerItems
If it.Selected = True Then
call nextvisiblecellV10
End If
Next it
End With
End Sub
Sub nextvisiblecellV10()
Dim r As Range
Set r = Range("D1")
For i = 1 To Rows.Count
Set r = r.Offset(1, 0)
If r.EntireRow.Hidden = False Then
r.Copy
Exit Sub
End If
Next
End Sub
The following VBA script for an Excel sheet allows an individual to type text into a cell and then automatically jump to the next cell in the array once they hit enter.
However, this requires someone to type into every cell for the script to advance to the next cell in the array below and does not recognize tab, only enter.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim tabArray As Variant
Dim i As Long
tabArray = Array("B5", "C6", "D7", "E8")
Application.ScreenUpdating = False
For i = LBound(tabArray) To UBound(tabArray)
If tabArray(i) = Target.Address(0, 0) Then
If i = UBound(tabArray) Then
Me.Range(tabArray(LBound(tabArray))).Select
Else
Me.Range(tabArray(i + 1)).Select
End If
End If
Next i
Application.ScreenUpdating = True
End Sub
I'd like to let the person hit tab or enter if they want to skip the given field and advance without typing anything in the cell.
You can also just protect the sheet and have the input cells unlocked.
Right Click on the Cell, choose "Format cells".
Then, go to Protection Tab, and uncheck "Locked".
Once the input cells are unlocked, go to the "Review" Ribbon, and click on Protect Sheet. Uncheck "Select locked cells" and it should only allow you to click on the cells that are unlocked, and can press Tab repeatedly to go to the next available cell.
Protecting the sheet and unlocking the data-entry cells as suggested by #Basher is probably a better solution IMO, but this sort-of works. Couple of notes:
To "break into" the tab sequence you need to select one of the data-entry cells
To "break out" of the tab sequence (so you can select some other cell maybe) you can make a multi-cell selection.
Code:
Dim lastCellAddress As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim tabArray As Variant
Dim i As Long, addr, mCurr, indx, mPrev
If Target.Cells.CountLarge = 1 Then
tabArray = Array("B5", "C6", "D7", "E8")
addr = Target.Address(False, False)
mCurr = Application.Match(addr, tabArray, 0)
If IsError(mCurr) Then 'current selection isn't a data entry cell
If lastCellAddress <> "" Then
'was user previously in a data entry cell?
mPrev = Application.Match(lastCellAddress, tabArray, 0)
If Not IsError(mPrev) Then
'mPrev is 1-based but tabArray is 0-based...
indx = IIf((mPrev - 1) < UBound(tabArray), mPrev, 0)
On Error GoTo haveError
Application.EnableEvents = False
Me.Range(tabArray(indx)).Select '<< select the next entry cell
Application.EnableEvents = True
End If
End If
End If
lastCellAddress = Selection.Address(False, False)
Else
lastCellAddress = "" 'breaks out of the sequence
End If
Exit Sub
haveError:
Debug.Print Err.Description
Application.EnableEvents = True
End Sub
You don't need VBA to achieve this task - it's a built-in Excel functionality.
Simply select your data entry range and after inputting the desired value into the first cell hit TAB, it will take you to the next highlighted cell - either to the right of the current one or to the next row if you were at the end of the row.
I wrote a macro that hides row where we have zero cells, but I want to add one more code that will unhide it too. Hide and unhide together. The code is below:
Sub HideRows()
Dim cell As Range
For Each cell In Range("U9:U149")
If Not IsEmpty(cell) Then
If cell.Value = 0 Then
cell.EntireRow.Hidden = True
End If
End If
Next
End Sub
If you mean you want the same code to toggle the rows between hidden / visible, then change it to:
Sub ToggleHideRows()
Dim c As Range
For Each c In Range("U9:U149")
If Not IsEmpty(c) And c.Value = 0 Then
c.EntireRow.Hidden = Not c.EntireRow.Hidden
End If
Next
End Sub
I've changed your variable name from cell to c - it's a bad idea to used 'special' words as variable names.
Do you want to hide rows with 0 and show rows without 0?
Sub HideRows()
Dim c As Range
For Each c In Range("U9:U149")
If Not IsEmpty(c) Then
c.EntireRow.Hidden = (c.Value = 0)
End If
Next
End Sub