# VBA code with If and two Conditions



## Yeft (Jan 6, 2023)

Hello There!

Any one who can help me with the code I need?. I'm working with a spread sheet and I need to insert certain specific text in the Blank cells in Column G but 
only if two conditions are achieved:
1. Column D (Status Description). Items which are not "In Assembly" status.
2. Column G ( Crib bin ) cells are empty .
Then Insert comment " Specific text" in those empty cells in Column G. Please see the code below and suggest.










Thank you!


----------



## shinigamilight (Jan 6, 2023)

```
Sub nerd()
       
        Dim k As Integer
        Dim lr As Long
       
        lr = Range("D" & Rows.Count).End(xlUp).Row
       
        For k = 2 To lr
                If Range("D" & k) <> "In Assembly" And Range("G" & k) = "" Then
                    Range("G" & k) = "No Crib Bin Assigned"
                End If
        Next k

End Sub
```


----------



## Yeft (Jan 6, 2023)

Hi shinigamilight,

Thank you for your prompt support. I tried it and is giving me a Run time error "13" Type missmatch



Am I doing something wrong?


----------



## shinigamilight (Jan 6, 2023)

Yeft said:


> Hi shinigamilight,
> 
> Thank you for your prompt support. I tried it and is giving me a Run time error "13" Type missmatch
> View attachment 82245
> Am I doing something wrong?


You're pasting it in the sheet instead of module


----------



## Yeft (Jan 6, 2023)

Yes I did it cause when I pasted it in the Module it showed me the same error, now i put it back in the module and still does not work ( Same error).


----------



## shinigamilight (Jan 6, 2023)

Which line is giving error and can you tell me what's the value of lr by pressing F8 and running through every line step by step


----------



## shinigamilight (Jan 6, 2023)

```
Sub nerd()

        Dim k As Variant
        Dim lr As Long
       
        lr = Range("D" & Rows.Count).End(xlUp).Row
       
        For k = 2 To lr
                If Range("D" & k) <> "In Assembly" And Range("G" & k) = "" Then
                    Range("G" & k) = "No Crib Bin Assigned"
                End If
        Next k
        
End Sub
```
try this


----------



## Yeft (Jan 6, 2023)

Please See below:


----------



## Joe4 (Jan 6, 2023)

Do you have any errors in columns D or G?
That would cause the code to stop at that point.


----------



## shinigamilight (Jan 6, 2023)

If this doesn't work then idk

```
Sub Skip()

        Dim k As Variant
        Dim lr As Long
       
        lr = Range("D" & Rows.Count).End(xlUp).Row
       
        For k = 2 To lr
                If IsError(Range("D" & k)) Or IsError(Range("G" & k)) Then
                    GoTo NextIteration
                ElseIf Range("D" & k) <> "In Assembly" And Range("G" & k) = "" Then
                    Range("G" & k) = "No Crib Bin Assigned"
                End If
NextIteration:
        Next k
        
End Sub
```


----------



## Yeft (Jan 6, 2023)

Hello There!

Any one who can help me with the code I need?. I'm working with a spread sheet and I need to insert certain specific text in the Blank cells in Column G but 
only if two conditions are achieved:
1. Column D (Status Description). Items which are not "In Assembly" status.
2. Column G ( Crib bin ) cells are empty .
Then Insert comment " Specific text" in those empty cells in Column G. Please see the code below and suggest.










Thank you!


----------



## Yeft (Jan 6, 2023)

No errors in the excel file yet. The code simply does not work and shows that type of error. so macro does not execute..


----------



## Joe4 (Jan 6, 2023)

Try running this code and see what error message it returns:

```
Sub nerd()

        Dim k As Variant
        Dim lr As Long
      
        lr = Range("D" & Rows.Count).End(xlUp).Row
      
        On Error GoTo err_chk
        For k = 2 To lr
                If Range("D" & k) <> "In Assembly" And Range("G" & k) = "" Then
                    Range("G" & k) = "No Crib Bin Assigned"
                End If
        Next k
        On Error Resume Next
       
        Exit Sub

err_chk:
    MsgBox "Error code: " & Err.Number & vbCrLf & "Error on row: " & k
       
End Sub
```
Look at what row it says the error is on, and tell us exactly what is in columns D and G of that particular row.


----------



## Yeft (Jan 6, 2023)

Joe4 said:


> Try running this code and see what error message it returns:
> 
> ```
> Sub nerd()
> ...


Ok perfect!. Thank you so much!!. Give me some time and I’ll let you know what the results are.


----------



## Yeft (Jan 7, 2023)

Hello Shinigamilight and Joe4,

I tried on my laptop at home and what you guys suggested worked well. I'll check again when back to work. I don't really know if it is because my lap top has Office 2019, and my desktop @ work has Office 365. I'll try again on my desktop and see if changes work and I'll let you guys know.
Anyways I greatly appreciate the time you guys took in providing support.


----------

