Button produces different result

pfordor

New Member
Joined
Mar 21, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. Web
Hello, this is my first post here. I have followed & searched for a long time trying to learn. But I just can't seem to overcome this. The following code run from the ribbon. works fine. Applied to a button & the code in orange returns only 14 (January 14, 1900 as it is a date). Cell AF92 is a date (pay date) that I am trying to add 14 days to each time this code is run. Format in the cell is short form (45372, example for today) date. Button is located on "Timesheet" sheet. "Data Log" sheet is just a running copy of what a person entered on Timesheet for their personal history, then all is cleared on Timesheet for future manual input. All in same workbook. There may be other issues here, please advise to all, but my main concern is why in the world am i getting different results when applying to a button?

Rich (BB code):
Sub vba_copy_to_another_worksheet()

Dim orig_Rang As Range
Set orig_Rng = Selection

ActiveSheet.Unprotect "**"
Worksheets("Data Log").Unprotect "**"

Worksheets("Data Log").Range("F:F").EntireColumn.Insert
Worksheets("Data Log").Columns("E").Copy
Worksheets("Data Log").Columns("F").PasteSpecial xlPasteValues

Application.CutCopyMode = False

 
 Dim s As Range
 
    Worksheets("Timesheet").Range("J117:O117").Copy
    Worksheets("Timesheet").Range("J112:O112").PasteSpecial xlPasteValues
    Worksheets("Timesheet").Range("I7:AL20").ClearContents
    Worksheets("Timesheet").Range("AF92").Value = Range("AF92").Value + 14
    Worksheets("Data Log").Range("A41:C41").ClearContents
        

orig_Rng.Parent.Activate
orig_Rng.Select
  
Worksheets("Timesheet").Protect "**"
Worksheets("Data Log").Protect "**"

  
  
End Sub
 
Last edited by a moderator:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
You didn't specify the sheet for the second Range property. It should be:

VBA Code:
Worksheets("Timesheet").Range("AF92").Value = Worksheets("Timesheet").Range("AF92").Value + 14

Or use a With block to save typing the same sheet name repeatedly:

VBA Code:
With Worksheets("Timesheet")
    .Range("J117:O117").Copy
    .Range("J112:O112").PasteSpecial xlPasteValues
    .Range("I7:AL20").ClearContents
    .Range("AF92").Value = .Range("AF92").Value + 14
End With
    Worksheets("Data Log").Range("A41:C41").ClearContents
 
Upvote 0
Solution
Oh my gosh! Right in front of my face. Thank you so much!!

Also for teaching me about the Block. Used Excel for years, just never needed DBA until lately. I thought their must be a better way. Still learning.

Thank you again!!
 
Upvote 0
Glad we could help, and welcome to the forum! :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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