VBA to run code in different worksheets and run multiple calculations

JimCorb

New Member
Joined
Jan 11, 2016
Messages
6
Dear Friends,
I'm stuck up with interesting but challenging problem and am looking for some guidance on how to fix the problem. Pls allow me try to explain with best possible details.

I have 4 worksheets namely Buildings, Scoring, Costing and Report.

I need to capture user requirements, then match which of my products in raw data matches it. Once I identify it then I've to do Costing calculation and produce the Report as output.

Here's how I'm approching this coding situation.

My BUILDINGS worksheet acts as my raw data and list s different features of all the buildings separately.
Once I capture user requirements then I've to compare it with my raw data in BUILDINGS sheet and shorlist different buildings for my recommendations.
I shortlist this by using SCORING sheet which gives a score of 1 for every matched feature and 0 for unmatched feature against all buildings. Total Score in Row 20 in SCORING sheet helps identify qualified buildings. I've done this part already .For example total score of 8 for a building means it matches the user requirements. Here in my sample the Buildings 1, 6 and 14 are the shortlisted buildings..

These shortlisted buildings need to undergo COSTING sheet one by one.
The COSTING sheet has some embedded formulas from builder which needs some fields from User inputs and then applies the related costs for offering to customer. So my macro needs to run in a loop for Each Qualified Building . For any building, It pastes the user parameters in cells B6 to B8 (shown in GREY cells) . This will result in final costing values to appear in cells B 9 to B14. These Final values along with Building Name needs to be captured in REPORT worksheet.

So if I got THREE buildings as qualified (which matches score of 8) then my output in REPORT worksheet should show all those three buildings along with related costs.

I'm trying to do this in 3-parts.
Part 1 - Identify qualifying buildings. This is done.
Part 2 - Copy only shortlisted buildings along with their features at the bottom of SCORING sheet. Range B25 onwards. This is stuck with errors.
Part -3 Pick up only required parametrs and paste in Costing sheets. This part I need to figuer out best way to setup some Loops.

Problems I'm facing are in:
Part 2 :
Some of my codes run individually when I put them in different sheets and while those sheets are active. In real scenario I've to run them ffrom central module.
But I'm getting errors in referencing to different worksheets through my code in module. I want my code to go to some reference cell for example in Cell A1 in BUILDINGS and then proceed to OFFSET location for copying the required cells. I get this error whenever my code referes to another worksheet within same workbook.
"Run Time error 1004 Activate method of Range Class failed"

Part 3:
What's the best way to pick the required fields from inputs and retrieve the corresponding Costing outputs and paste it in REPORTS ?


Here's my Code:

Option Explicit




Sub ListingQualifiedBuildings()

Dim wb As Workbook
Set wb = ActiveWorkbook

Dim wsBuildings As Worksheet, wsScoring As Worksheet, wsCosting As Worksheet, wsReport As Worksheet

Dim score As String
Dim counter As Long
Dim QualifyingScore As String

Set wsScoring = wb.Worksheets("Scoring")
wsScoring.Range("b20").Activate

'Declare & CORRECT the total qualifying score here


QualifyingScore = 8

Range("B20").Select
counter = 1
Do Until ActiveCell.Value = ""

Range("B20").Select ' required again as each offset is counting from B20

ActiveCell.Offset(0, counter).Select
'Setting limits for counter run
If ActiveCell.Value = "" Then
Exit Sub
Else
End If
score = ActiveCell.Value

If score = QualifyingScore Then

Call RangeSelection

Else
End If
counter = counter + 1

Loop


End Sub








Sub RangeSelection()

Dim wb As Workbook
Set wb = ActiveWorkbook

Dim wsBuildings As Worksheet, wsScoring As Worksheet, wsCosting As Worksheet, wsReport As Worksheet

Dim countColumnLocation As Long

Set wsBuildings = wb.Worksheets("Buildings")
Set wsScoring = wb.Worksheets("Scoring")

'setting temporary counter to know how many columns to offset. It should get column location from qualifying building column in Scoring sheet

countColumnLocation = 2

wsBuildings.Range("a1").Offset(0, countColumnLocation).Activate
wsBuildings.Range(Selection, Selection.End(xlDown)).Select
Selection.Copy


'Above copied selection should paste in Scoring sheet at location A25 and in its subsequent Righ columns.
'For example if qualified scores appear in column C,H,J in Scoring Sheet which reflects Building 1, 6 & 14.
'Then actual Building's features for Building 1,6 & 14 will be copied from Range B25,C25 and D25


wsScoring.Range("a25").Activate

Do Until ActiveCell.Value = ""

ActiveCell.Offset(0, 1).Select
Loop


Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

' End With

End Sub

Excel Screens are here for ref...


