I'm trying to delete every cell, and a cell to the left of it, if the value of the cell is 0. (Also, to set the interior color to no fill if the value of the cell is greater than 0.)
This is my code so far
For Each cell In Range("I2:I" & LastTransaction)
If cell.Value = 0 Then
Range(cell.Offset(0, -1).Address, cell.Address).Delete Shift:=xlUp
ElseIf cell.Value > 0 Then
cell.Interior.ColorIndex = 0
End If
Next cell
The problem here is that, every time the cells are deleted, AND SHIFTED UPWARDS, the for each loop doesn't take the upward shift into account, and skips the next cell in the range.
As per #JvdV's comment, when deleting in a loop you need to do it back to front (or in this case bottom to top), using Step -1.
In this case your For loop would look something like;
For x = LastTransaction to 2 Step -1
If Range("I" & x).Value = 0 then
Range("H" & x & ":I" & x).Delete Shift:=xlUp
ElseIf Range("I" & x).Value > 0 Then
Range("I" & x).Interior.ColorIndex = 0
End If
Next
The main issue is that, when you loop through the range, each cell refers to a particular cells on the sheet.
So your loop loops through I2, I3, I4, .... If you delete a row, all the other rows are moved up, and what was in cell Ix is now in cell I(x-1), but the loop will be at cell Ix, and so a whole row will have avoided being processed.
One solution is to store all the rows that are to be deleted in a Collection, and then delete the collection afterwards.
'data
Cells.Clear
For i = 1 To 15
Cells(i, 1) = Int(Rnd * 4)
Cells(1, i + 2) = Cells(i, 1)
Next i
'code
Dim tbd As New Collection 'to be deleted collection
For Each c In [a1:a15]
If c = 1 Then tbd.Add c 'adds a row that is to be deleted
Next c
For Each c In tbd
c.Delete 'deletes all rows in tbd
Next c
The first part of the code creates some sample data to process. Then it defines a new Collection, adds the rows to it that are to be deleted (anything with a value 1 is this case), and then deletes them from the collection.
Related
I am trying to move cell values in columns K to L down within its column to the same row as every blank cell in column E.
Hopefully this makes sense but I think i need to figure out how to find each blank cell's row number and force it as a row variable i can then use to tell my code to move cell values in range K13:L. For example, if there's a value in K13:L14 and the blank cells in column E is E20 and E23, i want K13 and L13 to move to K20 and L20 while K14 and L14 move to K23 and L23.
The number of blank cells will always match however many cells with value are in column K/L
Would appreciate any help on this!
Use the macro below to start your studies. But first you need to remove the values in column K&L to N&O. (Maybe you can record a macro and add the recorded codes to the start of the codes below.)
Sub move_it()
i = 13
j = 13
Do While Cells(j, 14).Value <> ""
If Cells(i, 5) <> "" Then
i = i + 1
Else
Range("n" & j, "o" & j).Select
Selection.Cut
Range("K" & i).Select
ActiveSheet.Paste
j = j + 1
i = i + 1
End If
Loop
End Sub
The answer that was i needed to offset my copy paste by the rows i needed to move it to!
ws.Range("E" & openitemstartrow + 1, ws.Range("F" & openitemstartrow +
10).End(xlUp)).Copy
targetws.Range("G" & rows.Count, "H" & rows.Count).End(xlUp).Offset(1, 4).PasteSpecial Paste:=xlPasteValues
I am creating several macro's to fill in values in an excel sheet based on formulas so that the file doesn't get to heavy to process.
I'm using for if else with in the if statement the condition to check if the cell itself and a cell in another column is empty. If these cells are empty no value needs to be put in the destination cell.
If the cells are not empty, first I fill in the formula and then I make sure only the value remains.
For the cells which didn't need to be filled in however, the formula is filled in.
I've tried several types of loops, I also exported the modules, removed them and imported them again, but this doesn't work.
I've started with the code below:
Sub ActionCI()
i = 4
x = 4
' x is column number of column which needs to be filled in
For i = 4 To 100
If (ThisWorkbook.Sheets("CHECK IN").Cells(i, x).Value = "" And _
IsEmpty(ThisWorkbook.Sheets("CHECK IN").Cells(i, 2).Value)) Then Exit For
End if
Sheets("CHECK IN").Cells(i, x).Value = "=IFNA(INDEX('PLANNED FOR ARRIVAL'!G:G,MATCH([#REFERENCE],'PLANNED FOR ARRIVAL'!D:D,0)),"""")"
Sheets("CHECK IN").Cells(i, x).Value = Sheets("CHECK IN").Cells(i, x).Value
Next i
End Sub
Then I tried:
Sub ActionCI()
i = 4
x = 4
' x is column number of column which needs to be filled in
For i = 4 To 100
If (ThisWorkbook.Sheets("CHECK IN").Cells(i, x).Value = "" And _
IsEmpty(ThisWorkbook.Sheets("CHECK IN").Cells(i, 2).Value)) Then _
ThisWorkbook.Sheets("CHECK IN").Cells(i, x).Value = ""
Exit For
Else
Sheets("CHECK IN").Cells(i, x).Value = "=IFNA(INDEX('PLANNED FOR ARRIVAL'!G:G,MATCH([#REFERENCE],'PLANNED FOR ARRIVAL'!D:D,0)),"""")"
Sheets("CHECK IN").Cells(i, x).Value = Sheets("CHECK IN").Cells(i, x).Value
End If
Next i
End Sub
What can I change/add to make this work?
I expect cell f.ex. in row 10, column 4 to remain empty if it was empty before and also the cell in row 10, column 2 was empty.
However the formula IFNA... is inserted in this cell (up until row 100)
What I would do:
replace IsEmpty with LenB(Trim(...) as it can process "invisible" chars that may come up when importing or copying data from an external source
use Cells().FormulaLocal when inserting formula into a cell
use a var for sheet reference which is correct in your case but it takes too much typing and chance of error
use With when working with a single sheet/workbook/etc
So the code would look like this:
Sub ActionCI()
Dim sh as WorkSheet
i = 4
x = 4
Set sh = ThisWorkbook.Sheets("CHECK IN")
With sh
For i = 4 To 100
If LenB(Trim(.Cells(i, x).Value)) = 0 And _
LenB(Trim(.Cells(i, 2).Value)) = 0 Then
.Cells(i, x).ClearContents ' it is a copy from code but makes no sense for the cell is empty anyway
Exit For
Else
.Cells(i, x).FormulaLocal = "=IFNA(INDEX('PLANNED FOR ARRIVAL'!G:G,MATCH([#REFERENCE],'PLANNED FOR ARRIVAL'!D:D,0)),"""")"
End If
Next i
End With
End Sub
I have a data sheet where column B is a list of data types. The only value I care about is if the value is SIS.
If row 2 has the value SIS in column B and row 3 has the value SIS in column B, then delete row 2. If row 3 contained instead a value of Topic, then keep row 2, ignore row 3, and look at row 4.
The attached image shows the sample data with a column called VBA Instructions. Any help is appreciated.
I think the below could work, this is testing if the cell in front has the same value as the current cell, if so then it would delete it. I would copy the dataset into another sheet before running a macro which deletes rows. The only problem with the below code is if you have two "Topic" cells right next to each other, for example if "Topic" tags were in rows 3 and 4 then row 3 would be deleted.
Sub test()
Columns("A:A").Insert Shift:=xlToRight
last = Cells(Rows.Count, 3).End(xlUp).Row
For Each Cell In Range("C2:C" & last)
If (Cell = Cells(Cell.Row + 1, 3)) = True Then
Range("A" & Cell.Row).Value = True
Else:
Range("A" & Cell.Row).Value = False
End If
Next Cell
For x = 2 To last:
If Cells(x, 1) = True Then
Rows(x).Delete
x = x - 1
End If
Next x
Columns("A:A").Delete
End Sub
I want to perform a goal seek for several rows (I don't know exactly how many - in the code I have below it stops in row 100). The thing is, for example, if I have 10 rows, row number 5 can be empty. So I want it to skip from row 4 to row 6, then continue, then "jump" another empty row if they exist.
I want to set cell M2 to 0 by changing the value of cell K2. The same for row 3, row 4, etc. and I want it to skip empty rows.
Right now I just have this... a simple case
Sub GSeek()
Dim i As Long
For i = 2 To 100
range("M" & i).GoalSeek Goal:=0, ChangingCell:=range("K" & i)
Next
End Sub
Try this
Dim i As Long
For i = 2 To 100
If range("M" & i).Value <> "" Then
range("M" & i).GoalSeek Goal:=0, ChangingCell:=range("K" & i)
End If
Next i
Each time it just makes sure that the cell isn't empty. If it is, it'll just move to next i
Due to beginner for VBA, I am in a difficult to find this codes.
I need to create 'Command Button' to insert formula according to
current cell location.
Eg. If current cell location is S7, need to get formula in to it '=K7*L7'.
Current cell location change all the time. Multiplication of Column K and L fixed.
Please help me to write this codes.
You can assign below code to command button
Sub Insert_Formula
n = Selection.row
Selection.Value = "=K" & n & "*L" & n
End Sub
In VBA, Selection will get the selected cell properties.
For example, if you select S7,
n = Selection.Row
Then n will be 7
Selection.Value = "=K" & n & "*L" & n
Above will set selected cell's formulat to =K7*L7
In addition, if you want the button to work on selected range which is more than one cell,
Private Insert_Formula_Multi_Cells
For X = 1 To Selection.Rows.Count
n = Selection.Row + X - 1
Selection.Range(Cells(X, 1), Cells(X, Selection.Columns.Count)) = "=K" & n & "*L" & n
Next X
End Sub
Selection.Rows.Count Gets number of rows selected.
Selection.Columns.Count gets number of columns selected
to get current location in excel you can use ActiveCell.Address command.
Below code first gets current selected cells address and then multiplies with K(11) and L(12) columns to print value in active cell.
Sub acell()
Dim s As String
s = ActiveCell.Address
Range(s).Select
Range(s).Value2 = Cells(2, 11) * Cells(2, 12)
Debug.Print s
End Sub
You can add them in loop as per your requirement.