EXCEL VBA Skip blank row - excel

Private Sub CommandButton1_Click()
Dim rng As Range
Dim cell As Variant
Set rng = Range("C8:C12")
For Each cell In rng
Sheets("Sheet1").Range("A1:H7").Copy Destination:=Sheets("Quantity").Range("XFD4").End(xlToLeft).Offset(-3, 3)
Sheets("Quantity").Range("XFD1").End(xlToLeft).Offset(0, 1).Value = cell.Offset(1, -1).End(xlUp).Value
Sheets("Quantity").Range("XFD2").End(xlToLeft).Offset(0, 1).Value = cell.Value
Sheets("Quantity").Range("XFD3").End(xlToLeft).Offset(0, 1).Value = cell.Offset(0, 1).Value
Next
End Sub
What I want to accomplish here is to skip blank cell/row. Because it will copy empty data to the sheet. Is there any method e.g. Not isEmpty or isBlank for this For loop? Thanks in advance.

You should be able to check IsEmpty(cell) to see if a cell is empty.
For example (untested):
For Each cell In rng
If Not IsEmpty(cell) Then
Sheets("Sheet1").Range("A1:H7").Copy Destination:=Sheets("Quantity").Range("XFD4").End(xlToLeft).Offset(-3, 3)
Sheets("Quantity").Range("XFD1").End(xlToLeft).Offset(0, 1).Value = cell.Offset(1, -1).End(xlUp).Value
Sheets("Quantity").Range("XFD2").End(xlToLeft).Offset(0, 1).Value = cell.Value
Sheets("Quantity").Range("XFD3").End(xlToLeft).Offset(0, 1).Value = cell.Offset(0, 1).Value
End If
Next

Related

in Excel VBA why does my code not work with SpecialCells type visible and work without it?

In columns Bk and CB they both contain formula's that will result in a code. Now CB will also contain four codes and a remove statement which if they match with the cell in column BK in the same row then take the value from CB and paste over hence overriding the value in BK with that code and then paste it red.
the above should be done only on a filtered range though.
The ignore #N/A are in there as the overide column will error out on almost everyline except for when there is a code to overide.
This macro works perfectly without the visible cells statement at the end of my with range line but as soon as the visible cells statement is added the loop only goes up to #N/A and disregards the rest of the ElseIF statement.
Here is my code below:
Option Explicit
Sub Override()
Dim x As Workbook: Set x = ThisWorkbook
Dim rRange As Variant, fltrdRng As Range, aCell As Range, rngToCopy As Range
Dim ws As Worksheet
Dim LR As Long
Dim LR2 As Long
Dim SrchRng As Range, cel As Range
Dim mRow
mRow = 2
Set ws = x.Worksheets("Data")
LR = ws.Range("CB" & ws.Rows.Count).End(xlUp).Row
LR2 = ws.Range("BK" & ws.Rows.Count).End(xlUp).Row
'clears any filters on the sheet
ws.AutoFilterMode = False
' turns formula's to manual
Application.Calculation = xlManual
'copies down the formula in Column BK ignoring the last two rows as they have already been pasted over.
ws.Range("BK2:BK4 ").AutoFill Destination:=ws.Range("BK2:BK" & LR2 - 2)
'filters on N/A's and 10 as these are the codes we are interested in overiding
ws.Range("$A$1:$CB$1").AutoFilter Field:=19, Criteria1:=Array( _
"10", "N/A"), Operator:= _
xlFilterValues
' will loop through all cells in specified range and ignore any error's and #N/A's and will paste over the code overided in CB column to the BK column if conditions are met.
On Error Resume Next
While IsEmpty(ws.Range("CB" & mRow)) = False
With ws.Range("CB" & mRow).SpecialCells(xlCellTypeVisible)
If .Value = "#N/A" Then
ElseIf .Value = "1234" Then
.Offset(0, -17).Value = "1234"
.Offset(0, -17).Interior.Color = vbRed
ElseIf .Value = "1235" Then
.Offset(0, -17).Value = "1235"
.Offset(0, -17).Interior.Color = vbRed
ElseIf .Value = "1236" Then
.Offset(0, -17).Value = "1236"
.Offset(0, -17).Interior.Color = vbRed
ElseIf .Value = "Remove" Then
.Offset(0, -17).Value = "Remove"
.Offset(0, -17).Interior.Color = vbRed
ElseIf .Value = "1237" Then
.Offset(0, -17).Value = "1237"
.Offset(0, -17).Interior.Color = vbRed
End If
End With
mRow = mRow + 1
Wend
'turn Formula 's back to automatic
Application.Calculation = xlAutomatic
End Sub
With ws.Range("CB" & mRow).SpecialCells(xlCellTypeVisible)
Using SpecialCells on just one cell is problematic.
Instead, use it on the entire filtered column, like this, which will replace your entire While...Wend loop (by the way, While...Wend is obsolete):
On Error Resume Next
Dim visibleCells As Range
Set visibleCells = ws.Range("CB2:CB" & LR).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If visibleCells Is Nothing Then Exit Sub
Dim cell As Range
For Each cell In visibleCells
If Not IsError(cell.Value) Then
Select Case cell.Value
Case "1234", "1235", "1236", "1237", "Remove"
cell.Offset(0, -17).Value = cell.Value
cell.Offset(0, -17).Interior.Color = vbRed
End Select
End If
Next

