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
Related
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
I have working code to hide/unhide rows depending on the corresponding cell value.
This is a list of materials and there is a 'finalize' button. You press the button and any row where quantity = 0 should be hidden.
There are 400+ lines and I can see the lines disappear. It is processing roughly 20 lines per second which makes it over 20 seconds to do the list. The list will double every few months.
Is there another method that will hide the lines faster?
Hide:
Public Sub HideRows()
Dim cell As Range
For Each cell In ActiveSheet.Range("H18:H469")
cell.EntireRow.Hidden = (cell.Value = 0 And cell.Value <> "")
Next cell
End Sub
Unhide:
Public Sub UnhideRows()
Dim cell As Range
For Each cell In ActiveSheet.Range("H18:H469")
If (cell.Value = 0 And cell.Value <> "") Then cell.EntireRow.Hidden = False
Next cell
End Sub
I was just typing up as appeared in comments. Use Union to gather qualifying ranges and hide in one go. I am not sure why you are doing a double test. Wouldn't = 0 be sufficient? Or as #Marcucciby2 queries, did you intend an Or?
And as mentioned in other answer, you can do some optimization by switching of things like ScreenUpdating, pageBreaks and switch to manual calculation mode.
If possible, get rid of that ActiveSheet reference and use the actual workbook and worksheet references.
Option Explicit
Public Sub HideRows()
Dim cell As Range, unionRng As Range
For Each cell In ActiveSheet.Range("H18:H469")
If cell.Value = 0 Then
If Not unionRng Is Nothing Then
Set unionRng = Union(unionRng, cell)
Else
Set unionRng = cell
End If
End If
Next
If Not unionRng Is Nothing Then unionRng.EntireRow.Hidden = True
End Sub
Turn off screen updating and manual calculations at the start of your code. Be sure to turn if back on at the end of your code.
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'...your code...
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
I need to create an Excel document with updating totals in the A column, based on numbers entered in the B column. Each respective cell in row A should update based on its equivalent B cell value whenever a new value is added, and then the value entered into B is cleared once added to A.
I have gotten things working for one single row but don't have knowledge or understanding on how to best make this work for EACH cell pair in the entire column. I really don't want to copy and paste this 10,000 times and update the cells to reference the correct pair. Code for single cell:
Private bIgnoreEvent As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If bIgnoreEvent Then Exit Sub
bIgnoreEvent = True
Cells(1, 2) = Cells(1, 2) + Cells(1, 1)
Cells(1, 1) = ""
bIgnoreEvent = False
End Sub
I am hoping this can be achieved with a loop function, or a range of some sort.
This should work:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, c As Range
'any updates in ColB?
Set rng = Application.Intersect(Target, Me.Columns(2))
If Not rng Is Nothing Then
Application.EnableEvents = False '<< prevent re-triggering of event
'Process each updated cell
For Each c In rng
If IsNumeric(c.Value) Then
With c.Offset(0, -1)
.Value = .Value + c.Value
End With
c.ClearContents
End If
Next c
Application.EnableEvents = True '<< re-enable event
End If
End Sub
I have VBA code that works, but does not seem to be optimal. The code should change the colour of the text in the relevant cell in columns H & I when a cell in Column N is clicked.
For example, when cell N5 is clicked, the text in cells H5 and I5 should turn white. When it is unclicked, they return to their normal colour.
The code does not seem to be optimal because the change in column I lags behind that in column H.
I would like a way to make both change instantaneously.
(Bonus points if you can make the cells change colour AND turn into bold instaneously, with the constraint that like for colour, the bold disappears once the cell is unselected).
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim r As Range
Intersect(Columns("H"), ActiveSheet.UsedRange).Font.Color = vbBlack
Set r = Intersect(Range("N:N"), Target)
If r Is Nothing Then Exit Sub
Cells(r.Row, "H").Font.Color = vbWhite
Intersect(Columns("I"), ActiveSheet.UsedRange).Font.Color = vbBlack
Set r = Intersect(Range("N:N"), Target)
If r Is Nothing Then Exit Sub
Cells(r.Row, "I").Font.Color = vbWhite
End Sub
Please note, this is my first time writing VBA, hence any amateurismes.
You don't need to address each column separately...
EDIT: added bold and multiple cells
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim r As Range, c As Range
Set r = Intersect(Me.Range("N:N"), Target)
'? what to do if user selects multiple cells ?
'E.g. - exit if user has >100 cells selected (possibly whole column)
If r Is Nothing Or Target.Cells.CountLarge > 100 Then Exit Sub
Application.ScreenUpdating = False
HighlightIt Application.Intersect(Me.Range("H:I"), Me.UsedRange), False
For Each c In r.Cells
HighlightIt Me.Cells(c.Row, "H").Resize(1, 2)
Next c
Application.ScreenUpdating = False
End Sub
'utility sub for highlighting/unhighlighting
Sub HighlightIt(rng As Range, Optional hilite As Boolean = True)
With rng
.Font.Color = IIf(hilite, vbWhite, vbBlack)
.Font.Bold = hilite
End With
End Sub
Always worth thinking about what should happen if the user selects multiple cells (or even a whole column). Handling this robustly is sometimes a challenge, depending on what you want to happen when they do that.
I am developing a sheet for work. When staff complete a row either myself or my manager need to be able to put our names in column J in order to lock the respective rows to prevent further editing. Is there a way of doing this?
First Unlock EVERY cell in the sheet. Then Protect the Sheet.
Next add this code to the Worksheet Events Module:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim C As Range
If Target.Column = 4 Then 'IF Changed cell is in Column D
For Each C In Target.Cells
If C.Value = "YourName" Or C.Value = "YourManagersName" Then
ActiveSheet.Unprotect
C.EntireRow.Locked = True
ActiveSheet.Protect
End If
Next C
End If
End Sub
Now if one of the 2 names is entered in the specific column then it will Lock that row.
If you want a Password use:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim C As Range
If Target.Column = 4 Then 'IF Changed cell is in Column D
For Each C In Target.Cells
If C.Value = "YourName" Or C.Value = "YourManagersName" Then
ActiveSheet.Unprotect "Password"
C.EntireRow.Locked = True
ActiveSheet.Protect "Password"
End If
Next C
End If
End Sub
NOTE: This will also allow you to lock multiple rows in one shot.