Excel 2007
ABCDEFGHI
1Building Offer / CriteriaBuilding1Building2Building3Building4Building5Building6Building…Building20
2Minimum Price (US$)$2,000$3,000$4,000$5,000$1,500$2,500$3,500$4,000
3Maximum Price (US$)$3,800$4,800$5,800$6,800$3,300$4,300$5,300$5,800
4Currency (USD, GBP, EUR, CNY)USDGBPUSDEURCNYUSDUSDUSD
5Urgency (Lo/Med/Hi/NA)LoMedMedMedHiHiMedMed
6Inclusion {Multiple Selections }( eg.. High Floor, Sea Facing, Security, Private Lawn, NA)High FloorSea FacingPrivate LawnHigh FloorSecurityPrivate LawnSea Facing
7Exclusion {Multiple Selections } ( eg..Mountain Facing, No Lift, Security)Mountain Facing No LiftMountain Facing No LiftMountain FacingMountain FacingMountain Facing No Lift, Security
8Sr Citizen Discount (Y/N)YYYNNNNN
9Protected Rental (Y/N)YNNYYNNN
10Minimum Contract (1,2,3,4 Yrs)12334211
11Security Deposit (20%, 30%, 40% depending on No. of Contract years)20%40%30%20%30%30%30%30%
12Distance from Mall12NA1NA3NA2
13Metro station Proximity (Y/N)YNNYYNNY
14Bus Stop ProximityNYYYNYNN
15School ProximityYNYYYNYN
16Cinema Proximity (Y/N/NA)NAYNNANANNN
17Hospital (<1 Km, 1-3 KM, >5KM)1-3 KM1 KM>5KM1 KM>5KM1-3 KM1 KM>5KM
18Misc Building featuresShopsCinemaParty Hall
19Misc Building featuresKindergartenShopsShops
20Misc Building featuresDance Room
21Misc Building featuresYoga
22Misc Building featuresBadminton
23Misc Building featuresSquash

<tbody>
</tbody>
Buildings



Scoring Sheet

Excel 2007
ABCDEFGHI
1Ref CodeBuilding Offer / CriteriaBuilding1Building2Building3Building4Building5Building6Building…7
21Minimum Price (US$)
32Maximum Price (US$)1000000
43Currency (USD, GBP, EUR, CNY)0010111
54Risk Appetite (Lo/Med/Hi/NA)1001111
65Inclusion {Multiple Selections }( eg.. High Floor, Sea Facing, Security, Private Lawn, NA)1011110
76Exclusion {Multiple Selections } ( eg..Mountain Facing, No Lift, Security)0011111
87Sr Citizen Discount (Y/N)1111111
98Protected Rental (Y/N)1001011
109Minimum Contract (1,2,3,4 Yrs)0100000
1110Security Deposit (20%, 30%, 40% depending on No. of Contract years)1111111
1211Distance from Mall0000000
1312Metro Station Proximity (Y/N)1000001
1413Bus Stop Proximity1000110
1514School Proximity0000000
1615Cinema Proximity (Y/N/NA)0000000
1716Hospital (<1 Km, 1-3 KM, >5KM)0000000
18
19
20Total8356787
21
22
23
24
25Building Offer / CriteriaBuilding1Building6Building…14
26Minimum Price (US$)$2,0002500
27Maximum Price (US$)$3,80043000
28Currency (USD, GBP, EUR, CNY)USDUSD1
29Urgency (Lo/Med/Hi/NA)LoHi1
30Inclusion {Multiple Selections }( eg.. High Floor, Sea Facing, Security, Private Lawn, NA)High Floor1
31Exclusion {Multiple Selections } ( eg..Mountain Facing, No Lift, Security)Mountain FacingMountain Facing1
32Sr Citizen Discount (Y/N)YN1
33Protected Rental (Y/N)YN1
34Minimum Contract (1,2,3,4 Yrs)120
35Security Deposit (20%, 30%, 40% depending on No. of Contract years)20%0.31
36Distance from Mall131
37Station Proximity (Y/N)YN0
38Bus Stop ProximityNY0
39School ProximityYN0
40Cinema Proximity (Y/N/NA)NAN0
41Hospital (<1 Km, 1-3 KM, >5KM)1-3 KM1-3 KM0
42Misc Building features
43Misc Building features
44Misc Building features
45Misc Building features
46Misc Building features
47Misc Building features

<tbody>
</tbody>
Scoring




Costing Sheet

Excel 2007
ABCDE
5Building Name (from qualified Building details)Building 1
6Minimum Price (US$) (from qualified Building details)1500
7Metro station Proximity (Y/N)Y
8Inclusion {Multiple Selections }( eg.. High Floor, Sea Facing, Security, Private Lawn, NA) (from qualified Building details)High Floor
9Basic Cost {from Builder's Calculation Parameters)5500Fixed for building in B5
10Location Factor (%) {from Builder's Calculation Parameters)440Fixed for building location
11Cost Overhead % Charges due to Inclusions {from Builder's Calculation Parameters)68.75Provided by builders (% of Basic Cost)
12Fixed Maintenance Charges {from Builder's Calculation Parameters)550Fixed $ Amt
13
14Total6558.75

<tbody>
</tbody>
Costing



Final Report

Excel 2007
ABCD
1Buildings NamesBuilding 1Building 6 Building 10
2Basic Cost350030005500
3Location Factor (%)280240440
4Cost Overhead % Charges due to Inclusions43.7537.568.75
5Fixed Maintenance Charges350300550
6
7Total4173.753577.56558.75

<tbody>
</tbody>
Report
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Yippee!:rolleyes: I just resolved part 2 also. Now I seek guidance on Part 3 of my problem pls..pls.pls...
 
Upvote 0

Forum statistics

Threads
1,214,886
Messages
6,122,093
Members
449,064
Latest member
Danger_SF

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