We help IT Professionals succeed at work.

Using range variable in Networkdays formula

273 Views
Last Modified: 2016-01-12
Help please. The following code always throws a "Type Mismatch" error on the final line whether I dimension HldyRange as a Variant or a Range.  
Dim LastHldyRow As Long, LastHldyCol As Long
Dim HldyRnge As Variant

      Worksheets("Holidays").Activate
      Worksheets("Holidays").UsedRange
      LastHldyRow = Range("A1").SpecialCells(xlCellTypeLastCell).Row
      LastHldyCol = Range("A1").SpecialCells(xlCellTypeLastCell).Column
      Set HldyRnge = Range(Cells(1, 2), Cells(LastHldyRow, 2))
     
      Worksheets("OpenPOs").Activate
      Worksheets("OpenPOs").UsedRange
     
      Range("Q1") = "Remaining Time"
      Range(Cells(2, 17), Cells(LastRow, 17)).FormulaR1C1 = "=NetworkDays(RC[-1],DATE(YEAR(RC[-4]),MONTH(RC[-4]),DAY(RC[-4]))," & HldyRnge & ")"

I originally tried to use the variable LastHldyRow in the formula on the last line as:
      Range(Cells(2, 17), Cells(LastRow, 17)).FormulaR1C1 = "=NetworkDays(RC[-1],DATE(YEAR(RC[-4]),MONTH(RC[-4]),DAY(RC[-4])), Holidays!B1:B" & LastHldyRow & ")"

This creates a formula in the spreadsheet but it produces the range reference in the spreadsheet formula as Holidays!B:'B11'  which produces a #NAME? error in Excel. If I modify the formula manually in excel, take the tic marks out, and use the reference range Holidays!B1:B11, it works just as expected. I don't know how to avoid the code adding tic marks.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
Replace your formula in your code with the following.

"=NETWORKDAYS(RC[-1],DATE(YEAR(RC[-4]),MONTH(RC[-4]),DAY(RC[-4])),HldyRnge)"


"=NETWORKDAYS(RC[-1],DATE(YEAR(RC[-4]),MONTH(RC[-4]),DAY(RC[-4])),Holidays!R[-8]C[-6])"

Author

Commented:
Thanks EA but the first line of code just puts the following formula in Excel:
=NETWORKDAYS(P2,DATE(YEAR(M2),MONTH(M2),DAY(M2)),HldyRnge)

and I get a #NAME error

The second line would put variable ranges in. I want to use a fixed range from the Holidays worksheet. The range in the Holidays work sheet might change from time to time but everytime that formula is pasted in the OpenPOs worksheet, the Holidays range is the same.
CERTIFIED EXPERT
Top Expert 2015

Commented:
EdLB,

Did you try the formula which i posted??

Saurabh...
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks Saurabh and EA. I'm sure EA's solution would work but I can use Saurabh's without having to create a named range. Not sure why the R1C1 version of the formula creates the tic marks in the range but happy to use the non-R1C1 version.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.