Excel looop

hi my friends. I need help with a loop I have two variables kod1 and kod2 now I want to create a loop and if and I want to look at all columns if the two variables are in one column in excel then write the end column risk

Hello @a.hezardastan, try using VB:

Sub SearchAndWriteRisk()
Dim ws As Worksheet
Dim lastColumn As Long
Dim i As Long
Dim foundKod1 As Boolean
Dim foundKod2 As Boolean

' Set the worksheet where you want to perform the search
Set ws = ThisWorkbook.Sheets("YourSheetName")

' Define the values to search for
Dim kod1 As String
Dim kod2 As String
kod1 = "YourKod1Value"
kod2 = "YourKod2Value"

' Determine the last column in the worksheet
lastColumn = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column

' Initialize flags
foundKod1 = False
foundKod2 = False

' Loop through each column
For i = 1 To lastColumn
    ' Check if kod1 exists in the current column
    If WorksheetFunction.CountIf(ws.Columns(i), kod1) > 0 Then
        foundKod1 = True
    End If
    
    ' Check if kod2 exists in the current column
    If WorksheetFunction.CountIf(ws.Columns(i), kod2) > 0 Then
        foundKod2 = True
    End If
    
    ' If both kod1 and kod2 are found in the current column, write "risk" in the end column
    If foundKod1 And foundKod2 Then
        ws.Cells(1, lastColumn + 1).Value = "risk"
        Exit For ' Exit the loop if both are found
    End If
Next i

' Clean up
Set ws = Nothing

End Sub

Cheers! :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.