VBA VLookup to not override data if run multiple times

spared

New Member
Joined
Sep 16, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Long time reader, first time poster.

I am trying to implement a macro for a journal upload that has specific posting requirements. The VLookUp works as intended (returns the format of the journal entry - code below) but overrides any values that may have been entered after. Is it possible to have those lines skipped if the macro is run again? IE the end-user adds another GL after setting up their entry, the macro can be re-rerun and leave those cells with values as is.

Snip shows column E with the user input and column F after macro has been run with F12 updated. Desired output would be to have those cells remain intact and cells F13,17,18 etc. filled in if run again.

I have been Googling and saw a few of Floof's suggestions but I am not clever enough to modify them for my own needs since I'm still in the beginning stages of learning how to even write this! 😅

Entry.PNG

VBA Code:
Sub VLookUp()

    Dim Sheet1Ws As Worksheet, ListWs As Worksheet
    Dim ListLastRow As Long, Sheet1LastRow As Long, x As Long
    Dim DataRng As Range
 
    Set Sheet1Ws = ThisWorkbook.Worksheets("Sheet1")
    Set ListWs = ThisWorkbook.Worksheets("List")
 
    ListLastRow = ListWs.Range("A" & Rows.Count).End(xlUp).Row
    Sheet1LastRow = Sheet1Ws.Range("A" & Rows.Count).End(xlUp).Row
 
    Set DataRng = ListWs.Range("A2:H" & ListLastRow)
 
    For x = 2 To Sheet1LastRow
        On Error Resume Next
        Sheet1Ws.Range("F" & x).Value = Application.WorksheetFunction.VLookUp( _
            Sheet1Ws.Range("E" & x).Value, DataRng, 8, False)
 
 
    Next x
 
 
End Sub

Thank you for reading this and any help offered!!!!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
@spared Welcome.
Is this what you are after? NB: Not tested.

VBA Code:
Sub VLookUp()

    Dim Sheet1Ws As Worksheet, ListWs As Worksheet
    Dim ListLastRow As Long, Sheet1LastRow As Long, x As Long
    Dim DataRng As Range
 
    Set Sheet1Ws = ThisWorkbook.Worksheets("Sheet1")
    Set ListWs = ThisWorkbook.Worksheets("List")
 
    ListLastRow = ListWs.Range("A" & Rows.Count).End(xlUp).Row
    Sheet1LastRow = Sheet1Ws.Range("A" & Rows.Count).End(xlUp).Row
 
    Set DataRng = ListWs.Range("A2:H" & ListLastRow)
 
    For x = 2 To Sheet1LastRow
     
           On Error Resume Next
         'Only do if  F is blank
        If Sheet1Ws.Range("F" & x).Value = vbNullString Then
           Sheet1Ws.Range("F" & x).Value = Application.WorksheetFunction.VLookUp( _
               Sheet1Ws.Range("E" & x).Value, DataRng, 8, False)
        End If
 
    Next x
   
 Tidy up:
 On Error GoTo 0
End Sub
 
Upvote 0
Solution
@Snakehips Holy wow, thank you for your help!! I spent way too many hours googling and this seems much simpler than other solutions I saw.

Tested and worked on multiple adjustments. Truly, thank you!!!!
 
Upvote 0

Forum statistics

Threads
1,216,851
Messages
6,133,075
Members
449,777
Latest member
chrixpowell

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