Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Business Applications Business Applications
Search Search
Search for:
Tech Support Guy > > >

macro to copy YYYY and replace the YYYY in format MM/DD/YYYY


(!)

tdosk's Avatar
tdosk tdosk is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jun 2012
Experience: Intermediate
08-Aug-2012, 01:06 PM #1
Exclamation macro to copy YYYY and replace the YYYY in format MM/DD/YYYY
For example i have wb1 which has ws1 containing "2013" cell updated every year. Another wb2 which has ws2 containing a column like this:

1/1/2012
2/1/2012
3/1/2012
......
12/1/2012




I want to have a macro that every year when i run it, the year in wb2 will be updated the same as the year in wb1.





Zack Barresse's Avatar
Computer Specs
Member with 5,434 posts.
 
Join Date: Jul 2004
Location: Oregon, United States
Experience: I'ma learnin'!
09-Aug-2012, 02:27 AM #2
Hi there!

So, this isn't very descriptive. Are these values or formulas? You could just make them formulas that point to that cell...

Code:
=DATE('ws1'!A1,1,1
Does that help?
tdosk's Avatar
tdosk tdosk is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jun 2012
Experience: Intermediate
09-Aug-2012, 09:24 AM #3
No it's not a formula, it's a text. wb2 is actually a csv file and wb1 is excel file.
Zack Barresse's Avatar
Computer Specs
Member with 5,434 posts.
 
Join Date: Jul 2004
Location: Oregon, United States
Experience: I'ma learnin'!
09-Aug-2012, 01:37 PM #4
Ok, well this should get you what you need. Please read the comments in the code...

Code:
Sub UpdateDates()
    
    Dim WB1 As Workbook
    Dim WB2 As Workbook
    Dim WS1 As Worksheet
    Dim WS2 As Worksheet
    Dim rYear As Range
    Dim rDates As Range
    Dim rCell As Range
    
    '/// This ASSUMES you will already have the files open.
    '/// If you will NOT have the files open, we need more code.
    Set WB1 = Workbooks("Excel File's Name Here")
    Set WB2 = Workbooks("CSV File's Name Here")
    Set WS1 = WB1.Worksheets("Worksheet Name Here")
    Set WS2 = WB1.Worksheets("Worksheet Name Here")
    
    '/// Year cell gets set here
    Set rYear = WS1.Range("A1").Value
    
    '/// Dates to get changed is set here
    Set rDates = WS2.Range("A1:A12")
    
    '/// Perform the action
    For Each rCell In rDates.Cells
        rCell.Value = DateSerial(rYear.Value, Month(rCell.Value), Day(rCell.Value))
    Next rCell
    
End Sub
HTH
Garf13LD's Avatar
Garf13LD Garf13LD is offline
Member with 455 posts.
 
Join Date: Apr 2012
Experience: Intermediate
09-Aug-2012, 10:29 PM #5
you can open csv in word and do a replace.
tdosk's Avatar
tdosk tdosk is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jun 2012
Experience: Intermediate
10-Aug-2012, 10:30 AM #6
to Zack: i tried your code but it gives me "Object Required" error at the line
Set rYear = ws1.Range("B1").Value

All the files are opened though

*I just figured it by myself. Since you use Dim ws1 as Range. There should be no Value after. Thanks alot.

Last edited by tdosk; 10-Aug-2012 at 10:42 AM..
Zack Barresse's Avatar
Computer Specs
Member with 5,434 posts.
 
Join Date: Jul 2004
Location: Oregon, United States
Experience: I'ma learnin'!
13-Aug-2012, 08:43 PM #7
I declared WS1 as a worksheet but you need to change "Worksheet Name Here" to whatever your worksheet name is. It is not a range [object].
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