Apply formula, offset three columns from selected cells

I created VBA code to apply a formula which should do the following: when the user selects a range of cells, the formula is applied 3 columns on the right side of the data of the selection. For example if the user selects range G8:G18, when the user executes the macro, the formula should be applied on range J8:J18 from the data of range G8:G18
However the formula is in range G8 instead of being applied on range J8:J18.
Sub ghjkk()
Dim c As Range
Dim rng As Range
Set rng = Selection.Offset(0, 3)
For Each c In rng
ActiveCell.FormulaR1C1 = _
"=IF(RC[-3]=0.2,""Y5"",IF(RC[-3]=0.1,""Y6"",IF(RC[-3]=0,""V0"",IF(RC[-3]=0.021,""Y3"",IF(RC[-3]=0.055,""Y4"",FALSE)))))"
Next c
End Sub
Try
Sub ghjkk()
Dim c As Range
Dim rng As Range
Set rng = Selection.Offset(0, 3)
For Each c In rng
c.FormulaR1C1 = _
"=IF(RC[-3]=0.2,""Y5"",IF(RC[-3]=0.1,""Y6"",IF(RC[-3]=0,""V0"",IF(RC[-3]=0.021,""Y3"",IF(RC[-3]=0.055,""Y4"",FALSE)))))"
Next c
End Sub
If needed, change sheet name and range & import the below code on Worksheet_Change Event on the specific sheet.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("G8:G18")) Is Nothing Then
Application.EnableEvents = False
If Cells(Target.Row, 4).Value = "0.2" Then
Cells(Target.Row, 10).Value = "Y5"
ElseIf Cells(Target.Row, 4).Value = "0.1" Then
Cells(Target.Row, 10).Value = "Y6"
ElseIf Cells(Target.Row, 4).Value = "0" Then
Cells(Target.Row, 10).Value = "V0"
ElseIf Cells(Target.Row, 4).Value = "0.021" Then
Cells(Target.Row, 10).Value = "Y3"
ElseIf Cells(Target.Row, 4).Value = "0.055" Then
Cells(Target.Row, 10).Value = "Y4"
Else: Cells(Target.Row, 10).Value = "False"
End If
Application.EnableEvents = True
End If
End Sub

Cell Address in a loop

I am looping through a range of cells to check if a value is 0. My logic is if cell value is zero, then cell value is the previous cells value. If that previous cell is also zero, then it is the next cell's value. But I said what if the last cell or first cell is zero? I need to check that too because if it is the first cell or last, then the loop fails. My question is, what should the code be to pass through the last row as an address. I know the last row, but I do not know how to write it as an address with a known column. The data starts at row 2 and then goes to row X.
For each Cell In rng
If Cell.Address="A2" Then
If Cell.Value=0 Then
Cell.Value=Cell.Offset(1,0).Value
End if
Elseif Cell.Address="AX" Then 'X is the last row
If Cell.Value=0 Then
Cell.Value=Cell.Offset(-1,0).Value
End If
Elseif Cell.Value=0 and Cell.Offset(1,0).Value=0 Then
Cell.Value=Cell.Offset(-1,0).Value
Elseif Cell.Value=0 Then
Cell.Value=Cell.Offset(1,0).Value
Else
Do Nothing
End If
Next
I've added three rows to define the variables and range.
Other than that I've only made changes to the IF statement and the first ELSEIF statement.
Sub Test()
Dim rng As Range
Dim Cell As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1:A20")
For Each Cell In rng
If Cell.Address = rng(1).Address Then
If Cell.Value = 0 Then
Cell.Value = Cell.Offset(1, 0).Value
End If
ElseIf Cell.Address = rng(rng.Cells.Count).Address Then
If Cell.Value = 0 Then
Cell.Value = Cell.Offset(-1, 0).Value
End If
ElseIf Cell.Value = 0 And Cell.Offset(1, 0).Value = 0 Then
Cell.Value = Cell.Offset(-1, 0).Value
ElseIf Cell.Value = 0 Then
Cell.Value = Cell.Offset(1, 0).Value
Else
'Do Nothing
End If
Next
End Sub
Edit: (after answer accepted).
To loop through each cell in each column you'll need a loop to look at each column and then another to look at each cell within the column.
In the code below I have defined col as a range.
This is then used in the first loop (For Each col in rng.Columns).
The second loop then looks at each cell within col (For Each Cell in col.Cells).
Sub Test()
Dim rng As Range
Dim Cell As Range
Dim col As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1:Z20")
For Each col In rng.Columns
For Each Cell In col.Cells
If Cell.Address = rng(1).Address Then
If Cell.Value = 0 Then
Cell.Value = Cell.Offset(1, 0).Value
End If
ElseIf Cell.Address = rng(rng.Cells.Count).Address Then
If Cell.Value = 0 Then
Cell.Value = Cell.Offset(-1, 0).Value
End If
ElseIf Cell.Value = 0 And Cell.Offset(1, 0).Value = 0 Then
Cell.Value = Cell.Offset(-1, 0).Value
ElseIf Cell.Value = 0 Then
Cell.Value = Cell.Offset(1, 0).Value
Else
'Do Nothing
End If
Next
Next col
End Sub

