for each loop in excel macros vba jumping alternate rows - excel

I have the below code in vba. The rows in sheet5 from columns a5 to a20 are:
a5=Sweden
a6=Spain
a7=Russia
a8=Italy
a9=Germany
a10=Finland
a11=Norway
a12=Switzerland
a13=France
a14=Belgium
Set fillcolumnrange = Sheet5.Range("A5:A20")
i = 1
For Each Row In fillcolumnrange.Rows
If Not Sheet5.Range("A" & i + 4) = "" Then
MsgBox Row(i)
End If
i = i + 1
Next Row
But this code is prompting only alternate values ie.
Sweden
Russia
Germany
Norway
France
Can anyone please help me out find the bug in the code

You were looping through the rows in your range and also advancing the variable i within your loop.
You can reference each variable that you are looping through.
Try this
Set fillcolumnrange = Sheet1.Range("A5:A20")
For Each cell In fillcolumnrange.Cells
If Not cell = "" Then
MsgBox cell
End If
Next cell

You've got a mixture of different types of loop.
Either do what Rick says.
Or use i:
Set fillcolumnrange = Sheet5.Range("A5:A20")
For i = 1 To fillcolumnrange.Rows.Count
If Not Sheet5.Range("A" & i + 4) = "" Then
MsgBox Sheet5.Cells(i + 4, 1)
End If
Next i
Or maybe a do-Loop
Set fillcolumnrange = Sheet5.Range("A5:A20")
i = 1
do until i = fillcolumnrange.Rows.Count + 4
If Not Sheet5.Range("A" & i + 4) = "" Then
MsgBox Sheet5.Cells(i + 4, 1)
End If
i=i+1
Loop
(EDIT now tested and seem to run ok)

Building on Rick's answer, here's the short version of the For Each loop:
For Each cell in fillcolumnrange.Cells
If len(cell) <> 0 Then MsgBox cell
Next

Related

VBA Insert set number of rows if string found

I am new to macros in Excel, and I’m trying to speed up a process. I need to add a varying number of blank rows, if certain text is present in the cell above it. Not equal, but containing.
For example if A1 contains 'Apples', add two blank rows beneath. If A6 has 'Plums', add four blank rows beneath, etc.
What I have now is this:
For a=1 To ActiveSheet.Cells(Rows.Count,1).End(x1Up).Row
If ActiveSheet.Cells(a,1).Value = “Apples” Then
ActiveSheet.Rows(2).Insert
a = a+1
ELSE
If ActiveSheet.Cells(a,1).Value = “Plums” Then
ActiveSheet.Rows(4).Insert
a = a+1
End If
End Sub
So far I've gotten a Compile Error, stating "Block If without End If" though I believe I closed them both. I'm not sure if I'm correctly comparing or searching for a string as well (referring to my use of ="Apples"), but cannot get it to run at all to test that part.
For a = 1 To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
If TypeName(ActiveSheet.Cells(a, 1)) = "String" Then
If ActiveSheet.Cells(a, 1).Value = "Apples" Then
ActiveSheet.Rows(2).Insert
a = a + 1
ElseIf ActiveSheet.Cells(a, 1).Value = "Plums" Then 'One error here
ActiveSheet.Rows(4).Insert
a = a + 1
End If
End If
Next 'And here too

Loop through listbox rows if column is not empty copy paste to empty row in sheet

