Hide row in Excel if 3 of the cells are blank - excel

I have a Sheet with columns A through F. I'm looking for the program to run through all the rows (Is there a way for it to only do active rows?) and check if D1 & E1 & F1 are blank, then hide the row (and so on).
Here's what I have which doesn't really work too well....
Sub Celltest2()
Dim rw As Range, cel As Range
Dim i As Integer
Dim celset As Range
For Each rw In Sheets("Phonelist").Range("D2:F5000").Rows
For Each cel In rw.Cells
If Len(cel.Text) = 0 Then
cel.EntireRow.Hidden = True
End If
Next
Next
End Sub

Try the code below:
Sub Celltest2()
Dim rw As Range, cel As Range
Dim i As Integer
Dim celset As Range
Dim LastRow As Long
With Sheets("Phonelist")
' find last row with data in Columns "D, "E" and "F" >> modify to your needs
LastRow = WorksheetFunction.Max(.Cells(.Rows.Count, "D").End(xlUp).Row, _
.Cells(.Rows.Count, "E").End(xlUp).Row, _
.Cells(.Rows.Count, "F").End(xlUp).Row)
For Each rw In .Range("D2:F" & LastRow).Rows
If WorksheetFunction.CountA(Range("D" & rw.Row & ":F" & rw.Row)) = 0 Then
rw.EntireRow.Hidden = True
End If
Next rw
End With
End Sub
Option 2: You can replace the loop above (the one that starts with For Each rw In .Range("D2:F" & LastRow).Rows) with the following loop:
For i = 2 To LastRow
If WorksheetFunction.CountA(Range("D" & i & ":P" & i)) = 0 Then
Rows(i).EntireRow.Hidden = True
End If
Next i

Related

specific values in 2 cells on same row in different columns if

wht i want is if cell value in column A is 60 then cell value in the same row in column C must equal FF code below.
Sub column_check2()
Dim c As Range
Dim alastrow As Long
Dim clastrow As Long
alastrow = ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Row
clastrow = ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Row
For Each c In Range("A2:A3" & alastrow & ",C2:C3" & clastrow)
If Not c.Value = "60" And c.Value = "FF" Then
MsgBox "error" & c.Address
End If
Next c
End Sub
You just need to loop through each value in Column A and check your criteria (Cell = 60). You can then adjust the value in Column C by using Offset to navigate 2 cells to the right from the current cell in the loop
Sub Looper()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") 'Update Sheet Name
Dim lr As Long, Target As Range
lr = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
For Each Target In ws.Range("A2:A" & lr)
If Target = 60 Then
Target.Offset(0, 2) = "FF"
End If
Next Target
End Sub
Even better, consider the way you would likely do this manually. Filter Column A for your target value and then just modify the resultant cells in Column C. Recreating this in VBA results in a solution more efficient than a loop (the larger the data set, the larger the efficiency gains)
Sub Filter()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim lr As Long: lr = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
ws.Range("A1:A" & lr).AutoFilter Field:=1, Criteria1:=60 'Filter
ws.Range("C2:C" & lr).SpecialCells(xlCellTypeVisible).Value = "FF" 'Apply Values
ws.AutoFilterMode = False 'Remove Filter
End Sub

Clear Cell Contents of range if Contes = 0

I'm wanting to clear a cell contents of a range based.
I think I'm nearly there.
Dim R As Range
Dim myRange As Range
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Set myRange = Range("Y2:Y" & LastRow)
For Each R In myRange
If R = 0 Then
cell.ClearContents
End If
Next
End Sub
All help appreciated.
The problem is your cell reference. Change cell. to R. R is the cell in the range that you are looping through so if R = 0, clear it.
Sub clearR()
Dim R As Range
Dim myRange As Range
lastRow = Range("A" & rows.count).End(xlUp).Row
Set myRange = Range("Y2:Y" & lastRow)
For Each R In myRange
If R = 0 Then
R.ClearContents
End If
Next
End Sub
Another Solution without a loop if column Y is not formula then this should work.
Sub clearR()
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("Y2:Y" & LastRow).Replace 0, "", xlWhole
End Sub
You could use:
Sub Macro1()
Dim LastRow As Long
With ThisWorkbook.Worksheets("Sheet1")
'Find last row
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
'Clear replace 0 with nothing in the specific range
.Range("Y2:Y" & LastRow).Replace What:="0", Replacement:="", LookAt:=xlWhole
End With
End Sub

