Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,

I have a problem in my for loop. I want to loop through each excel row in my excel table and find a value in specific column, when I find the value I edit this row's 25th column.

For this purpose I wrote a VBA code. But when I run it, the for loop not fill the cells one by one. I attached the screen shot of my columns when I run the macro.

Thanks in advance.

Link of screen-shot : Link[^]

Edit: I found out that, the table contains 156 rows but in for loop after 86th row the cell value gets empty. Why is that?

What I have tried:

VB
Private Sub FindAndReplaceButton_Click()
    Dim s As String
    s = WorkCommandNumbersInputField.Value
    Dim wcns() As String
    wcns = Split(s, ",")
    
    Dim wrksht As Worksheet
    Dim objListObj As ListObject
    Dim objListRows As ListRows
    
    Set wrksht = Application.ActiveSheet
    Set objListObj = wrksht.ListObjects(1)
    Set objListRows = objListObj.ListRows
    
    Dim rowCount As Integer
    rowCount = objListRows.Count
      
    For Each wcn In wcns
        For i = 1 To rowCount
            Dim FindedRow As Range
            Set FindedRow = objListObj.ListRows(i).Range
            Dim FindedCell As Range
            Set FindedCell = FindedRow.Cells(i, 2)
            If FindedCell.Value = wcn Then
                Dim ChangedCell As Range
                Set ChangedCell = FindedRow.Cells(i, 25)
                If (ChangedCell.Value = "") Or (ChangedCell.Value = "---") Then
                    ChangedCell.Value = ShelfNumberInputField.Value
                ElseIf ChangedCell.Value <> ShelfNumberInputField.Value Then
                    ChangedCell.Value = ChangedCell.Value + "-" + 
                    ShelfNumberInputField.Value
                End If
            End If
        Next i
    Next
End Sub
Posted
Updated 30-May-19 1:41am
v4
Comments
Graeme_Grant 7-Sep-17 5:41am    
any reason why you just don't use the built-in Excel If(...) function in the 25th column of each row?
Onur ERYILMAZ 7-Sep-17 5:51am    
Because If function always sets the cell 'False' or 'Empty', what I wanted to do if the condition is not match, do nothing. So built in if function does not offer otherwise do nothing option.
Graeme_Grant 7-Sep-17 5:58am    
Then you don't understand Excel. For example:
=IF(B7>15, "Big", "Small")

If B7 = 10, then the cell will contain "Small", if B7 = 20 then the cell will contain "Big".

You can take it a step further and use cell reference for the true/false states:
=IF(B7>15, D4, C4)
Onur ERYILMAZ 7-Sep-17 6:11am    
Bu the problem is I don't want to write "Small", I want this =IF(B7>15, "Big", otherwise leave the cell unthouched)
Graeme_Grant 7-Sep-17 6:13am    
Use a null string then, simple!

1 solution

When your code don't behave as you expect, use the debugger to see what it does, you will know the why by inspecting the variables.
Quote:
I found out that, the table contains 156 rows but in for loop after 86th row the cell value gets empty. Why is that?

Since your code do not depend on the row number, your code behavior depend on the sheet contain. You are the only one that can do something.
-----
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Debugging in Excel VBA - EASY Excel Macros[^]
MS Excel 2013: VBA Debugging Introduction[^]
How to debug Excel VBA - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900