VBA Code with VLOOKUP Loop

ten1jmj

New Member
Joined
Jan 10, 2019
Messages
7
Thank you in advance for any assistance you can provide. I have a workbook containing worksheets named "Dashboard" and "January". I have the following code to lookup descriptions on the "January" sheet and return the subsequent value. My cumulative list on the Dashboard has all possible charges, however not each of the charges will be incurred each month, so when the code runs, it pulls the correct data for any charge listed on the January sheet, however for any line item not on the January sheet, the value returned comes from the row above. For example, January only has four charges for the month, however the Dashboard returns a value for all items in column 'D'. I would like for all column 'D' to have a blank on any line item without a charge for that current month.

Thank you,



Sub FindCount()

Dim Description As String
Dim Count As String
Dim i As Integer

Worksheets("DASHBOARD").Range("A3").Select
LastRow = Cells(Rows.Count, "A").End(xlUp).Row

For i = 3 To LastRow
Description = ActiveCell
On Error Resume Next
Count = Application.WorksheetFunction.VLookup _
(Description, Worksheets("January").Range("A20:B109"), 2, False)
On Error Resume Next

If IsError(Count) Then
ActiveCell.Offset(0, 3).Value = ""
Else
ActiveCell.Offset(0, 3).Value = Count

End If

ActiveCell.Offset(1, 0).Select

Next i

End Sub
 

Attachments

  • Dashboard.PNG
    Dashboard.PNG
    10.2 KB · Views: 8
  • January.PNG
    January.PNG
    8.8 KB · Views: 8

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi,

just add count = "" before your next i loop.

VBA Code:
Sub FindCount()

Dim Description As String
Dim Count As String
Dim i As Integer

Worksheets("DASHBOARD").Range("A3").Select
LastRow = Cells(Rows.Count, "A").End(xlUp).Row

For i = 3 To LastRow
Description = ActiveCell
On Error Resume Next
Count = Application.WorksheetFunction.VLookup _
(Description, Worksheets("January").Range("A20:B109"), 2, False)
On Error Resume Next

If IsError(Count) Then
ActiveCell.Offset(0, 3).Value = ""
Else
ActiveCell.Offset(0, 3).Value = Count

End If

ActiveCell.Offset(1, 0).Select

Count = ""

Next i

End Sub
 
Upvote 0
Great, glad we could help - thanks for the feedback

Rob
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,983
Members
449,092
Latest member
Mr Hughes

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