Fill blank cells based on non blank cell above

I want to fill blank cells in sequential order based on the cell above.
The interval to be filled for column F is not consistent. Need to fill down two up to 20 rows based on the item number above.
Need VBA and not a formula.
This code would copy the item above the non blank and paste it down.
Sub FillBlanks()
Columns("F:F").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=R[-1]C"
End Sub
What I need is to autofill in sequence until the next nonblank.
Something like:
OA205S061169
OA205S061170
OA205S061171
OA205S061172
OA205S061173
OA205S061174
You can select your blank cells, as you are already doing, and then loop through each area, as follows...
Sub FillBlanks()
Dim lastRow As Long
Dim dataRange As Range
Dim currentArea As Range
lastRow = Cells(Rows.Count, "F").End(xlUp).Row
On Error Resume Next
Set dataRange = Range("F1:F" & lastRow).SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not dataRange Is Nothing Then
For Each currentArea In dataRange.Areas
With currentArea
With .Offset(-1, 0).Resize(.Rows.Count + 1)
.Cells(1).AutoFill Destination:=.Cells, Type:=xlFillDefault
End With
End With
Next currentArea
End If
End Sub
Note that it uses Column F to find the last used row. However, depending on your data, you'll likely need to use some other column to determine the last used row. To use Column A, for example, use the following instead...
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
Try
Sub FillTest()
Dim rngDB As Range
Dim vDB, vSplit
Dim i As Long, x As Long, n As Integer
Dim s As String
Set rngDB = Range("f1", Range("f" & Rows.Count).End(xlUp))
vDB = rngDB
For i = 1 To UBound(vDB, 1)
If vDB(i, 1) <> "" Then
vSplit = Split(vDB(i, 1), "S")
s = vSplit(0) & "S"
x = vSplit(1)
n = 0
Else
n = n + 1
vDB(i, 1) = s & Format(x + n, "000000")
End If
Next i
rngDB = vDB
End Sub

Can't count certain sign appeared in each cell

I'm trying to create a macro which is supposed to check for a number of range to find out if there are multiple items separated by semicolon ; within certain cells. If it finds one then the macro will count how many times that sign ; has appeared in each cell:
For example, in Range("A1") that sign has appeared 3 times:
apple;orange;guava;malta
I've tried so far:
Sub DistributeItemsToColumns()
Dim cel As Range
For Each cel In Range("A1:L4")
If InStr(cel, ";") > 0 Then
Debug.Print cel 'can't think further
End If
Next cel
End Sub
Try:
Option Explicit
Sub test()
Dim LastRow As Long, i As Long, arr As Variant
Dim NumberOfOccu As Long
With ThisWorkbook.Worksheets("Sheet1")
'Find last row of column A
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Loop from row 1 to lastrow
For i = 1 To LastRow
If InStr(.Range("A" & i).Value, ";") Then
'Number of characters
NumberOfOccu = InStr(.Range("A" & i).Value, ";")
arr = Split(.Range("A" & i).Value, ";")
.Range("C" & i).Resize(, UBound(arr) + 1) = arr
End If
Next i
End With
End Sub
Results:

How to delete the values which are not followed by second immediate cell?

How to delete the values which are not followed by second immediate cell?
Following might help.
Sub Demo()
Dim ws As Worksheet
Dim lastRow As Long
Dim cel As Range
Set ws = ThisWorkbook.Sheets("Sheet5") 'change Sheet5 to your data sheet
With ws
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For Each cel In .Range("B1:B" & lastRow)
If IsEmpty(cel) Then 'or use If Len(cel) = 0 Then
cel.Offset(0, -1).ClearContents
End If
Next cel
End With
End Sub
If you want to delete the rows where Column B is empty then try this
Sub Demo()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet5") 'change Sheet5 to your data sheet
ws.Range("B1:B100").SpecialCells(xlCellTypeBlanks).EntireRow.Delete 'change range as per your data
End Sub
Use this macro. Enter it in regular module (eq Module 1). it will remove all values in column A in case there is no value in adjacent cell in column B.
Sub delete()
For x = 1 To Cells(Rows.Count, "A").End(xlUp).Row
If Range("B" & x).Value <> "" Then
Else
Range("A" & x).ClearContents
End If
Next x
End Sub
or if you want to delete those rows.
Sub deleteRows()
For x = Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1
If Range("B" & x).Value <> "" Then
Else
Range("A" & x).EntireRow.delete
End If
Next x
End Sub

Resources