VBA - apply formula and paste

omnivl

Board Regular
Joined
Aug 25, 2014
Messages
53
I have the following data in A1 that has been pasted from another sheet

Category
Description
Weight
Points
Vendor Score
Criteria
Description of item 1
6%
10
8
Criteria
Description of item 2
6.5%
10
7
Criteria
Description of item 3
10%
10
6
Criteria
Description of item 4
7%
10
5
Totals

<tbody>
</tbody>

In VBA i would like to apply the formula (Vendor Score * Weight) * Points and update the Vendor Score so it looks like so

Category
Description
Weight
Points
Vendor 1 Score
Criteria
Description of item 1
6%
10
4.8%
Criteria
Description of item 2
6.5%
10
4.55%
Criteria
Description of item 3
10%
10
6%
Criteria
Description of item 4
7%
10
3.5%
Totals

<tbody>
</tbody>

The other issue i have is that in Category it could be 4 or 10 or x number of criteria but the last row will always be Totals, likewise with Vendor 1 Score, there could be a number of vendors so Vendor 1 Score, Vendor 2 Score in columns ....

Any help would be awesome...my head hurts
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Assuming data in columns A:E, headers in row 1

Try in a copy of your workbook

Code:
Sub aTest()
    Dim lastRow As Long
    
    lastRow = Cells(Rows.Count, "E").End(xlUp).Row
    With Range("E2:E" & lastRow)
        .Value = Evaluate(Replace("=C2:C@*D2:D@*E2:E@/100", "@", lastRow))
        .NumberFormat = "0.00%"
    End With
End Sub

Hope this helps

M.
 
Upvote 0
Assuming data in columns A:E, headers in row 1

Try in a copy of your workbook

Code:
Sub aTest()
    Dim lastRow As Long
    
    lastRow = Cells(Rows.Count, "E").End(xlUp).Row
    With Range("E2:E" & lastRow)
        .Value = Evaluate(Replace("=C2:C@*D2:D@*E2:E@/100", "@", lastRow))
        .NumberFormat = "0.00%"
    End With
End Sub

Hope this helps

M.

Thats great this does column e, but as above "likewise with Vendor 1 Score, there could be a number of vendors so Vendor 1 Score, Vendor 2 Score in columns ...."
is there a way i could incorporate

lastcol = Cells(Columns.Count, "E").End(xlUp).Column

and change the range
 
Upvote 0
Thats great this does column e, but as above "likewise with Vendor 1 Score, there could be a number of vendors so Vendor 1 Score, Vendor 2 Score in columns ...."
is there a way i could incorporate

lastcol = Cells(Columns.Count, "E").End(xlUp).Column

and change the range

Try to provide a data sample with more columns along with expected results.

M.
 
Upvote 0
Try to provide a data sample with more columns along with expected results.

M.

Category
clip_image002.png
Description

<tbody>
</tbody>
Criteria weightingPoints availableVendor 1Vendor 2Vendor 3
TBCTBC6.67%101077
TBCTBC6.67%101088
TBCTBC6.66%101055
TBCTBC5.00%101055
TBCTBC5.00%10101010
TBCTBC5.00%1010105
TBCTBC10.00%10101010
TBCTBC2.00%101000
TBCTBC1.00%101000
TBCTBC1.00%10101010
TBCTBC1.00%101000
TBCTBC50.00%-
Totals-100.00%-1106560

<colgroup><col><col><col span="2"><col span="2"><col></colgroup><tbody>
</tbody>


the result (however at the moment it is not updating vendor 2 and vendor 3)


CategoryDescriptionCriteria weightingPoints availableVendor 1Vendor 2Vendor 3
TBCTBC6.67%106.67%77
TBCTBC6.67%106.67%88
TBCTBC6.66%106.66%55
TBCTBC5.00%105.00%55
TBCTBC5.00%105.00%1010
TBCTBC5.00%105.00%105
TBCTBC10.00%1010.00%1010
TBCTBC2.00%102.00%00
TBCTBC1.00%101.00%00
TBCTBC1.00%101.00%1010
TBCTBC1.00%101.00%00
TBCTBC50.00%-
Totals-100.00%-0.56560

