This question already has answers here:
Change length of For loop while in the loop
(3 answers)
Closed 4 years ago.
I'm trying to run "for loop" certain times and occasionally add an extra row in the range being under the loop. Let's say, that example has originally 15 rows, and during the loop, the condition is true 2 times, so it should add +2 to the total number of rows. However, the code doesn't seem to execute the loop for those added rows and exits right after passing the value of orc=15.
Code below:
sub loop_to_orc()
dim i as integer, orc as integer
dim operations as range
set operations = range(cells(1, 1), cells(, 1).end(xldown))
orc = operations.rows.count
for i = 1 to orc
if cells(i,1)>0 then
rows(i+1).insert
end if
orc = operations.rows.count
next i
end sub
Where am I wrong? Is there any method to actually run the loop for added rows?
You could introuduce one additional variable:
Dim rowsAdded As Long
rowsAdded = 1
And then use it like this:
for i = 1 to orc
if cells(i,1)>0 then
rows(i+rowsAdded).insert
i = i - 1
rowsAdded = rowsAdded + 1
end if
next i
This way, you won't increment i when rows is inserted, but you select i in a loop, so we have to add rowsAdded.
Related
This question already has answers here:
Deleting Duplicate Visible Rows
(2 answers)
Code not deleting every non numeric row
(1 answer)
VBA For loop not exiting
(3 answers)
Delete specific rows from selection in for each loop
(1 answer)
Excel ListObject Table - Remove filtered / hidden rows from ListObject table
(1 answer)
Closed 4 years ago.
I am trying to write a code that goes row by row checking to see if a certain column in that row contains the string values "Pick-Up" or "Pickup", and if it does delete that entire row. The code I currently have runs, and deletes some of the rows containing said values. But not all of them.
I have tried the AutoFilter method, and for my purposes I do not believe that it will work.
Any help would be greatly appreciated. Thanks!
Sub Delete()
Dim x As Long, y As Long
Dim testString As String
y = Worksheets("Data").Range("A1", Range("A1").End(xlDown)).Rows.Count
For x = 2 To y
testString = Worksheets("Data").Cells(x, 27).Value
testString = Trim(testString)
If testString Like "*Pick-Up*" Or testString Like "*Pickup*" Then
Rows(x).EntireRow.Delete
Else
x = x + 1
End If
Next x
End Sub
This question already has answers here:
Code in VBA loops and never ends. How to fix this?
(2 answers)
Closed 5 years ago.
I am trying to delete entire rows based on whether the cell value in the D column is NULL or not. My entire code so far is:
Sub DeleteNULL()
Dim i As Long
For i = 2 To 119713
If IsEmpty(Range("Di")) = True Then
Rows([i]).EntireRow.Delete
Else
If IsEmpty(Range("Di")) = False Then
Next i
End If
End Sub
I keep getting compile errors, either If without Else or Next without For, how should I fix this?
Thanks in advance.
A few things:
Placement of a lot of syntax is off.
When adding or deleting rows, you need to loop backwards based on how Excel handles these events.
See code below:
Sub DeleteNULL()
Dim i As Long
For i = 119713 To 2 Step -1
If IsEmpty(Range("D" & i)) Then Rows([i]).EntireRow.Delete
Next i
End Sub
This question already has answers here:
workaround named range character limit
(3 answers)
Closed 5 years ago.
Suppose you have an array of numbers, and they are the column numbers that you would like to delete. A smart idea is to convert them to letters and then concatenate them and delete all the columns, as shown here by #Siddharth Rout. But there is a problem, it seems there is an upper limit of string inside range, so say
str = "AB:AB,CJ:CJ,CZ:CZ,NJ:NJ,NK:NK,NL:NL...",
Len(str)=300, 'Just about 50 columns, not too many indeed, there are 16384 columns in Excel 2010!!!
Chances are you will get an error if you use Range(str).Delete Shift:=xlToLeft, how to solve this problem?
Another option
Option Explicit
Public Sub DeleteColumns()
Dim i As Long, arr As Variant
arr = Split("3-5-7", "-") 'ascending order
Application.ScreenUpdating = False
With Sheet1
For i = UBound(arr) + 1 To LBound(arr) + 1 Step -1
.Cells(Val(arr(i - 1))).EntireColumn.Delete
Next
End With
Application.ScreenUpdating = True
End Sub
(slow for a large number of columns)
I am trying (using VB) to copy 7 values from 1 location to another.
(copy from F115 to F134.)
(F115 is source of numbers; F134 is destination for numbers)
If there is data (any data) in F134 I want to advance the row by 1 and then print/copy. I do not want to overwrite any lines of data.
So, if there is data in the first row (F134) then I want to move/advance the row count by 1 and then copy the values into row F135 - and so on. It's possible that there could be 500 to 100 rows of numbers and I need to be able to view all of this data.
So far, this is the code I have: some works, but it does NOT advance the row count and continues to print into Cell F134. (oh yes, F124 is just a set of empty cells.)
Ok, this is my code;
Sub dbtest4()
Dim RowCounter As Integer
rowCounter = 1
Do Until rowCounter = rowCounter +1
'trying to advance the row counter by 1 such that if there is data in F134, the line count will advance and the new data will print into the NEXT line. (which would be F135)
'the following code probably is unnecessary, but I'm trying anything...
'actually the following code works ok: Range("F134").Resize(1,7).Value = Range("F115").Resize(1,7).Value, but that's about it.
If Range ("F134") = 0 then
Range("F134").Resize(1,7).Value = Range("F115").Resize(1,7).Value
'trying to print data cells from F124 if there is no data in F134
Else
Range("F134"),Resize(1,7).Value = Range("F124).Resize(1,7).Value
'trying to print the zeros or blank numbers from F115 if F134 has data in it
End if
Loop
Exit Do
End Sub
Thanks to anyone who may be able to offer some assistance.
So right now the exit condition for the Do While loop is that rowCounter has to increment by one. I can't see a reason why this should fail and that is why it is only happening once. Here is some psuedocode:
Do Until copiedVals = 7 'Or whatever exit value you want
If row.Value.Exists() then
'There is a value in this row, so increment the row count
rowCounter = rowCounter + 1
Else
'This row is empty and you can do your thing
'rowCounter will have the current row value
copiedVals = copiedVals + 1
End If
End Loop
Dim i As Long
Dim rows As Long
Dim rng3 As Range
rows = rng3.rows.Count
For i = rows To 1 Step (-1)
Does anyone know how this loop works? I'm confused on the meaning of rows To 1 Step (-1).
from high number To 1 adding (-1) each iteration
Note: It's adding because + AND - in mathematical logic evaluate to a -
If rows = 10 then
for i = 10 to 1 step -2 would mean loop back from 10 to 1 subtracting 2 from the i in each loop cycle.
adding a Debug.Print i inside the loop may give you a better clue.
Note: turn ON the Immediate Window hitting CTRL+G or View => Immediate Window from the VBE menu bar
An example loop increasing by 3 on each cycle.
for i = 1 to 10 step 3
debug.print i
next i
Usage
The step-back technique is mostly used when deleting rows from a spreadsheet.
To see the logic in practice see the following
How to select and delete every 3rd column
Delete entire excel column if all cells are zeroed
Excel VBA - Scan range and delete empty rows
When deleting rows, it is often common practise to start at the end and step backwards, this is so no rows are skipped.
Dim i As Long
Dim rows As Long
Dim rng3 As Range
rows = rng3.rows.Count
For i = rows To 1 Step (-1)
'delete row if "delete" is in column 1
If rng3.cells(i,1).Value = "delete" Then
rng3.Rows(i).EntireRow.Delete
End If
next i
Dim i as Integer
For i = 1 To 14 Step 3
Debug.Print i
Next i
In above code loop will iterate from 1 to 14 increment with 3 so output will be like
1 4 7 10 13
It means it can not cross 14 that is limit.
So whatever value is provided in step it will add into the variable use for looping purpose. Here
i = i +3
But in For loop in VBA, Step value can not be changed dynamically. For example:
Dim i As Integer
For i = 1 To 10 Step i
Debug.Print i
Next i
Here, before starting iteration Step is equal to the value of i that is the default value i.e. 0. So i will increment like below:
i = i+ i => i = i+0
So i will not increment here and loop will iterate for ever.
Now for below code:
Dim i as Integer
For i = 1 To 14 Step i+1
Debug.Print i
Next i
i will increment like :
i=i+(i+1) => i= i+(0+1) =>i = i+1
so it will increment by 1 and output will be 1 2 3 .... 14
Now for below code :
Dim i As Integer
i = 3
For i = 1 To 10 Step i
Debug.Print i
Next i
here, i is equal to 3 before loop execution, so Step value will be 3, but loop will start with i = 1 and will increment with 3 through out the loop.
here,
i = i+3
so output will be 1 4 7 10.
Now for some other variable:
Dim i As Integer
Dim j As Integer
j = 2
For i = 1 To 10 Step j
Debug.Print i
j = i
Next i
in above code Step value will be 2, so i will increment by 2 for every iteration whether j is modifying inside loop or not, it will not impact Step value, so output will be
1 3 5 7 9
Please correct me if I miss anything or something is wrong in this. Also suggest if there is any way for dynamic looping using For loop in VBA.