I have some very basic VBA code here
sub HideRows_Based_On_Values
For Each cell in Range (C12:AG37)
if cell.value = "SD" or "SA" or "SN" then cell.entirerow.hidden = false
else cell.entirerow.hidden = true
next cell
End Sub
The range is correct, the inputs in the cells are correct.
For some reason, excel is hiding some (not all) of the rows that have at least one of these values present in them. I can't figure out what I'm doing wrong.
I expected for the code to filter out any rows that don't have one of SD, SA, or SN present - but it's only working on some of the rows and not all.
Not:
A = 1 OR 2 OR 3
But:
(A = 1) OR (A = 2) OR (A = 3)
This is true in every computer program, computer programming language, ...
I've been testing until it worked and this is what I came up with:
Sub HideRows_Based_On_Values()
For Each cell In Range("C12:AG37")
If ((cell.Value = "SD") Or (cell.Value = "SA") Or (cell.Value = "SN")) Then
cell.EntireRow.Hidden = False
Else: cell.EntireRow.Hidden = True
End If
Next cell
End Sub
The weird part is that, next to the range where you need double quotes (Range("C12:AG37")), you need to put the action of the Then on the next line, otherwise it won't work.
Before:
After:
Edit:
Hereby the solution, as provided by the author of the question:
Sub HideRows_Based_On_Values()
For Each Row In Range("C12:AG37").Rows
If Application.CountIf(Row, "SD") + Application.CountIf(Row, "SA") + Application.CountIf(Row, "SN") = 0 Then
Row.Hidden = True
Else Row.Hidden = False
End If
Next Row
End Sub
sub HideRows_Based_On_Values
For Each cell in Range (C12:AG37)
if cell.value = "SD" OR cell.value = "SA" OR cell.value = "SN" then
cell.entirerow.hidden = false
else
cell.entirerow.hidden = true
end if
next cell
End Sub
Related
I am trying to divide all the cells in a selection by 1000 but not cells which have a sum formula.
Criteria:
Cell must have a number.
Cell should not contain the Sum() formula (any other formula is okay).
If both of these criteria are met then divide the cell value by 1000.
For Each cell In Selection.Cells
If IsNumeric(cell) = False Or cell.Address = Left(ActiveCell.Formula, 5) = "=Sum(" Or cell.Address = Left(ActiveCell.Formula, 6) = "=+SUM(" Or cell.Address = Left(ActiveCell.Formula, 6) = "=-SUM(" Then
MsgBox ("Selection either does contain numbers or has only sum formulae")
Else
cell.value = cell.value / 1000
End If
Next
Not sure what you're trying to achieve with
Cell.Address = Left(ActiveCell.Formula,5) = "=Sum(
You can only compare one value with another.
I'm sure the more advanced people here will frown on the way I phrased the if statement but it does the job:
Sub divideBy1000()
Dim formCheck As Variant
For Each ccell In Selection.Cells
Debug.Print (ccell.Formula)
Debug.Print (ccell.Value)
formCheck = InStr(ccell.Formula, "SUM(") 'This checks if SUM is in the formula and returns a value bigger than 0 if so (where it occurs in the string).
If len(trim(ccell.value2))>0 Then
If IsNumeric(ccell.Value) And (formCheck = Null Or formCheck = 0) Then
'You have to use AND so none of the SUM formulas get through, no else needed this way
If Left$(ccell.Formula, 1) = "=" Then 'EDIT to keep your formula
ccell.Formula = "=(" & Right$(ccell.Formula, Len(ccell.Formula) - 1) & ")/1000"
Else
ccell.Value = ccell.Value / 1000
End If
End If
End If
Next ccell
End Sub
Edit:
Added a catch for empty cells so they don't turn to 0 and added Instr to check for the SUM as suggested by #Marcucciboy2
Good afternoon,
I have another problem related to IF statement.
I have the following code:
Sub BOMNewRow()
If Range("D38").Value <> "" Or Range("C38") <> "x" Or Range("C38") <> "0" Then
'Inserting new Core Drill row + formula autofill from the previous one
Range("A38").EntireRow.Insert
Range("A38").Value = "First Core drill into building or existing chamber (per event)"
Range("B38").Value = "Sitec"
If Range("D39").Value Like "*2*" Or Range("C39").Value = 2 Then
Range("C38").Value = 2
Else
Range("C38").Value = 1
End If
Range("E38").Value = 100.39
Range("F37").Select
Selection.AutoFill Destination:=Range("F37:F38"), Type:=xlFillDefault
'customizing new row
With Range("A38:B38,D38:F38")
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeBottom).Weight = xlThin
End With
With Range("C38")
.Borders(xlEdgeBottom).Weight = xlMedium
End With
Else
Call AuditCheck 'go straight away to the BoM audit control
'Taking a look on one of the audit cell
End If
If Range("C39").Value <> "x" Or Range("C39").Value <> "0" Then
Range("C39").Value = "x"
Range("D38").Value = Range("D39")
Call New_version2
Sheets("BoM").Activate
Else
Call AuditCheck
End If
Range("A43").Select
End Sub
What I intend to do is:
insert the new row 38 and customize it - this is done by my first IF statement.
run the second IF statement based on the cell C39, which doesn't work.
If previously I had the X value in the cell C38, now when I inserted a new row the second IF statement still executes the process for value C39, which is X.
.
It looks like I don't understand the if statement order within the macro.
How can I solve this problem?
Hey I have been writing some code to add a part ID to a spreadsheet off of a user form in Excel VBA. I have been reading through different documentation and can not figure out why no matter what type of method of inserting a row I try it inserts a row with a repeating value instead of a blank one. If anyone knows how to specify blank, other than writing the whole row to blank and then writing my numbers I want after, that would be appreciated.
I have tried both the following lines to add a row
Cells (x+1 ,column).EntireRow.Insert Shift:= xlDown
ws1.Rows(x+1).Insert Shift:=xlDown
This is the function it is used in:
Public Sub Add(IDRange As Range)
SearchCell = Cells(x, IDRange.Column)
Cells(x, IDRange.Column).Select
Do
If SearchCell = PartID Then
MsgBox " this Company Already uses this part"
Exit Sub
ElseIf x <> StopRow Then
x = x + 1
SearchCell = Cells(x, IDRange.Column)
End If
Loop While x <> StopRow And SearchCell <> PartID
Cells(x + 1, IDRange.Column).EntireRow.Insert Shift:=xlDown
Cells(x, IDRange.Column).Value = PartID
MsgBox PartID & " has been added to Adress " & Cells(x, IDRange.Column).Address
Cells(x, IDRange.Column).Select
End Sub
Bellow is the function that calls the Add Function and where I belive it may be getting the company name from
Private Sub AddPart_Click()
AddPartCounter = 0
Company = UserForm1.CompanyBox.Value
PartID = UserForm1.PartBox.Value
If Company = "" Then
MsgBox " Please put in the company you would like the part to go under"
ElseIf PartID = "" Then
MsgBox " Please put in the Part you would like entered"
ElseIf UserForm1.Studs.Value = False And UserForm1.Spreaders.Value = False And UserForm1.Blocks.Value = False And UserForm1.Imma.Value = False Then
MsgBox "Please select the type of part you are trying to add"
Else
Dim CurrentCell
Set CurrentCell = Cells.Find(What:=Company, LookAt:=xlWhole)
If CurrentCell Is Nothing Then
MsgBox " Company Not Found "
Exit Sub
End If
x = CurrentCell.Row
Do
Set CurrentCell = CurrentCell.Offset(1, 0)
Loop While CurrentCell.Offset(1, 0) = "" And Not CurrentCell Is Nothing And CurrentCell.Offset(1, 0).Row <> thisvar.Row + 1
StopRow = CurrentCell.Row
'If they are trying to add a nut
If UserForm1.Imma.Value = True Then
Call Add(Nut_ID_Rng)
'IF they are trying to add a stud
ElseIf UserForm1.Studs.Value = True Then
Call Add(Stud_ID_Rng)
'If they are trying to add a block
ElseIf UserForm1.Blocks.Value = True Then
Call Add(Block_ID_Rng)
'If they are trying to add a spreader
ElseIf UserForm1.Spreaders.Value = True Then
Call Add(Spreader_ID_Rng)
End If
End If
AddPartCounter = 1
End Sub
I know that the repeating pattern is coming from the insert line through debugging but I can not figure out why I have tried changing variables to numbers and it still did the same thing. This what it looks like with the repeating values.
enter image description here
The problem is that you most likely have a value still stored in your clipboard when you execute the Macro. To fix that, simply add this line of dode before running the insert line:
Applcation.CutCopyMode = False
That will clear your clipboard and allow the inserted rows to be blank.
I am looking to hide all rows (until row 150), that contain a certain text in a certain column. The column contains a drop down of two choices, "Yes" and "No". If the answer is yes, I want to hide all the rows below, if it is no, then not hide.
ie, C2 is "No", don't hide. C3 is "Yes", hide...There are 150 rows of Yes or no, but once yes, you can hide all the rows.
I've tried to cycle the code below and it works with the first cell, but all rows after that do not work
Option Explicit
Private Sub HideRows(ByVal Target As Range)
If Range("C2").Value = "Yes - provide details" Then
Rows("3:150").EntireRow.Hidden = True
ElseIf Range("C2").Value = "No" Then
Rows("3:150").EntireRow.Hidden = False
End If
If Range("C3").Value = "Yes - provide details" Then
Rows("4:150").EntireRow.Hidden = True
ElseIf Range("C3").Value = "No" Then
Rows("4:150").EntireRow.Hidden = False
End If
' all the way through to C149
If Range("C149").Value = "Yes - provide details" Then
Rows("150").EntireRow.Hidden = True
ElseIf Range("C149").Value = "No" Then
Rows("150").EntireRow.Hidden = False
End If
End Sub
I expected to be able to cycle through the first If code, but it doesn't work after the 1st set of them
(this is untested)
Rows("3:150").EntireRow.Hidden = False
For i = 2 to 150
If Range("C" & i).Value = "Yes - provide details" Then
Rows(i + 1 & ":150").EntireRow.Hidden = True
Exit For
End If
Next i
Since it looks like you don’t want to filter because you want all rows below a “yes” to be hidden. I would recommend doing a loop.
Option Explicit
Sub HideRows()
Dim currRow as Integer: currRow = 1
Dim continue as Boolean: continue = True
While continue
If cells(currRow,3) = "Yes - provide details" then
rows(currRow & ":150").EntireRow.Hidden = true
continue = False
Else
currRow = currRow + 1
End If
Wend
End Sub
This is untested from mobile.
I have 80 rows where the user can enter a predetermined value under column Ward. This unhides a button next to it. Upon clicking it, it empties the adjacent value and increments (+1) a particular cell in another sheet depending on the original value.
Currently, I have 80 ActiveX buttons next to the Ward cells that hides/unhides depending on the value of the Ward cells. I've noticed that adding more buttons slows down the spreadsheet because of the sheer volume of If Then statements I have.
If Range("F8").Value = 0 Then
Sheets("Admissions").EDAdmit1.Visible = False
Else
Sheets("Admissions").EDAdmit1.Visible = True
End If
If Range("L8").Value = 0 Then
Sheets("Admissions").ElecAdmit1.Visible = False
Else
Sheets("Admissions").ElecAdmit1.Visible = True
End If
If Range("F9").Value = 0 Then
Sheets("Admissions").EDAdmit2.Visible = False
Else
Sheets("Admissions").EDAdmit2.Visible = True
End If
If Range("L9").Value = 0 Then
Sheets("Admissions").ElecAdmit2.Visible = False
Else
Sheets("Admissions").ElecAdmit2.Visible = True
End If
.. and so on.
Not to mention the If Then statements I have for every button click.
Private Sub EDAdmit1_Click()
If Range("F8") = "ICU" Then
Worksheets("Overview").Range("AD11").Value = Worksheets("Overview").Range("AD11") + 1
ElseIf Range("F8") = "HDU" Then
Worksheets("Overview").Range("AF11").Value = Worksheets("Overview").Range("AF11") + 1
ElseIf Range("F8") = "DPU" Or Range("F8") = "Other" Then
Else
Col = WorksheetFunction.VLookup(Range("F8"), Range("U1:V27"), 2)
Worksheets("Overview").Range(Col).Value = Worksheets("Overview").Range(Col).Value + 1
End If
Range("F8").ClearContents
End Sub
Is there a more efficient way of doing this?
Admission List:
You could consider using "admit" hyperlinks in the cells next to the Ward selections: that way you only need one handler (Worksheet_FollowHyperlink in the worksheet module). Note you need to use Insert >> Hyperlink and not the HYPERLINK() formula-type links here (because formula-based links don't trigger the FollowHyperlink event).
You can ditch the hide/show code and instead use conditional formatting to change the link font color to hide the links when there's no Ward selected. If a user clicks on one of the hidden links then you can just do nothing.
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim rngSrc As Range, addr, ward
Set rngSrc = Target.Range '<< the cell with the link
ward = rngSrc.Offset(0, 1).Value '<< cell with Ward
'only do anything if a ward is selected
If Len(ward) > 0 Then
'find the cell to update
Select Case ward
Case "ICU"
addr = "AD11"
Case "HDU"
addr = "AF11"
Case "DPU", "Other"
addr = ""
Case Else
addr = Application.VLookup(ward, Me.Range("U1:V27"), 2, False)
End Select
'if we have a cell to update then
If Len(addr) > 0 Then
With Worksheets("Overview").Range(addr)
.Value = .Value + 1
End With
End If
rngSrc.Offset(0, 1).ClearContents
End If
rngSrc.Select '<< select the clicked-on link cell
' (in case the link points elsewhere)
End Sub
At the beginning of your code put this line:
Application.ScreenUpdating = False
this will disable all screen updates. Let your code do changes, and then enable screen updating, and all your changes will appear.
Application.ScreenUpdating = True
Disabling screen updating usually makes the execution of code faster.