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
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 am new to coding in anything, this project is the first time I have coded. I am trying to hide multiple row based on individual requirement. The requirement is if in a specific cell of the same row there is a space or is empty, the row will be hidden, if it is hidden and there is anything else, the row will be shown. The code need to work on specific worksheet as I have multiple worksheet where there is row to hide or columns to hide at different place.
There are 2 different pieces of code that I tried which don't work.
This picture represent the Excel sheet I am currently trying to hide row:
My goal is to hide row between 8 to 37 if there is there is a space or if it is empty, depending what the code inside the cell point at for the cell A8 to A37. if I activate the code, in the image only the row 8, 9 and 10 should be visible, 11 to 37 should be hidden.
So far I have tried these two pieces of code:
Sub C1()
Set ws = ActiveWorkbook.Worksheets("FR-3-06_Jeux Prod.")
Dim C As range
For Each C In range("A8:A37")
If C.Value = " " Then
C.EntireRow.Hidden = True
Else
If C.Value = Empty Then
C.EntireRow.Hidden = True
Else
C.EntireRow.Hidden = False
End If
End If
Next C
End Sub
This code work as intended except that it is not tied to a sheet. "Set ws = ActiveWorkbook.Worksheets("FR-3-06_Jeux Prod.")" is not working as well as a couple other code I tried, they point to an error. So when I try to use this code it will work on the active sheet and not "FR-3-06_Jeux Prod."
Sub Hide_column_and_Row_F_3_6()
Dim NbreLigne As Integer
Dim tableau As range
Set wrkshtDoc = ActiveWorkbook.Worksheets("FR-3-06_Jeux Prod.")
Set tableau = wrkshtDoc.range("A8:A37")
NbreLigne = tableau.Rows.Count
For k = 1 To NbreLigne
If tableau(1, k) = " " Then
tableau(1, k).EntireRow.Hidden = True
ElseIf tableau(1, k) = Empty Then
tableau(1, k).EntireRow.Hidden = True
Else
tableau(1, k).EntireRow.Hidden = False
End If
Next k
End Sub
This code only works as intended when I try to hide columns as in replace "row" in the code with "columns". There is sheet in my file where is it columns I need to hide and since this code is working I tried to reuse it... what it is currently doing is hiding row with "test", line 8 only. It wont hide the empty cell.
what would be the error or what would be needed to hide row with the requirement? I know that code #2 work with columns...
You are almost there with code1, you only need to add:
For each C in ws.Range("A8:A38")
Because you add ws. in front of the Range, it knows which sheet to apply it on.
Good luck!
Hide Blank Rows
Option Explicit
Sub HideBlankRows()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet: Set ws = wb.Worksheets("FR-3-06_Jeux Prod.")
Dim Cell As Range
For Each Cell In ws.Range("A8:A37").Cells
Cell.EntireRow.Hidden _
= IIf(Len(Trim(CStr(Cell.Value))) = 0, True, False)
Next Cell
End Sub
I want to run a code that will go down all of column D (D:D) in my sheet named PxV and if they find a blank cell it will completely delete that corresponding row. Another caveat is idk if my cells are truly blank? There is a formula running through all the cells and if it is false returns "". Idk if this would be recognized as blank by VBA so if you would have to delete cells that have "" even though they're blank. It would also be nice to run continuously so when new data is populates it runs but I am not sure if that is possible . Thanks!!!
See if this works. You'll need to edit the code to match your sheet name and column.
Sub DelRow()
Dim ws As Worksheet
Dim SheetsToSearch, SrchStrg As String, r As Range
Set ws = Sheets("Sheet1")
SrchStrg = ""
SrchStrg2 = 0
Application.ScreenUpdating = False
With ws.Range("A:A")
Set r = .Find(what:=SrchStrg, After:=.Range("A1"))
For Each r In ws.Range("A1:A1000")
If r = SrchStrg Then
r.EntireRow.Delete
ElseIf r = SrchStrg2 Then
r.EntireRow.Delete
End If
Next
End With
Application.ScreenUpdating = True
End Sub
I have a dataset where every row is a General Ledger (GL) account and in each column there is the value for the relevant period.
I would like to hide all GL accounts (rows) where no values (or zero values) are included for all periods (columns).
The code below seems to work for the "No values".
How do I hide all the rows with only zeroes (or all rows with zeroes or "no values"?
If one period has an amount, the row shouldn't be hidden.
Sub hide()
Dim c As Range
For Each c In Range("A1:F6")
If c.Value = "" Then
c.EntireRow.Hidden = True
Else
c.EntireRow.Hidden = False
End If
Next c
End Sub
Furthermore once any amounts change in a row this code should also make the unhidden rows reappear. At this moment it hides a row that has no value, but once this changes, the hidden row doesn't reappear anymore.
See code below if you want to test for both all blanks or all zeros and hide row if either present. Starts with an unhide of all rows.
Sub hide()
Dim wb As Workbook
Dim ws As Worksheet
Dim c As Range
Dim targetRange As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet5")
Set targetRange = ws.Range("A1:F6")
targetRange.EntireRow.Hidden = False
For Each c In targetRange.Rows
If (WorksheetFunction.CountIf(c,"<>0") - WorksheetFunction.CountIf(c,"") = 0) And (WorksheetFunction.CountA(c) - WorksheetFunction.Count(c) = 0) Then
c.EntireRow.Hidden = True
End If
Next c
End Sub
You have to check every row completely before deciding if to hide it or not. Currently, the last cell of every row decided if a row is hidden.
Give the following code a try. It sets a range to all cells of a row and uses the function CountA to count number of cells that are not empty.
Sub hide()
Dim ws As Worksheet, row As Long
Set ws = ActiveSheet
With ws
For row = 1 To 6
Dim myRange As Range
Set myRange = .Range(.Cells(row, 1), .Cells(row, 6))
.Rows(row).EntireRow.Hidden = (Application.WorksheetFunction.CountA(myRange) = 0)
Next row
End With
End Sub
Goal: Efficiently show/hide rows based on the data in the row.
Create a helper column that determines whether or not
a row should be hidden.
Have the formula in the helper
column return an error or a number.
Hide the helper column and write
code to execute the hiding/showing.
Question: Which one of the following methods would you expect to be faster? Column B is the helper column and will always be contiguous.
Sub SetRowVisibility1()
Dim rowsToCheck As Range
With ActiveSheet
Set rowsToCheck = .Range(Range("B7"), Range("B7").End(xlDown))
End With
Dim needToShow As Range, needToShow_Showing As Range
Dim needToHide As Range, needToHide_Showing As Range
Set needToShow = rowsToCheck.SpecialCells(xlCellTypeFormulas, xlNumbers)
Set needToHide = rowsToCheck.SpecialCells(xlCellTypeFormulas, xlErrors)
On Error Resume Next
Set needToShow_Showing = needToShow.Offset(0, 1).SpecialCells(xlCellTypeVisible)
Set needToHide_Showing = needToHide.Offset(0, 1).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not needToHide_Showing Is Nothing Then
needToHide_Showing.EntireRow.Hidden = True
End If
If Not needToShow Is Nothing Then
If needToShow.Count <> needToShow_Showing.Count Then
needToShow.EntireRow.Hidden = False
End If
End If
End Sub
Sub SetRowVisibility2()
Dim rowsToCheck As Range
With ActiveSheet
Set rowsToCheck = .Range(Range("B7"), Range("B7").End(xlDown))
End With
Dim needToShow As Range, needToHide As Range
Dim cell As Range
For Each cell In rowsToCheck
If IsError(cell.Value) And (cell.EntireRow.Hidden = False) Then
If needToHide Is Nothing Then
Set needToHide = cell
Else
Set needToHide = Union(needToHide, cell)
End If
End If
If Not IsError(cell.Value) And (cell.EntireRow.Hidden = True) Then
If needToShow Is Nothing Then
Set needToShow = cell
Else
Set needToShow = Union(needToShow, cell)
End If
End If
Next cell
If Not needToHide Is Nothing Then needToHide.EntireRow.Hidden = True
If Not needToShow Is Nothing Then needToShow.EntireRow.Hidden = False
End Sub
there is a different way and that is to use th auto filter feature - after all VBA has an A in it - use the features of the application wherever possible
so this bit of code is pretty short and sweet - assumes that the data is a contiguous block in columns a and b and assumes no other error handling in play. the resume next line allows for the filter to be already turned on.
Sub showHideRange()
Dim testrange
testrange = Range("A1").CurrentRegion.Address
On Error Resume Next
testrange.AutoFilter
ActiveSheet.Range(testrange).AutoFilter Field:=2, Criteria1:="show"
End Sub
If you do not wish to show the user what's happening, would it not be better to perform the calculation in VBA itself, rather than in a hidden column? Granted, that would seem to lock you into option 2, which I suspect is the slower option ... most of my VBA experience is in older versions of Excel, so I've not had the pleasure of working with some of the newer features, and the tasks I've done that involved processing rows of data were done row-by-row.
I guess one possible issue with the first sub is that if there is a problem with the worksheet or the values you're using to determine hiding/showing, the process will fail. If you check row-by-row and there is a row that causes problems, you could skip over that row and process the other ones correctly.