Reforlulate Macro to accept incontinuous array

mpierce

New Member
Joined
Oct 25, 2005
Messages
29
This function accepts a range of cells such as A1:F1 just fine, but if I try to press the control key and select difference cells like A1,A5,A23,A43 and then use that as myRange it does not work. I understand this on a fundamental level because that is not a range per se, but how could I rewrite this finction so that it could take in arguments like A1,A5,A23,A43 ?

Could anyone also explain a bit about what exactly would need to change??

Thank you!!

Function PriceChange(myRange As Range) As Variant

Dim cell As Range
Dim LastValue As Double
Dim NextToLastValue As Double
Dim CellCount As Long

For Each cell In myRange
If Len(cell) > 0 Then
CellCount = CellCount + 1
If CellCount > 1 Then NextToLastValue = LastValue
LastValue = cell.Value
End If
Next cell

If CellCount > 1 Then
PriceChange = LastValue - NextToLastValue
Else
PriceChange = ""
End If

End Function
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Irrespective of whether they are contiguous or not, what do you expect the result to be when the argument contains more than 2 cells?
 
Upvote 0

Forum statistics

Threads
1,213,530
Messages
6,114,163
Members
448,554
Latest member
Gleisner2

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