im bewildered whats going wrong

firexcel

New Member
Joined
Jan 2, 2007
Messages
10
i have a macro to run which copies the =now() from another cell same page

this is the code
Code:
Sub Macro11()
'
' Macro11 Macro
'
'ActiveSheet.Unprotect "6477"    
   Range("m2").Select
Selection.Copy
Range("B" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("c" & Rows.Count).End(xlUp).Offset(1).Select
ActiveCell.FormulaR1C1 = "IN"
Range("d" & Rows.Count).End(xlUp).Offset(1).Select
'ActiveSheet.Protect "6477"


    
        
End Sub


the problem is the copied date is changed and wrong example the month and day swap around


how can I ensure when the macro runs the correct dd/mm/yyyy hh:mm is pasted
 
Last edited by a moderator:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I cannot see how that would happen if you have an =NOW() formula in M2. You don't need all that code though:

Code:
Sub Macro11()

'ActiveSheet.Unprotect "6477"    

With Range("B" & Rows.Count).End(xlUp).Offset(1)
   .Value = Range("M2").Value
   .Offset(, 1).Value = "IN"
   .Offset(, 2).Select
End With

'ActiveSheet.Protect "6477"
End Sub
 
Upvote 0
You probably need to copy the number format as well. The code you posted looks like a complicated way of doing this:

Code:
Public Sub Macro11()

Dim lastRow As Long

'ActiveSheet.Unprotect "6477"
lastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
Cells(lastRow, "B").Value = Range("M2").Value
Cells(lastRow, "B").NumberFormat = Range("M2").NumberFormat
Cells(lastRow, "C").FormulaR1C1 = "IN"
Cells(lastRow, "D").Select
'ActiveSheet.Protect "6477"

End Sub

WBD
 
Upvote 0
Rory's code is more succinct. Just maybe add in copying the numberformat as well as the value:

Code:
Sub Macro11()

'ActiveSheet.Unprotect "6477"    

With Range("B" & Rows.Count).End(xlUp).Offset(1)
   .Value = Range("M2").Value
   .NumberFormat = Range("M2").NumberFormat
   .Offset(, 1).Value = "IN"
   .Offset(, 2).Select
End With

'ActiveSheet.Protect "6477"

End Sub

WBD
 
Upvote 0
thanks guys - is it possible to have a corrupt sheet as when I use the code it works fine, when I change the time to a historical one the day months swap, then if I just double click on the cell and do nothing each time I do this it swaps automatically !!
 
Upvote 0
It sounds like your historic dates are being entered as text, or you have some other code running (e.g. a Worksheet_Change event).
 
Upvote 0
It sounds like your historic dates are being entered as text, or you have some other code running (e.g. a Worksheet_Change event).

yes yes yes yes

thanks rory my worksheet change event included the date column which is no need - all working as should now thanks all
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,206
Members
453,022
Latest member
RobertV1609

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