Type mismatch error on using named range

Monicasinha

Board Regular
Joined
Dec 26, 2022
Messages
51
Office Version
  1. 365
Platform
  1. 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
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
VBA Code:
DSheet.Range("FirstMonth_col")
is a range statement yet you have got that within cells(i,***) statement which needs a long at that point so try changing it to:
VBA Code:
DSheet.Range("FirstMonth_col").column
do the same for the last month column as well
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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