+ Reply to Thread
Results 1 to 6 of 6

VBA Code to make two columns with dates the same

Hybrid View

  1. #1
    Registered User
    Join Date
    04-28-2013
    Location
    Stockholm
    MS-Off Ver
    Excel 2007
    Posts
    93

    VBA Code to make two columns with dates the same

    Hi there,

    I attach the file I'm working with. It contains two columns with dates and I want to make them containing exactly the same values. For example, A22 = 01/02/2005 while there is no such date in column B. Hence, I want to delete this so 02/02/2005 comes up and it is equal to cell B22. Then it continues and we have a cell in column A with the date 14/03/2005 but it corresponds with 11/03/2005 on the same row in column B, therefore the cell in column B must be deleted until the two get equal and so on. I wrote the following code:

    Sub Whatever()
        For i = 1 To 2246
            If Worksheets("Dates").Cells(1 + i, 1) < Worksheets("Dates").Cells(1 + i, 2) Then
                    Worksheets("Dates").Cells(1 + i, 1).Delete Shift:=xlUp
                    
                    Do Until Worksheets("Dates").Cells(1 + i, 1) >= Worksheets("Dates").Cells(1 + i, 2)
                    Loop
                    
            ElseIf Worksheets("Dates").Cells(1 + i, 1) > Worksheets("Dates").Cells(1 + i, 2) Then
            
                Do Until Worksheets("Dates").Cells(1 + i, 1) = Worksheets("Dates").Cells(1 + i, 2)
                    Worksheets("Dates").Cells(1 + i, 2).Delete Shift:=xlUp
                Loop
            End If
        Next i
            
    End Sub
    and it runs through the observations up to the beginning of 2006 when it can't go any further. I would greatly appreciate any suggestions on your side. Thank you! Dates.xlsx

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA Code to make two columns with dates the same

    Probably not the most efficient but i am sleepy and it is what comes to the top of my mind:

    Sub RunMe()
    Dim ws As Worksheet:    Set ws = Sheets("Dates")
    Dim iRow As Long, lastrow As Long
    
    Application.ScreenUpdating = False
    
    iRow = 2
    lastrow = ws.UsedRange.SpecialCells(xlCellTypeLastCell).Row
    
    Do
        If ws.Range("A" & iRow).Value < ws.Range("B" & iRow).Value Then
            ws.Range("A" & iRow).Delete Shift:=xlUp
        ElseIf ws.Range("A" & iRow).Value > ws.Range("B" & iRow).Value Then
            ws.Range("B" & iRow).Delete Shift:=xlUp
        Else
            iRow = iRow + 1
        End If
    Loop Until iRow >= lastrow Or ws.Range("A" & iRow).Value = ""
    
    Application.ScreenUpdating = True
    
    End Sub

  3. #3
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA Code to make two columns with dates the same

    Hi n_ant,
    try it
    Sub ertert()
    Dim x, y(), i&, j&
    With Range("A1").CurrentRegion
        x = .Value
        ReDim y(1 To UBound(x), 1 To 2)
        With CreateObject("Scripting.Dictionary")
            '            .CompareMode = 1
            For i = 2 To UBound(x)
                .Item(x(i, 1)) = 1
            Next i
            For i = 2 To UBound(x)
                If .Exists(x(i, 2)) Then
                    j = j + 1
                    y(j, 1) = x(i, 2)
                    y(j, 2) = x(i, 2)
                End If
            Next i
        End With
        .Offset(1).ClearContents
        .Offset(1).Resize(j, 2).Value = y()
    End With
    End Sub

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA Code to make two columns with dates the same

    Hi n_ant,

    I'm not actually sure what you're trying to do but this works:

    Sub Whatever(): Dim i As Long, ws As Worksheet: Set ws = Worksheets("Dates")
        For i = 2 To ActiveSheet.UsedRange.Rows.count
        If ws.Cells(i, 1) = ws.Cells(i, 2) Then GoTo GetNext
            If ws.Cells(i, 1) < ws.Cells(i, 2) Then
                    ws.Cells(i, 2).Insert Shift:=xlDown
                     
            ElseIf ws.Cells(i, 1) > ws.Cells(i, 2) Then
                ws.Cells(i, 1).Insert Shift:=xlDown
                
            End If
    GetNext:    Next i
            
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  5. #5
    Registered User
    Join Date
    04-28-2013
    Location
    Stockholm
    MS-Off Ver
    Excel 2007
    Posts
    93

    Re: VBA Code to make two columns with dates the same

    Thank you all for your suggestions! That's awesome. Words can't express my gratitude

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA Code to make two columns with dates the same

    You're welcome and thanks for the rep!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How can I make this code shorter by using a loop. or there's any easier shorcuts code
    By romarkevinruiz in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-10-2014, 08:53 AM
  2. Replies: 1
    Last Post: 08-14-2013, 12:01 PM
  3. How to make code look in other columns
    By Spark9871 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-06-2013, 03:56 PM
  4. [SOLVED] Code to look at 2 columns, one finding like value and the other evaluating Dates
    By mjfox52610 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-19-2013, 09:39 AM
  5. [SOLVED] [SOLVED] the dates on cell format make different dates.
    By date formats morph the dates/chang case in forum Excel - New Users/Basics
    Replies: 7
    Last Post: 04-17-2005, 10:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1