TotallyConfused
Board Regular
- Joined
- May 4, 2017
- Messages
- 247
- Office Version
- 365
- Platform
- Windows
The number of columns in the following data file shouldn’t change, however, there will be a new row of data added at the bottom every day. For this reason, I can’t manually create a range name that can be used with some Excel formulas such as ‘DSUM’ and perhaps others.
The code I have works great creating a range which I’ve called ‘Rng’ and places the address in a cell I’ve called ‘RngLoc’. In this case, RngLoc shows $A$1:$E$9. My problem is that when I go to Excel and try to enter the formula of =DSUM(RngLoc,5,J13:K14) all I get is the error message of ‘#VALUE!’ with a comment of ‘A value used in the formula is of the wrong data type’. I know my formula, my criteria and the data are okay, because if I enter the formula =DSUM(A1:E9,5,J13:K14) everything works great, so I’m assuming the problem is with the RngLoc.
I want to offer my THANKS in advance for any assistance you may be able to offer. I’m hoping to pick the brain of some of you experts to gain an answer to my problem.
<tbody>
</tbody>
The code I have works great creating a range which I’ve called ‘Rng’ and places the address in a cell I’ve called ‘RngLoc’. In this case, RngLoc shows $A$1:$E$9. My problem is that when I go to Excel and try to enter the formula of =DSUM(RngLoc,5,J13:K14) all I get is the error message of ‘#VALUE!’ with a comment of ‘A value used in the formula is of the wrong data type’. I know my formula, my criteria and the data are okay, because if I enter the formula =DSUM(A1:E9,5,J13:K14) everything works great, so I’m assuming the problem is with the RngLoc.
I want to offer my THANKS in advance for any assistance you may be able to offer. I’m hoping to pick the brain of some of you experts to gain an answer to my problem.
A | B | C | D | E | |
1 | CODE | DNUM | MNUM | YNUM | P1 |
2 | 42736 | 1 | 1 | 2017 | $0.00 |
3 | 5 | 6 | 7 | 1254 | $999.00 |
4 | 42736 | 1 | 1 | 2017 | $0.00 |
5 | 42736 | 1 | 3 | 2017 | $0.00 |
6 | 42736 | 1 | 1 | 2017 | $2.00 |
7 | 42736 | 1 | 2 | 2017 | $3.00 |
8 | 42736 | 1 | 1 | 2017 | $0.00 |
9 | 5 | 6 | 7 | 2017 | $999.00 |
<tbody>
</tbody>
Code:
Public Sub CreateRangeName() 'Create and names a range as 'Rng'
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Sheet1")
Dim Rng As Range 'Name of range created by this subroutine
Dim RngLoc As Range ‘Cell location of range
With ws1
.Cells(1, 1).Select 'Cursor at cell A1 and make it the ActiveCell
'The following line of code creates the range and names it 'Rng'
Set Rng = Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row_
,ActiveCell.End(xlToRight).Column))
.Range("RngLoc") = Rng.Address ‘Transfer address to Excel variable
End With ‘End With ws1
End Sub