VBA Code to Display Data by not using Vlookup

matt9man

New Member
Joined
Jan 4, 2011
Messages
22
Ok here is my problem I have vba code that first displays first row of expected results then on the next row the code does a vlookup using the key in the expected to find the actual results. Problem is the vlookup is being used for every column which takes to long. Ideally I want it to vlookup just my key then all cells to right to be populated by some column reference. In my code below i commented out and colored in red what I am picturing in my head. Also keep in mind the keys may have ../// in it. Thanks

Code:
Sub PopulateExp()
Dim intExpRow As Long
Dim intCompRow As Long
Dim intCol As Long
intExpRow = 3
intCompRow = 3
intCol = 1
Sheet3.Select
intRowCnt = Sheet4.Cells(1, 2)
Call ClearAllComp
While Sheet2.Cells(intExpRow, intCol) <> ""
   Sheet3.Cells(intCompRow, intCol) = "Expected"
   Sheet3.Cells(intCompRow, intCol + 1) = Sheet2.Cells(intExpRow, intCol + 1)
   Sheet3.Cells(intCompRow, intCol + 2) = Sheet2.Cells(intExpRow, intCol + 2)
   Sheet3.Cells(intCompRow, intCol + 3) = Sheet2.Cells(intExpRow, intCol)
 
   While (intCol <= intRowCnt)
   Sheet3.Cells(intCompRow, intCol + 4) = Sheet2.Cells(intExpRow, intCol + 3).Value
   intCol = intCol + 1
   Wend
 
   Sheet3.Rows(intCompRow).Select
 
    With Selection.Interior
        .ColorIndex = 34
        .Pattern = xlSolid
    End With
 
   Call PopulateAct(intCompRow)
 
   intCompRow = intCompRow + 2
   intExpRow = intExpRow + 1
 
 
intCol = 1
Wend
 
End Sub
Sub PopulateAct(intCompRow As Long)
Dim intExpRow As Long
Dim intCol As Long
intExpRow = 3
intCol = 1
intRowCnt = Sheet4.Cells(1, 2)
Sheet3.Select
   Sheet3.Cells(intCompRow + 1, intCol) = "Actual"
   Sheet3.Cells(intCompRow + 1, intCol + 1) = Sheet3.Cells(intCompRow, intCol + 1)
   Sheet3.Cells(intCompRow + 1, intCol + 2) = Sheet3.Cells(intCompRow, intCol + 2)
 
   Sheet3.Cells(intCompRow + 1, intCol + 3) = "=VLOOKUP(D" & intCompRow & ",Actual!A1:ZZ100000," & (intCol) & ",FALSE)"
    If IsError(Sheet3.Cells(intCompRow + 1, intCol + 3).Value) Then
      Sheet3.Cells(intCompRow + 1, intCol + 3) = "Actual Result Not Found"
      Sheet3.Cells(intCompRow + 1, intCol + 3).Font.ColorIndex = 5
      Sheet3.Cells(intCompRow + 1, intCol + 3).Font.Bold = True
      Else
        actualRow = "=row(VLOOKUP(D" & intCompRow & ",Actual!A1:ZZ100000," & (intCol) & ",FALSE))"
[COLOR=red]       'loop through columns[/COLOR]
[COLOR=red]       '   populate[/COLOR]
[COLOR=red]       '       looking to actual row with column indexes[/COLOR]
[COLOR=red]       '   if equal[/COLOR]
[COLOR=red]       '       do nothing[/COLOR]
[COLOR=red]       '   else[/COLOR]
[COLOR=red]       '       format difference[/COLOR]
[COLOR=red]       'end while[/COLOR]
        
        If Sheet3.Cells(intCompRow + 1, intCol + 3).Value = Sheet3.Cells(intCompRow, intCol + 3).Value Then
           While (intCol <= intRowCnt)
            Sheet3.Cells(intCompRow + 1, intCol + 4) = Sheet5.Cells(intExpRow, intCol + 1).Value
            intCol = intCol + 1 'to exit loop
           Wend
 
        Else
            While (intCol <= intRowCnt)
                Sheet3.Cells(intCompRow + 1, intCol + 3).Interior.ColorIndex = 40
                Sheet3.Cells(intCompRow + 1, intCol + 3).Font.ColorIndex = 5
                Sheet3.Cells(intCompRow + 1, intCol + 3).Font.Bold = True
                Sheet3.Cells(intCompRow + 1, 2).Value = "Difference"
                Sheet3.Cells(intCompRow, 2).Value = "Difference"
            Wend
        End If
   End If
   intCol = intCol + 1
 
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
you could turn off screen updating, calculation and events until its run, its probable from what i have read before that each formula is then calculated, hence taking too long
 
Upvote 0
Yea I have tried that before but still takes to long and also I get a overflow error (even when its set as Long) at 10,000. Thats why I want to populate by column indexes. Any ideas?
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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