Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 5th, 2005, 04:48 AM
Authorized User
 
Join Date: Sep 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default inputing formula problems

hi there im having problems inserting a formula in a macro when i do the same input by hand it woks fine

With Range(Cells(2, "A"), Cells(2, "A").End(xlDown))
   lRowStart = .Row
   lRowEnd = lRowStart + .Rows.Count - 1
 End With
 With Range(Cells(lRowStart, "B"), Cells(lRowEnd, "B"))
    .Offset(-1).Value = "WE"
    .Formula = "=IF(WEEKDAY(A2)=7,A2+6,IF(WEEKDAY(A2)=1,A2+5,IF(W EEKDAY(A2)=2,A2+4,IF(WEEKDAY(A2)=3,A2+3,IF(WEEKDAY (A2)=4,A2+2,IF(WEEKDAY(A2)=5,A2+1,IF(WEEKDAY(A2)=6 ,A2)))))))"
    Range("B2").Select
 End With

the problem i get is some cells show the wrong date format and i dont seem to be able to change even if i try to format the cells afterwards

any help would be much appriciated

 
Old September 5th, 2005, 06:16 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

replace your formula with the following.

    .FormulaR1C1 = "=IF(WEEKDAY(R2C1)=7,R2C1+6,IF(WEEKDAY(R2C1)=1,R2C 1+5,IF(WEEKDAY(R2C1)=2,R2C1+4,IF(WEEKDAY(R2C1)=3,R 2C1+3,IF(WEEKDAY(R2C1)=4,R2C1+2,IF(WEEKDAY(R2C1)=5 ,R2C1+1,IF(WEEKDAY(R2C1)=6,R2C1)))))))"


Cheers

Matt

 
Old September 5th, 2005, 08:19 AM
Authorized User
 
Join Date: Sep 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i tried the code that u gave me but now all my dates are coming the same

 
Old September 5th, 2005, 11:19 AM
Authorized User
 
Join Date: Sep 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

could anyone tell me why the code produces one date and then repeats it???Please

 
Old September 6th, 2005, 10:52 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I understood you to require the cell A2 to be a single cell that is reference by all the formula, but if you want it to be relative to the current row, provided the date is in column A and the formaula is in Col B then the following will work.

  .FormulaR1C1 = "=IF(WEEKDAY(RC[-1])=7,RC[-1]+6,IF(WEEKDAY(RC[-1])=1,RC[-1]+5,IF(WEEKDAY(RC[-1])=2,RC[-1]+4,IF(WEEKDAY(RC[-1])=3,RC[-1]+3,IF(WEEKDAY(RC[-1])=4,RC[-1]+2,IF(WEEKDAY(RC[-1])=5,RC[-1]+1,IF(WEEKDAY(RC[-1])=6,RC[-1])))))))"

Also replace the following...
   With Range(Cells(2, "A"), Cells(2, "A").End(xlDown))
      lRowStart = .Row
      lRowEnd = lRowStart + .Rows.Count - 1
   End With
   With Range(Cells(lRowStart, "B"), Cells(lRowEnd, "B"))

With...
   With Range("B2:B" & Range("A65000").End(xlUp).Row)

Matt

 
Old September 6th, 2005, 11:06 AM
Authorized User
 
Join Date: Sep 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks matt






Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with formula Juan0214 Access 4 August 1st, 2008 07:25 AM
Help with formula 2 Juan0214 Access 3 July 10th, 2008 06:52 PM
Formula scandalous Access VBA 11 February 27th, 2007 09:49 AM
formula sinha Crystal Reports 1 October 26th, 2005 12:53 PM
Problems introducing a formula using VBA mariona_cid Excel VBA 0 June 1st, 2004 07:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.