<colgroup><col style="width:98pt" width="131"> <col style="width:239pt" width="319"> <col style="width:77pt" span="2" width="103"> <col style="width:79pt" span="3" width="105"> </colgroup><tbody>
</tbody>

Code

Code:
Option Explicit
Private Sub CommandButton1_Click()
    Dim c
    Dim lastRow As Long
    Worksheets("scoring").Range("A1:ZV10000").Clear
    Worksheets("scoring").Range("A1:ZV10000").ClearFormats
    
    For Each c In Range("A1:IV1").Cells
        If c = "" Then
        Columns(1).Resize(, c.Column - 1).EntireColumn.Copy Destination:=Sheets("scoring").Columns(1)
            Exit For
        End If
    Next
    Worksheets("scoring").Activate
    lastRow = Cells(Rows.Count, "E").End(xlUp).Row
    With Worksheets("scoring").Range("E2:E" & lastRow - 2)
        .Value = Evaluate(Replace("=C2:C@*D2:D@*E2:E@/100", "@", lastRow))
        .NumberFormat = "0.00%"
    End With
    Worksheets("eval").Activate
End Sub


Thanks for the help
 
Upvote 0
Something like this...

Code:
Sub aTest()
    Dim firstCol As Long, lastCol As Long, lastRow As Long
    Dim i As Long
    
    With Sheets("scoring")
        'Define column of the first vendor
        firstCol = 5
        'Get last column
        lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        
        'loop through columns
        For i = firstCol To lastCol
            'Get lastrow with data of column i
            lastRow = .Cells(.Rows.Count, i).End(xlUp).Row - 2
            
            With Range(.Cells(2, i), .Cells(lastRow, i))
                .Value = Evaluate(Replace("=C2:C@*D2:D@*" & .Address & "/100", "@", lastRow))
                .NumberFormat = "0.00%"
            End With
        Next i
    End With
End Sub

M.
 
Upvote 0
The code above needs a small adjustment. Try...

Code:
Sub aTest()
    Dim firstCol As Long, lastCol As Long, lastRow As Long
    Dim i As Long
    
    With Sheets("scoring")
        'Define column of the first vendor
        firstCol = 5
        'Get last column
        lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        
        'loop through columns
        For i = firstCol To lastCol
            'Get lastrow with data of column i
            lastRow = .Cells(.Rows.Count, i).End(xlUp).Row - 2
            
            With Range(.Cells(2, i), .Cells(lastRow, i))
                .Value = [B]Sheets("scoring")[/B].Evaluate(Replace("=C2:C@*D2:D@*" & .Address & "/100", "@", lastRow))
                .NumberFormat = "0.00%"
            End With
        Next i
    End With
End Sub


M.
 
Upvote 0
The code above needs a small adjustment. Try...

Code:
Sub aTest()
    Dim firstCol As Long, lastCol As Long, lastRow As Long
    Dim i As Long
    
    With Sheets("scoring")
        'Define column of the first vendor
        firstCol = 5
        'Get last column
        lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        
        'loop through columns
        For i = firstCol To lastCol
            'Get lastrow with data of column i
            lastRow = .Cells(.Rows.Count, i).End(xlUp).Row - 2
            
            With Range(.Cells(2, i), .Cells(lastRow, i))
                .Value = [B]Sheets("scoring")[/B].Evaluate(Replace("=C2:C@*D2:D@*" & .Address & "/100", "@", lastRow))
                .NumberFormat = "0.00%"
            End With
        Next i
    End With
End Sub


M.

You sir, are a legend.....thanks works perfect!
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,893
Members
449,097
Latest member
dbomb1414

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