Combine two VBAs into one

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Greetings I have two macros that work great using the VLOOKUP. The issue I have one will take the text from Column 8 and then the other from Column 9. Before I had a macro that removed all rows that had text in Column 8 and the other in Column 9 which worked great. However I need a VLOOKUP that will search the whole data base. Basically when there is text in Column 8 it will be blank in Column 9 and vice versa. Before would end up having two sheets of data. This new one will give me one. My Macros I hope to combine are below,

I think they can be combined but some sort of statement stating if there is nothing in Column 8 then to search in Column 9.

Thank you very much.


VBA Code:
Private Sub VLOOK_UP()
   Dim i As Long, n As Variant
    For i = 1 To Split(Worksheets("Schedule").UsedRange.Address, "$")(4)
         n = Right(Worksheets("Schedule").Cells(i, 8).Value, 4)
       If IsNumeric(n) Then n = CLng(n)
        
        Worksheets("Schedule").Cells(i, 1).Value = _
        Application.WorksheetFunction.VLookup(n, Worksheets("Codes").Range("A:B"), 2, 2)
    Next i
End Sub

second code:

VBA Code:
Private Sub VLOOK_UP2()
   Dim i As Long, n As Variant
    For i = 1 To Split(Worksheets("Schedule").UsedRange.Address, "$")(4)
         n = Right(Worksheets("Schedule").Cells(i, 9).Value, 4)
       If IsNumeric(n) Then n = CLng(n)
        
        Worksheets("Schedule").Cells(i, 1).Value = _
        Application.WorksheetFunction.VLookup(n, Worksheets("Codes").Range("A:B"), 2, 2)
    Next i
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Maybe like this:
VBA Code:
Private Sub VLOOK_UP()
   Dim i As Long, n As Variant
    For i = 1 To Split(Worksheets("Schedule").UsedRange.Address, "$")(4)
        n = IIf(Worksheets("Schedule").Cells(i, 8).Value <> "", Right(Worksheets("Schedule").Cells(i, 8).Value, 4), Right(Worksheets("Schedule").Cells(i, 9).Value, 4))
        If IsNumeric(n) Then n = CLng(n)
        
        Worksheets("Schedule").Cells(i, 1).Value = _
        Application.WorksheetFunction.VLookup(n, Worksheets("Codes").Range("A:B"), 2, 2)
    Next i
End Sub
 
Upvote 0
Solution
Maybe like this:
VBA Code:
Private Sub VLOOK_UP()
   Dim i As Long, n As Variant
    For i = 1 To Split(Worksheets("Schedule").UsedRange.Address, "$")(4)
        n = IIf(Worksheets("Schedule").Cells(i, 8).Value <> "", Right(Worksheets("Schedule").Cells(i, 8).Value, 4), Right(Worksheets("Schedule").Cells(i, 9).Value, 4))
        If IsNumeric(n) Then n = CLng(n)
       
        Worksheets("Schedule").Cells(i, 1).Value = _
        Application.WorksheetFunction.VLookup(n, Worksheets("Codes").Range("A:B"), 2, 2)
    Next i
End Sub
That is perfect! Thank you.
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,937
Members
449,094
Latest member
teemeren

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