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:

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

cizzett

Board Regular
Joined
Jan 10, 2019
Messages
121
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]
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
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
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
I'm glad to help you. Thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,108,678
Messages
5,524,241
Members
409,566
Latest member
MickB

This Week's Hot Topics

Top