excel vba Add check and then use different reference and source

cizzett

Board Regular
Joined
Jan 10, 2019
Messages
121
So I am using the below code and it works perfectly thanks to some help on here but I want to add a check so that if the row has data but the value of ws1 column G is empty then I want to get the value of ws1 G by using ws1 E as the reference and getting the value for G from ws2 column A. The matching reeference would be in ws2 column c to match

Code:
Sub GetSource()    Application.ScreenUpdating = False
    Dim Val As String, ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Sheets("Today")
    Set ws2 = Sheets("SOS")
    Dim i As Long, v1, v2
    v1 = ws1.Range("G3", ws1.Range("G" & Rows.Count).End(xlUp)).Resize(, 26).Value
    v2 = ws2.Range("A2", ws2.Range("A" & Rows.Count).End(xlUp)).Resize(, 7).Value
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(v2, 1)
            Val = v2(i, 1)
            If Not .Exists(Val) Then
                .Add Val, i + 2
            End If
        Next i
        For i = 1 To UBound(v1, 1)
            Val = v1(i, 1)
            If .Exists(Val) Then
                ws1.Cells(i + 2, "S") = ws2.Cells(ws2.Range("A:A").Find(Val).Row, "E")
                ws1.Cells(i + 2, "U") = ws2.Cells(ws2.Range("A:A").Find(Val).Row, "F")
                ws1.Cells(i + 2, "V") = ws2.Cells(ws2.Range("A:A").Find(Val).Row, "G")
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub

So I got this code to get the data I want but how can I incorporate it to only run this if G is blank but E is not blanks on ws1?

Code:
Sub GetSAPNum()    Application.ScreenUpdating = False
    Dim Val As String, ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Sheets("Today")
    Set ws2 = Sheets("SOS")
    Dim i As Long, v1, v2, v3
    v1 = ws1.Range("E3", ws1.Range("E" & Rows.Count).End(xlUp)).Resize(, 26).Value
    v2 = ws2.Range("C2", ws2.Range("A" & Rows.Count).End(xlUp)).Resize(, 7).Value
    v3 = ws1.Range("G3", ws1.Range("G" & Rows.Count).End(xlUp)).Resize(, 26).Value
            
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(v2, 1)
            Val = v2(i, 1)
            If Not .Exists(Val) Then
                .Add Val, i + 2
            End If
            
        Next i
        For i = 1 To UBound(v1, 1)
            Val = v1(i, 1)
        '    If .Exists(Val) Then
                ws1.Cells(i + 2, "G") = ws2.Cells(ws2.Range("A:C").Find(Val).Row, "A")
        '    End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Ok so let me reword my question,

I want to run the below code but I want to check if the cell in column G is empty and if it is then proceed otherwise go to the next.

Code:
[COLOR=#333333]ub GetSAPNum()    Application.ScreenUpdating = False[/COLOR]    Dim Val As String, ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Sheets("Today")
    Set ws2 = Sheets("SOS")
    Dim i As Long, v1, v2, v3
    v1 = ws1.Range("E3", ws1.Range("E" & Rows.Count).End(xlUp)).Resize(, 26).Value
    v2 = ws2.Range("C2", ws2.Range("A" & Rows.Count).End(xlUp)).Resize(, 7).Value
    v3 = ws1.Range("G3", ws1.Range("G" & Rows.Count).End(xlUp)).Resize(, 26).Value
            
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(v2, 1)
            Val = v2(i, 1)
            If Not .Exists(Val) Then
                .Add Val, i + 2
            End If
            
        Next i
        For i = 1 To UBound(v1, 1)
            Val = v1(i, 1)
        '    If .Exists(Val) Then
                ws1.Cells(i + 2, "G") = ws2.Cells(ws2.Range("A:C").Find(Val).Row, "A")
        '    End If
        Next i
    End With
    Application.ScreenUpdating = True [COLOR=#333333]End Sub[/COLOR]
 
Upvote 0
Try this:

Code:
            If ws1.Cells(i + 2, "G") = "" Then
                ws1.Cells(i + 2, "G") = ws2.Cells(ws2.Range("A:C").Find(Val).Row, "A")
            End If
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top