Hide multiple row if their requirement is met - excel

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

Related

Delete Rows in excel using VBA

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

Order sheets based on input in Excel spreadsheet

In the Excel spreadsheet the user can define the order of the sheets within Column A:
A B
1 Sheet3
2 Sheet4
3 Sheet1
4 Sheet2
5
Once the user enterred the order in Column A he/she can click a button which is linked to this VBA code:
Sub Move()
Sheets(Sheet1.Range("A2").Value).Move After:=Sheets(Sheet1.Range("A1").Value)
Sheets(Sheet1.Range("A3").Value).Move After:=Sheets(Sheet1.Range("A2").Value)
Sheets(Sheet1.Range("A4").Value).Move After:=Sheets(Sheet1.Range("A3").Value)
End Sub
This VBA puts the sheets in the order based on the inputs from the user in Column A. All this works fine so far
Now I have the issue that the number of sheets varies so it can happen that instead of only 4 sheets there will be 8 or 10 or 15 and so on. In this case it would be necessary to add all those sheets manually to the VBA code.
Is it possible to make the VBA code more dynamically. Something like an array for the values in Column A and a VBA like this:
Sub Move()
MoveSheets based on Array {Sheet1.Range("A1:A5")}
End Sub
I think this will work if your sheet names are on "Sheet1" in B4 going down.
Sub x()
Dim n As Long, i As Long, r As Range
Application.ScreenUpdating = False
With Sheets("Sheet1")
Set r = .Range("B4", .Range("B" & Rows.Count).End(xlUp))
For n = r.Count To 1 Step -1
Sheets(r.Cells(n).Value).Move after:=Sheets(Sheets.Count - i)
i = i + 1
Next n
Application.Goto .Range("A1")
End With
Application.ScreenUpdating = True
End Sub

Excel Function - Set Cell Color based on Another Cell Selection

I need to write a function to color a bunch of cells in one row based on the colors of cells in another row. I've tried some different VBA variations, but this is what I am wanting to do:
Public Function CopyColorFormat(Target As Range)
If Not Target.Interior.Color Is Nothing Then
ActiveCell.Interior.ThemeColor = Target.Interior.ThemeColor
End If
End Function
I then went to A1 and set the formula to =CopyColorFormat(C1). (C1 is Purple and I want A1 to also be shaded Purple.) However, this results in a #VALUE! error in the cell I try to put the function in.
The reason I am wanting this and not to use conditional formatting is that I need to apply this to a ton of cells (via their corresponding cell) and dont want to make a rule for each one.
I don't know much about functions, but this macro should do the trick for you.
Sub color_cells()
Application.ScreenUpdating = False
Dim currentcell As Range
Dim copycell As Range
Dim current As Long
Dim copy As Long
Set currentcell = Range("A1")
Set copycell = Range("C1")
current = 1
copy = 1
For x = 1 To 8 ' instead of 8 - enter the number of rows you want the code to run on.
If Not copycell.Interior.ColorIndex = xlNone Then
copycell.copy
currentcell.PasteSpecial xlPasteFormats
End If
current = current + 1
copy = copy + 1
Set currentcell = Range("A" & current)
Set copycell = Range("C" & copy)
Next x
Application.ScreenUpdating = True
End Sub
You might want to specify which workbook and worksheet the code runs on if you work with multiple at the same time.

Hide rows based on cells contaning all zeroes or no values

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

Change a range of cells on one or more sheets based on a selected range of cells in a main sheet

I'd like to preface this by saying that I'm largely inexperienced with coding. I've been working on a project at work on my own, and finally ran into a wall that I can't solve by simple googling.
To explain briefly with pertinent details, I have a workbook with a varying number of worksheets that each have a specific cell for text (explained later), and a checkbox.
My goal is to select a range of cells, usually 1-5 in a row, and then have a button to change the color of the same selected cells of all sheets given a specific criteria. The criteria being that only the sheets that have "Office" listed in the aforementioned cell, and the check box un-checked, will be changed.
While I have no issue having Excel cycle through the sheets and do something only on the sheets that meet the criteria, the problem comes with trying to edit the same cells as are selected on the main sheet.
The coding I have come up with so far:
Dim cell As Range
Dim n As Integer
Set cell = Selection
If Range("AN6").Text = "Office" Then
For n = 1 To Sheets.Count - 2
If Sheets(n).Range("AN6").Text = "Office" And Sheets(n).CheckBox1.value = False Then
For Each cell In Selection
Sheets(n).Range(cell).Interior.ColorIndex = 56
Next cell
End If
Next n
End If
The error I am getting is: Application-defined or object-defined error.
Hopefully there is a way to execute this. I'd greatly appreciate any help I can get.
You can use the .Address property of a range. I'm pretty sure you're getting an error because your selection is in one sheet, and you try to refer to that range in another sheet. Using just the address of the range should fix that issue.
Dim cell As Range
Dim n As Integer
Set cell = Selection
If Range("AN6").Text = "Office" Then
For n = 1 To Sheets.Count - 2
If Sheets(n).Range("AN6").Text = "Office" And Sheets(n).CheckBox1.value = False Then
For Each cell In Selection
Sheets(n).Range(cell.Address(False,False)).Interior.ColorIndex = 56
Next cell
End If
Next n
End If
The only testing I did on this code was using this sub. I selected a range on my worksheet and ran the macro and the msg box said Q12:U26
Sub test()
Dim cell As Range
Set cell = Selection
MsgBox cell.Address(False, False)
End Sub
This should work:
Sub test()
With Selection
startRow = .Cells(1).Row
startColumn = .Cells(1).Column
endRow = .Cells(.Cells.Count).Row
endColumn = .Cells(.Cells.Count).Column
End With
If Range("AN6").Text = "Office" Then
For n = 1 To Sheets.Count - 2
If Sheets(n).Range("AN6").Text = "Office" And Sheets(n).CheckBox1.value = False Then
For Each cell In Range(Sheets(n).Cells(startRow, startColumn), Sheets(n).Cells(endRow, endColumn))
cell.Interior.ColorIndex = 56
Next cell
End If
Next n
End If
End Sub

Resources