[VBA]how to update date in selected cells

jovie

Board Regular
Joined
Nov 13, 2015
Messages
68
Hi I am very new of VBA, I would like to write a programme that when I click the button I set, it will update the date in selected cells instead of all the cells in the range I choose.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Update the date to what? The current date?
 
Upvote 0
Yup, I would like to update the date to current date, I only can update certain range like from A1 to A100, however during this range I have some cells that I dun want that update.
 
Upvote 0
So what is the logic for which cells should be updated and which cells shouldn't?
 
Upvote 0
maybe this:

Code:
Option Explicit


Sub fillNoColor()
    Dim c As Range
    Dim rng As Range
    Set rng = Range("A1:A19") 'change range as required
    
    Application.ScreenUpdating = False
    For Each c In rng
    If c.Interior.ColorIndex <> 6 Then 'change colorindex as required. This is yellow
    c.Value = Date
    End If
    Next c
    Application.ScreenUpdating = True
    
    
End Sub
 
Upvote 0
maybe this:

Code:
Option Explicit


Sub fillNoColor()
    Dim c As Range
    Dim rng As Range
    Set rng = Range("A1:A19") 'change range as required
    
    Application.ScreenUpdating = False
    For Each c In rng
    If c.Interior.ColorIndex <> 6 Then 'change colorindex as required. This is yellow
    c.Value = Date
    End If
    Next c
    Application.ScreenUpdating = True
    
    
End Sub

I think this will be very useful, but I am not with my laptop now, so I can't try the code now. However I wonder to know that is there have anyway to update the date in one row that I write sth today, and next time when I write sth in next row it will update the date of that row for the current row
 
Upvote 0
OK. Try this. If you select the range of cells you want to apply it to and run this, it will put the date in any cell that does not have any color fill (regardless of what color fill you may be using):
Code:
Sub MyDateStamp()

    Dim cell As Range
    
    Application.ScreenUpdating = False
    For Each cell In Selection
        If cell.Interior.Pattern = xlNone Then
            cell = Date
        End If
    Next cell
    Application.ScreenUpdating = True
            
End Sub
So it will work on any selected range and any color fill without having to adjust anything in the code.
 
Upvote 0
OK. Try this. If you select the range of cells you want to apply it to and run this, it will put the date in any cell that does not have any color fill (regardless of what color fill you may be using):
Code:
Sub MyDateStamp()

    Dim cell As Range
    
    Application.ScreenUpdating = False
    For Each cell In Selection
        If cell.Interior.Pattern = xlNone Then
            cell = Date
        End If
    Next cell
    Application.ScreenUpdating = True
            
End Sub
So it will work on any selected range and any color fill without having to adjust anything in the code.
Best regard, Really, thanks
 
Upvote 0

Forum statistics

Threads
1,216,212
Messages
6,129,547
Members
449,515
Latest member
lukaderanged

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