Tuesday, November 16, 2010

How do i use VB code in MS Excel?

I want to write a function for excel when i type something in one cell the date should be entered into another cell.If i write something in A1 then A3 should automatically get today's date.How do i do that?How do i use VB code in MS Excel?
One way to do this is with some VBA coding that works whenever there is a modification to the Worksheet. The following example is not specific for your example of the A1 and A3 cells, but could be modified to work for you.



Public Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo Worksheet_Change_Err

Dim r As Integer

Dim c As Integer

c = Target.Column

r = Target.Row

If r %26gt; 1 Then 'everything but the header row.

If c = 3 Then 'allow removal of Date/Time-Stamp.

Exit Sub

Else

Cells(r, 3).Value = Now() 'the full Date/Time value.

End If

End If

Worksheet_Change_Exit:

Exit Sub

Worksheet_Change_Err:

MsgBox ';Error#'; %26amp; Err.Number %26amp; ';: '; %26amp; Err.Description %26amp; vbCrLf %26amp; _

Err.Source, vbOKOnly + vbCritical, ';Subscribers Sheet1 -- Worksheet_Change';

Resume Worksheet_Change_Exit

End Sub





You can replace the ';Now()'; with ';Today()'; if you want only the date and not the time as well. And the above uses numbers to represent the columns, so where it says ';3'; that is column ';C';.



.How do i use VB code in MS Excel?
I agree that you don't need a Macro to do this, but just setting the cell to equal another cell doesn't seem like what you're asking for.



If something is typed into cell A1, putting the following formula in cell A3 will show today's date:



=IF(A1%26lt;%26gt;';';, TODAY(), ';';)



I found that the cell needs to be formatted as a date to show as an actual date rather than a long number.
you dont need a macro for it. All you need is a formula in the cell A3. Just type =A1 in the cell A3

No comments:

Post a Comment