Question posted 2010 · +2 upvotes
Would it be wrong if write the following code
Sub Something()
Dim i As integer
Dim xRange As Range
Dim yRange As Range
Set xRange= Range("x_table")
Set yRange= Range("y_table")
For i = 1 To xRange.Columns.Count
xRange.Columns(i) = Application.Sum(y_table.Columns(i))
Next i
End Sub
without specifically declaring each of the variables? Like bellow;
Sub Something()
Set xRange= Range("x_table")
Set yRange= Range("y_table")
For i = 1 To xRange.Columns.Count
xRange.Columns(i) = Application.Sum(y_table.Columns(i))
Next i
End Sub
Accepted answer +12 upvotes
If Option Explicit isn’t turned on you can do it that way, but I wouldn’t recommend it because then you’re relying on the framework to guess at the type of variable it is dealing with, which could cause unexpected results.
Excel VBA objects referenced (5)
Application— Using events with the Application objectApplication— Working with Other ApplicationsColumns.Count— Count function (Microsoft Access SQL)Columns.Count— Count the number of records in a DAO RecordsetRange— Refer to Cells by Using a Range Object
Top excel-vba Q&A (6)
- How to clear the entire array? +58 (2010)
- How to change Format of a Cell to Text using VBA +55 (2011)
- Download attachment from Outlook and Open in Excel +43 (2012)
- Can a VBA function in Excel return a range? +36 (2009)
- 2 Dimensional array from range +34 (2013)
- Hiding an Excel worksheet with VBA +33 (2009)
excel-vba solutions on this site
.