VBA range reference

on_an_island

New Member
Joined
Feb 8, 2018
Messages
2
Hello,


I'm running a knapsack type of problem with my data located in multiple columns (i.e., A2:A10, B2:B:10, etc.) and can't figure out the correct argument to use that references the value in a cell for the algorithm to analyze and select or leave out. As currently configured, I can get the algorithm to work if I use the Range("cell").value argument. However, this only references the value in that specific cell and does not work on a range when used like this: Range("A1:A10").value. Manually entering a reference for each cell would be too time consuming and make for very messy code. I'm looking for an argument that will read cells in a specified range (A1:A10) and reference the value in each respective cell as the algorithm needs to crunch each cells value and compare it to others (see sample of code below).


Dim limit As Double, weight As Double,
Dim i, j As Integer

Dim weighti, weightj As Integer
Dim valuei, valuej As Integer


weighti = Range("C2").value <=== Range("C2:C10") will not work returning Run-Time Error '13' Type Mismatch error
weightj = Range("C3").value


valuei = Range("D2").value
valuej = Range("D3").value


For i = 0 To 1
weight = weighti * i + weightj * j <===number crunching based on referenced cell value
value = valuei * i + valuej






Any thoughts are appreciated!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Welcome to the board

First things first, when you write
Code:
dim i,j as integer
you're only declaring j as an integer, i is being declared but only as a variant (the default type). You need to write
Code:
dim i as integer, j as integer

In this case if you've done the same thing with weighti, which you want to be an integer value, but it can't be if you're referring to multiple cells. The only way it can be is to loop through them one at a time

So you'll need an extra loop, or to reconfigure the existing one:
Code:
dim x as integer
for x = 2 to 10
    weighti = cells(x,3).value
    weightj = cells(x+1,3).value
    valuei = cells(x,4).value

etc etc
If you don't want to use CELLS(..).value you can use e.g. RANGE("C" & x).value
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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