+ Reply to Thread
Results 1 to 15 of 15

Search workbooks and paste to ActiveSheet.Range

  1. #1
    Registered User
    Join Date
    04-24-2015
    Location
    Dublin, Ireland
    MS-Off Ver
    MS Office 2010
    Posts
    7

    Post Search workbooks and paste to ActiveSheet.Range

    Evening!

    So, I'm trying to make a macro that will search multiple workbooks for a date defined from a user input box, select that cell, offset the selection by 3 columns and resize with 14 rows.
    The reason for this is that every day I get 1 staffing report from 6 different bases (effectively 6 files) and have to compare the info from yesterday with the updated info from today.
    So what I get are 6 files that look something like what's below. A2 and downwards are the dates I want to search for, while D2 and downwards contain the numbers I want.
    The .Resize is because I need the info from the date I search from and the 2 following weeks (+14 rows).

    Report ex.
    A B C D E
    1| x x x 1 x
    2| x x x 2 x
    3| x x x 3 x
    4| x x x 4 x
    5| x x x 5 x

    I've already set up an Excel sheet to store and compare the extracted info, but I'm having some issues with efficiently extracting it from multiple rows in the reports, to 1 row in the "master".
    The example below is for 1 report only, but I'm thinking that if I get it to work, I can just add the path and name variables, then add the code necessary.

    What I got so far:
    Option Explicit

    Sub SearchARN()

    Application.ScreenUpdating = False

    Dim ARNsource As Workbook
    Dim MasterWB As Workbook
    Dim MasterSH As Worksheet

    Set MasterWB = ActiveWorkbook
    Set MasterSH = MasterWB.Sheets("Sheet1")
    Set ARNsource = Workbooks.Open("C:\Users\"xxxxx"\Desktop\Macro\Reports\Nightshift_ARN.xlsx")

    ' D2 here is for testing only, later it should be the "date"/value stored from the user input box

    With ARNsource.Sheets("Sheet1").Range("D2").Resize(14, 0)
    .Select.Copy
    End With

    ' The range defined is where I need the extracted data to go, for it to work with the comparison formula in the "master"
    ' I've tried using .Transpose to paste from say "D2:D16" to "C33:Q33", but for me that only created a whole new set of issues

    With MasterSH.Range("C33:Q33").PasteSpecial
    End With

    ARNsource.Close SaveChanges:=False

    Application.CutCopyMode = False
    Application.ScreenUpdating = True

    End Sub
    I'm getting a bit different errors depending on where I make changes, but I'm hoping you guys could give me some pointers to where I can begin.

    Thanks in advance!

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

    Re: Search workbooks and paste to ActiveSheet.Range

    Hi Bodaw,

    Welcome to the Forum!

    Try this:

    Please Login or Register  to view this content.
    *Note that I used code tags - here's how we address that:

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    Last edited by xladept; 04-24-2015 at 02:43 PM.
    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

  3. #3
    Registered User
    Join Date
    04-24-2015
    Location
    Dublin, Ireland
    MS-Off Ver
    MS Office 2010
    Posts
    7

    Re: Search workbooks and paste to ActiveSheet.Range

    40min later and it does pretty much exactly what I want!

    I've got a couple questions that I hope you could answer though:
    1.: The target range now saves to C33:C46 instead of C33:Q33, which only activates one of the comparison cells, could that be solved?
    (I'm more adept in PowerShell, and only recently started with VBA, so normally I would use 'Delimiter'. I tried playing around with '.Transpose', but then I'm back to the errors...)

    2.: After the save, the original formatting is removed. Earlier I used the .PasteSpecial to solve that, and I hoped .Value would do the same now.
    Would it be easier to add the formatting back further down or is there a ".keepFormatting" kind of thing I could use?

    3.: At the bottom there's a 'Application.CutCopyMode = False' to clear out the clipboard.
    Could this potentially clear out what I've stored from the user input box earlier or is it limited to the 'Sub -> End Sub'?

    Thanks for your help so far!

  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: Search workbooks and paste to ActiveSheet.Range

    Maybe:

    Please Login or Register  to view this content.
    *Let me know if this works - and, I'm thinking about the formatting - what is the formatting, by the way?
    Last edited by xladept; 04-24-2015 at 04:12 PM.

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

    Re: Search workbooks and paste to ActiveSheet.Range

    Here it is with the paste special code:

    Please Login or Register  to view this content.
    *Are you saying that all those 14 values are a single date?

  6. #6
    Registered User
    Join Date
    04-24-2015
    Location
    Dublin, Ireland
    MS-Off Ver
    MS Office 2010
    Posts
    7

    Re: Search workbooks and paste to ActiveSheet.Range

    The code now works perfectly - The formatting solved it self after I entered it again, it had probably just been removed by one of my earlier attempts
    Those 14 values are the amount of people we expect to have available for each day, so they're not 14 values stored within a single day, rather 1 value from 14 days.

    I'm now trying to integrate the user input box value as a text string to locate in the target.
    What I got so far seems logical to me, but I'm getting a '438' error from the target range.

    Please Login or Register  to view this content.

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

    Re: Search workbooks and paste to ActiveSheet.Range

    Try:

    Please Login or Register  to view this content.
    ** When you put quotes around a variable name it's looking for the variable name - not the variable
    Last edited by xladept; 04-24-2015 at 06:48 PM.

  8. #8
    Registered User
    Join Date
    04-24-2015
    Location
    Dublin, Ireland
    MS-Off Ver
    MS Office 2010
    Posts
    7

    Re: Search workbooks and paste to ActiveSheet.Range

    The whole thing makes much more sense now, and you've probably saved me many, many hours of frustration!

    Last thing before this all should fall in to place.
    This piece here is returning "Duplicate decleration...":
    Please Login or Register  to view this content.
    Tried removing it and changing:
    Please Login or Register  to view this content.
    To:
    Please Login or Register  to view this content.
    But then of course the 'F =' is missing a reference.

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

    Re: Search workbooks and paste to ActiveSheet.Range

    Put all the Dims up top and try it:

    Please Login or Register  to view this content.
    Last edited by xladept; 04-24-2015 at 08:34 PM.

  10. #10
    Registered User
    Join Date
    04-24-2015
    Location
    Dublin, Ireland
    MS-Off Ver
    MS Office 2010
    Posts
    7

    Re: Search workbooks and paste to ActiveSheet.Range

    Smart thought, but this is returning "Object doesn't support this property or method"
    Please Login or Register  to view this content.

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

    Re: Search workbooks and paste to ActiveSheet.Range

    My bad

    Try:

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    04-24-2015
    Location
    Dublin, Ireland
    MS-Off Ver
    MS Office 2010
    Posts
    7

    Re: Search workbooks and paste to ActiveSheet.Range

    Should have noticed that my self... :P
    Tried it out at work today and it's running without errors, but it's also transferring the data from the first source book to all the target ranges.
    Played around with some loop functions, but then the errors were back of course.

    Here's the code now, with all the paths to the workbooks set up:
    Please Login or Register  to view this content.
    It's basically taking the info I request from ARN and pastes it in to the ranges for the other bases as well, without retrieving the info from the 5 other bases.

    Ideas?

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

    Re: Search workbooks and paste to ActiveSheet.Range

    Please Login or Register  to view this content.
    Why wouldn't the second assignment over write all but 4 of the first assignment?

  14. #14
    Registered User
    Join Date
    04-24-2015
    Location
    Dublin, Ireland
    MS-Off Ver
    MS Office 2010
    Posts
    7

    Re: Search workbooks and paste to ActiveSheet.Range

    You've lost me.
    What it does now is fill up the target range for ARN (C13:Q13) as well as the range for BGO (C17:Q17), and then continues to fill up the ranges for the other bases as well, with the information from ARN.
    Instead of filling up range ARN, then continuing to the BGO, locating the input and moving it over to the BGO range, and so on.

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

    Re: Search workbooks and paste to ActiveSheet.Range

    Replace your settings with:

    Please Login or Register  to view this content.
    And try it:|

+ 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] Macro to search workbooks in a folder for a specified column and paste in another workbook
    By maximumkai in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 04-24-2014, 03:46 PM
  2. Macro to cut, paste, search, repeat between two workbooks; request for help
    By wallabumba in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-03-2014, 10:58 AM
  3. [SOLVED] ActiveSheet.Paste Link:= True Does Not Work After Awhile - 1004 MS Excel cannot paste data
    By zfeinstein in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-25-2013, 04:28 PM
  4. [SOLVED] Copy Named Range on Different Sheet and Perform Paste Special on ActiveSheet
    By ScottyBee in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-08-2013, 06:34 PM
  5. search list of files in a folder and paste a image from activesheet and paste to files
    By khan.washik in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-25-2012, 01:37 PM

Tags for this Thread

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