I'm trying to loop through listbox(attached picture below), and if the column(3) has value, then copy & paste row to empty row in excel sheet for later use.
I wanted to copy the row using column value to put it in sheet, but it just copys last row value, and repeats.
Could you please point out what I did wrong in the below code?
TIA
s = 22
For i = 0 To Me.AnBox.ListCount - 1
If Me.AnBox.Column(3) <> "" Then
Sheets("SparePartsRequestForm").Range("A" & s).Value = Me.AnBox.Column(2)
Sheets("SparePartsRequestForm").Range("C" & s).Value = Me.AnBox.Column(1)
Sheets("SparePartsRequestForm").Range("D" & s).Value = Me.AnBox.Column(3)
s = s + 1
End If
Next i
Userform
Part of excel sheet
Few errors:
The index of your ListBox.Column is zero based, so you not looking at the third but second Index.
You are accessing the values wrongly, trying to read a full column so it seems. The correct syntax is expression.Column(pvargColumn, pvargIndex) so you are missing a parameter. Check the documentation remarks to see the difference between using the second parameter or not.
Make use of the iteration and the more common List property to access each row individually.
So therefor your code could look like:
s = 22
For i = 0 To Me.AnBox.ListCount - 1
If Me.AnBox.List(i, 2) <> "" Then
Sheets("SparePartsRequestForm").Range("A" & s).Value = Me.AnBox.List(i, 1)
Sheets("SparePartsRequestForm").Range("C" & s).Value = Me.AnBox.List(i, 0)
Sheets("SparePartsRequestForm").Range("D" & s).Value = Me.AnBox.List(i, 2)
s = s + 1
End If
Next i
It is possible through the Column property too though:
s = 22
For i = 0 To Me.AnBox.ListCount - 1
If Me.AnBox.Column(2, i) <> "" Then
Sheets("SparePartsRequestForm").Range("A" & s).Value = Me.AnBox.Column(1, i)
Sheets("SparePartsRequestForm").Range("C" & s).Value = Me.AnBox.Column(0, i)
Sheets("SparePartsRequestForm").Range("D" & s).Value = Me.AnBox.Column(2, i)
s = s + 1
End If
Next i
Note: If your intention is to paste at the next available row, there are great ways to return the last used row of a range. See here for example

For IF Else loop not stopping when cells are empty

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

=Sum Formula VBA. Need loop

Spreadsheet
I am trying to insert a new sum formula into column P that sums the corresponding values in column S. This process will repeat over as you will see in the image of my spreadsheet I have attached. Any ideas for creating a loop to do this?
Update: I figured out a loop that will do this here is my code.
Sub COMPLETE()
TotalRow = 0
SumEnd = 0
For cur_row = 3 To 174
If IsEmpty(Cells(cur_row, 19)) = True Then
If cur_row = 3 Then
TotalRow = 3
Counts = -1
Else
Cells(TotalRow, 25) = "=SUM(S" & (SumEnd - Counts) & ":S" & SumEnd & " )"
TotalRow = cur_row
Counts = -1
End If
Else:
Counts = Counts + 1
SumEnd = cur_row
End If
Next cur_row
End Sub
You can use pivot table to resume your data in that way:
Pivot table
Configurations
Is that what you mean? or did you mean a VBA code?
Regards
For the example you gave, put this formula in cell P3 and fill down:
=IF(LEN(S3)=0,SUM(INDIRECT("R"&ROW()+1&"C"&COLUMN(S3)&":R"&ROW()+MATCH(TRUE,INDEX(S4:S$17="",0),0)-1&"C"&COLUMN(S3),FALSE)),"")
Note that if your data extend past row 16, you need to change "S$17" to one cell below the last cell in column S.

Find duplicates in excel workbook

I have a excel workbook that i want to find all duplicate rows across all the sheets and highlight them. Figured out how todo it in one sheet. Anyone that can help me out?
it's only a starting point...
For b = 1 To Sheets.Count - 1
For e = 1 To 9000
yy = Sheets(b).Range("A" & e).Value
If yy = "" Then Exit For
For k = b + 1 To Sheets.Count
For i = 1 To 9000
If Sheets(k).Range("A" & i).Value = yy Then
Sheets(k).Range("A" & i).Interior.Color = 65500
Sheets(b).Range("A" & e).Interior.Color = 1265500
End If
If Sheets(k).Range("A" & i).Value = "" Then Exit For
Next
Next
Next
Next
This function check between all the sheets, only the column "A" if there are no blanks. Set different color of the first occurrance and the others... If you have a big XLS or the searching criteria are more deeply (check all the cells of a sheets with AND OR NOT ...) the prg can use a lot of time...

Resources