Monicasinha
Board Regular
- Joined
- Dec 26, 2022
- Messages
- 51
- Office Version
- 365
- Platform
- Windows
Hi
I am getting type mismatch error on using named ranges. Can you please help here.
A part of the vba code that I am using:
Dim DSheet As Worksheet
Dim LastRow As Long
Dim LastCol As Long
Dim LastCol1 As Long
Set DSheet = Worksheets("Ant")
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
LastCol1 = LastCol + 1
Dim dataArr() As Variant
ReDim dataArr(2 To LastRow, 1 To 1)
For i = 2 To LastRow
dataArr(i, 1) = WorksheetFunction.Sum(DSheet.Range(DSheet.Cells(i, DSheet.Range("FirstMonth_col")), DSheet.Cells(i, DSheet.Range("LastMonth_col"))))
Next i
DSheet.Range(DSheet.Cells(2, LastCol1), DSheet.Cells(LastRow, LastCol1)).Value = dataArr
DSheet.Cells(1, LastCol1).Value = "Total"
I am gettimg type mismatch error on the line
dataArr(i, 1) = WorksheetFunction.Sum(DSheet.Range(DSheet.Cells(i, DSheet.Range("FirstMonth_col")), DSheet.Cells(i, DSheet.Range("LastMonth_col"))))
Here "FirstMonth_col" and "LastMonth_col" are named ranges
I am getting type mismatch error on using named ranges. Can you please help here.
A part of the vba code that I am using:
Dim DSheet As Worksheet
Dim LastRow As Long
Dim LastCol As Long
Dim LastCol1 As Long
Set DSheet = Worksheets("Ant")
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
LastCol1 = LastCol + 1
Dim dataArr() As Variant
ReDim dataArr(2 To LastRow, 1 To 1)
For i = 2 To LastRow
dataArr(i, 1) = WorksheetFunction.Sum(DSheet.Range(DSheet.Cells(i, DSheet.Range("FirstMonth_col")), DSheet.Cells(i, DSheet.Range("LastMonth_col"))))
Next i
DSheet.Range(DSheet.Cells(2, LastCol1), DSheet.Cells(LastRow, LastCol1)).Value = dataArr
DSheet.Cells(1, LastCol1).Value = "Total"
I am gettimg type mismatch error on the line
dataArr(i, 1) = WorksheetFunction.Sum(DSheet.Range(DSheet.Cells(i, DSheet.Range("FirstMonth_col")), DSheet.Cells(i, DSheet.Range("LastMonth_col"))))
Here "FirstMonth_col" and "LastMonth_col" are named ranges