Summing all Instance of Variable in Range VBA

I have this code:
Sub yeartest()
Dim cell As Range
storeval = 0
For Each cell In Range("I7:I17")
If cell.Value = "THISVALUE" Then
Let storeval = cell.Offset(-1, 0).Value
End If
Range("Q18").Activate
ActiveCell.Formula = "=SUM(storeval)"
Next cell
End Sub
What the code should do is analyze the range I7:I17. Everytime it encounters a cell in this range with the value THISVALUE it should go right by one cell and store that value. After the entire range has been analyzed the sum of all cells one right of THISVALUE should be output in cell Q18.
Currently cell Q18 just displays a #NONAME value when I execute the macro.
Sub yeartest()
Dim cll As Range
storeval = 0
For Each cll In Range("I7:I17")
If cell.Value = "THISVALUE" Then
storeval = storeval + cell.Offset(-1, 0).Value
End If
Next cll
Range("Q18")=storeval
End Sub

Search through selected cells and hide them if they contain a letter

I want to loop through cells and look for letters. If they contain the letter hide the cell with NumberFormat. This works but how do I make this loop toggeable so i can hide/unhide.
Private Sub CommandButton1_Click()
Dim rng As Range, cell As Range
Set rng = Range("D5:F35,D43:F73,J5:L35,J43:L73,P5:R35,P43:R73,V5:X35,V43:X73,AB5:AD35,AB43:AD73,AH5:AJ35,AH43:AJ73")
For Each cell In rng
If InStr(1, cell.Value, "A") > 0 Then cell.NumberFormat = ";;;"
If InStr(1, cell.Value, "B") > 0 Then cell.NumberFormat = ";;;"
If InStr(1, cell.Value, "C") > 0 Then cell.NumberFormat = ";;;"
If InStr(1, cell.Value, "D") > 0 Then cell.NumberFormat = ";;;"
If InStr(1, cell.Value, "F") > 0 Then cell.NumberFormat = ";;;"
Next cell
End Sub
I'm not sure what you mean by "toggeable"
If you want to unhide everything, no matter what it contains, then just set the .numberformat property of the entire range to General.
If you mean that when you remove one of the target letters from the cell, that it should become unhidden, then try this macro below:
EDIT Edited to add what I think you mean by toggle.
========================================
Option Explicit
Option Compare Binary
Private Sub CommandButton1_Click()
Dim rng As Range, cell As Range
Set rng = Range("D5:F35,D43:F73,J5:L35,J43:L73,P5:R35,P43:R73,V5:X35,V43:X73,AB5:AD35,AB43:AD73,AH5:AJ35,AH43:AJ73")
Application.FindFormat.NumberFormat = ";;;"
Set cell = rng.Find(what:="*", searchformat:=True)
If Not cell Is Nothing Then
rng.NumberFormat = "General"
Exit Sub
End If
For Each cell In rng
If cell.Value Like "*[ABCDEF]*" Then
cell.NumberFormat = ";;;"
Else
cell.NumberFormat = "General"
End If
Next cell
End Sub
====================================
Try...
Private Sub CommandButton1_Click()
Dim rng As Range
Dim cell As Range
Set rng = Range("D5:F35,D43:F73,J5:L35,J43:L73,P5:R35,P43:R73,V5:X35,V43:X73,AB5:AD35,AB43:AD73,AH5:AJ35,AH43:AJ73")
For Each cell In rng
If cell.Value Like "*A*" Or cell.Value Like "*B*" Or cell.Value Like "*C*" Or cell.Value Like "*D*" Or cell.Value Like "*F*" Then
cell.NumberFormat = ";;;"
End If
Next cell
End Sub
Sub Macro1()
If cell.NumberFormat = ";;;" Then
cell.NumberFormat = "General"
End If
End Sub
The last part of the number format is for text - just remove that part from your custom number format to hide/show text.
Sub HideText()
Dim rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1") _
.Range("D5:F35,D43:F73,J5:L35,J43:L73,P5:R35,P43:R73,V5:X35,V43:X73,AB5:AD35,AB43:AD73,AH5:AJ35,AH43:AJ73")
If rng.NumberFormat <> "#,##0;-#,##0;#,##0;" Then
rng.NumberFormat = "#,##0;-#,##0;#,##0;"
Else
rng.NumberFormat = "#,##0;-#,##0;#,##0;#"
End If
End Sub

Resources