IF condition meets import line to another file

khalidnisar

New Member
Joined
May 4, 2011
Messages
8
Hi, I need help,i am trying to import a line from another excel file say "file-1.xlsx , sheet1", when a particular conditions meets in a column 'E' say when cell=today's date then import then full line to another file called "file-2.xlsx, sheet-2 in first empty line".Also the date from hidden lines i don't want them to be extracted.
any help will be highly appreciated.thanking in advance.
regards Khalid
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Assuming that both workbooks are open, try...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] test()

    [color=darkblue]Dim[/color] wkbSource [color=darkblue]As[/color] Workbook
    [color=darkblue]Dim[/color] wksSource [color=darkblue]As[/color] Worksheet
    [color=darkblue]Dim[/color] wkbDest [color=darkblue]As[/color] Workbook
    [color=darkblue]Dim[/color] wksDest [color=darkblue]As[/color] Worksheet
    [color=darkblue]Dim[/color] FoundCells [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] Cell [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] FirstAddress [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] Cnt [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] NextRow [color=darkblue]As[/color] [color=darkblue]Long[/color]
    
    [color=darkblue]Set[/color] wkbSource = Workbooks("file-1.xlsx")
    [color=darkblue]Set[/color] wksSource = wkbSource.Worksheets("Sheet1")
    
    [color=darkblue]Set[/color] wkbDest = Workbooks("file-2.xlsx")
    [color=darkblue]Set[/color] wksDest = wkbDest.Worksheets("Sheet2")
    
    [color=darkblue]With[/color] wksSource.Columns("E")
        [color=darkblue]Set[/color] Cell = .Find(what:=Date, LookIn:=xlFormulas)
        [color=darkblue]If[/color] [color=darkblue]Not[/color] Cell [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
            FirstAddress = Cell.Address
            [color=darkblue]Do[/color]
                Cnt = Cnt + 1
                [color=darkblue]If[/color] FoundCells [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
                    [color=darkblue]Set[/color] FoundCells = Cell.EntireRow
                [color=darkblue]Else[/color]
                    [color=darkblue]Set[/color] FoundCells = Union(FoundCells, Cell.EntireRow)
                [color=darkblue]End[/color] [color=darkblue]If[/color]
                [color=darkblue]Set[/color] Cell = .FindNext(Cell)
            [color=darkblue]Loop[/color] [color=darkblue]While[/color] Cell.Address <> FirstAddress
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]End[/color] [color=darkblue]With[/color]
            
    [color=darkblue]If[/color] Cnt > 0 [color=darkblue]Then[/color]
        [color=darkblue]With[/color] wksDest
            [color=darkblue]With[/color] .UsedRange
                NextRow = .Rows.Count + .Rows(1).Row
            [color=darkblue]End[/color] [color=darkblue]With[/color]
            FoundCells.Copy .Cells(NextRow, "A")
            MsgBox "Completed...", vbInformation
        [color=darkblue]End[/color] [color=darkblue]With[/color]
    [color=darkblue]Else[/color]
        MsgBox "No records found...", vbInformation
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0
You're very welcome! And thanks for the feedback!
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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