Discrepancy formula and VBA Weekday

Peltz

Board Regular
Joined
Aug 30, 2011
Messages
87
Hi there. Im using this formula in Cell N4
Code:
="Week "&(N6-($D$4-WEEKDAY($D$4;1)+2))/7+1
N4 has general formatng. This work as a charm. I get the output "Week [and a single digit number]"

However, I need to control this from VBA
The code:
PHP:
 Private Sub Commandbutton1_Click()

 Range("N4").Value = "Weekday " & (N6 - (C8 - Weekday(C8, 1) + 2)) / 7 + 1
End Sub

.. Returns "Weekday 1,71428571428571"

Somethng is obviously wong here but cant figure out what.

Any input woulde much appreciated.

Tanks
 
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Even if that was the way you did it they arent the same. The formula references D4 and N6 and your code says C8 and N6. Which is correct?
 
Upvote 0
Sorry, cut the wrond code:
Code:
[FONT=Courier New][COLOR=#007700]Private [/COLOR][COLOR=#0000BB]Sub Commandbutton1_Click[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]()

 [/COLOR][COLOR=#0000BB]Range[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"N4"[/COLOR][COLOR=#007700]).[/COLOR][COLOR=#0000BB]Value [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#DD0000]"Week " [/COLOR][COLOR=#007700]& ([/COLOR][COLOR=#0000BB]N6 [/COLOR][COLOR=#007700]- (D4[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#007700]- [/COLOR][COLOR=#0000BB]Weekday[/COLOR][COLOR=#007700](D4[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]1[/COLOR][COLOR=#007700]) + [/COLOR][COLOR=#0000BB]2[/COLOR][COLOR=#007700])) / [/COLOR][COLOR=#0000BB]7 [/COLOR][COLOR=#007700]+ [/COLOR][/FONT][COLOR=#0000BB][FONT=Courier New]1
End Sub[/FONT][/COLOR]

This is it.

Sorry
 
Upvote 0
You cant just use 'N6' you need to use Range("N6").Value or better still include the sheet name eg Sheets("Sheet1").Range("N6").Value but as long as the button is on the same sheet as the values this will do:

Code:
Range("N4").Value = "Weekday " & (Range("N6").Value - (Range("D4").Value - Weekday(Range("D4").Value, 1) + 2)) / 7 + 1
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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