How can I save a range, for example C3:C20, to a dynamic array?
Dim XVal as Double
Redim XV(1 To 100)
XVal = Worksheets(“Sheet1”).Range(C3:C20)
Note that the range always starts at row 3, and can have an unknown number of rows.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Dynamics array (2002/SP3)
Thanks Mike. However, I get a “Type Mismatch” error. Any ideas why?
This is part of my code:
Dim XVal() As Double, strSheetName as String
strSheetName = “Sheet1”
With Worksheets(strSheetName)
ReDim XValues(1 To 100)
XVal = .Range(“c3:C” & Cells(1, 3).End(xlDown).Row) ‘ << error occurs at this line
You can search and sort directly on worksheets – look up the Find and Sort methods of the Range object in the Excel VBA help.
You could do the following:
Dim m As Long
Dim r As Long
m = Range(“C3”).End(xlDown).Row
ReDim XVal(1 To m – 2) As Double
For r = 3 To m
XVal(r – 2) = Range(“C” & r).Value
Next r
You may be able to adapt the methodology of the following. It assigns the data in C3 through the end of the continuous range and then moves those numbers to the J column. It then displays the sum of the numbers in the C3:C? range in a message box.
Sub addToArray() Dim i As Long, xVal() As Double, cellCount As Long, xRange As Range Set xRange = Worksheets("Sheet1").Range("c3:C" & Cells(1, 3).End(xlDown).Row) cellCount = xRange.Count ReDim xVal(1 To cellCount - 1) For i = 1 To cellCount - 1 xVal(i) = xRange(i) Next For i = 1 To UBound(xVal) Cells(i, 10) = xVal(i) Next MsgBox Application.WorksheetFunction.Sum(xVal) End Sub
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications