• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

vba macro that takes info from the macro to excel file but filtered by date

Ana Luna

New Member
Hi All,
I attach the" Date" vba macro with a vba code that takes info from "Date" and place it into excel "file". But, I need to take only the dates before today .
Do you know the code to apply it?
Thanks you!
 

Attachments

  • file.xlsx
    11.9 KB · Views: 4
  • Date.xlsm
    19.7 KB · Views: 4
I rewrote your program so that I could understand it better. I don't see anything wrong with your program, but mine looks like this:
Code:
  'Datos Origen
  Dim wbLibOrig, wsHojOrig, clOrig
  Set wbLibOrig = Workbooks(ThisWorkbook.Name)
  Set wsHojOrig = wbLibOrig.Worksheets("Sheet1")
  Set clOrig = wsHojOrig.Cells

  'Datos Destino
  Const Ruta = "C:\Users\bikai\Documents\file.xlsx"
  Dim wbLibDest, wsHojDest, clDest
  Set wbLibDest = Workbooks.Open(Ruta)
  Set wsHojDest = wbLibDest.Worksheets("Sheet1")
  Set clDest = wsHojDest.Cells

  Set org = wsHojOrig.Range("A:K") 'this is the range that is to be searched; it's the same throughout the program, so no need
    'to specify it more than once.
  For jr = 3 To wsHojDest.Rows.Count 'I'm used to For rather than While
    Dim ValBusq, FilOrig
    ValBusq = clDest(jr, 1).Value 'establish the search value
    If ValBusq = "" Then Exit For 'exit the loop if we've found the end of the data
    FilOrig = Application.VLookup(ValBusq, org, 2, 0).Row 'this is the row where Vlookup found the search value.  The
      'row is the same for columns A:K, so no need to do the Vlookup more than once
    For jc = 2 To 11 'for each of the Destino columns
      clDest(jr, jc).Value = clOrig(FilOrig, jc).Value 'copy the value from Origen to Destino
      Next jc
    Next jr
I'm not sure I understand your question, though. The dates are in row 2. I guess that you want something like this: Before copying each copy the values from the origen worksheet only in the columns that have a past date (before today) in that column. Is that right?

If so, I would do it this way: Inside the second (column) loop, look at the date in row 2, and skip the copy if the date doesn't meet your criterion. Maybe like this:
Code:
ValHoy = Now 'do this at the beginning of the program, before the loops start
.
.
.
    For jc = 2 To 11 'for each of the Destino columns
      If clOrig(2, jc).value > ValHoy Then Goto SaltarCol
      clDest(jr, jc).Value = clOrig(FilOrig, jc).Value 'copy the value from Origen to Destino
SaltarCol:
      Next jc
You didn't say which dates to check, Origen or Destino. And the way I wrote it, it'll do the copy if the date is equal to today. You'll have to test this, and adapt it to your own coding style.
 
Back
Top