Question

VBA Combining workbooks/worksheets - Copy data from rows where column C is not blank

Asked by: jasocke2

Hi.

I have the attached 2 example workbooks (& the code) which merges the worksheets from 7 different workbooks together into a 'Raw data' worksheet in my master workbook.

It is driven off a parameters table which allows you to select which workbooks/worksheets you want to include in the merge. - The code works fine however when it is copying the data from each worksheets it is just copying all the data as a 'block' copy where I need it do do it row by row becuase I dont want to copy the row if colum C in each row contains no data, ie. it is blank.

So in my code I need another loop which will:
Copy data from column A - E for each row if column C is not blank from workbook1
Paste into Column B - F in Raw Data worksheet (as column A contains the source filename) in master workbook
Loop until it reaches no more data

I ideally just want to copy the specific range as above, not the whole row but either will do.

This is my code, please refer to attachment too which includes master workbook and an data entry workbook where the data comes from.

I don't really want to use autofiltering to get the non blank data I would rater do it cell by cell/row by row checking everytime if column C is blank or not.

With the attached save and change directory in master file

Any help would be most appreciated.

Option Explicit
 
Sub OpenWorkbooks()
'------
Dim Book_Name As range  ' List of available books
Dim Sheet_Name As range ' List of available sheets
Dim dLastRow As Long
Dim oLastRow As Long
Dim sLastRow As Long
Dim DestinationSheet As Worksheet
Dim SourceSheet As Worksheet
Dim WorkBookList As range
Dim WorkSheetList As range
Dim WorkbookPath As String
 
 
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        .EnableEvents = False
    End With
    
        'set source and destination sheet
    Set DestinationSheet = Sheets("Raw Data")
    Set SourceSheet = Sheets("Parameters")
    
      
    With SourceSheet
        ' Path of the workbooks
        WorkbookPath = range("C4")
        ' List of available workbooks
        Set WorkBookList = range("D5:D16")
        ' List of available worksheets
        Set WorkSheetList = range("F5:F16")
    End With
    
 
    With DestinationSheet
        ' Delete any exsisting data and paste in row after heading
        dLastRow = range("A" & Rows.Count) + 2
        'Save
        oLastRow = dLastRow
        
        
        ' for each book name in the workbook list
        For Each Book_Name In WorkBookList
        ' If cell next to it has a Y or y in it then open workbook, if anything else raise error
        If UCase(Book_Name.Offset(0, 1)) = "Y" Then
            Workbooks.Open WorkbookPath & Book_Name
 
        ' for each sheet name in the sheetname list
        For Each Sheet_Name In WorkSheetList
'------------------------------------------------------------------------------------------
        ' If cell next to it has a Y or y in it then use worksheet, if anything else dont
        If UCase(Sheet_Name.Offset(0, 1)) = "Y" Then
            'Where from
        Set SourceSheet = ActiveWorkbook.Sheets(Sheet_Name.Value)
            'Get last row of data
            sLastRow = SourceSheet.range("A" & Rows.Count).End(xlUp).Row
            'If data
        If sLastRow > 1 Then
            'Copy
            SourceSheet.range("A3:E" & sLastRow).Copy .range("B" & dLastRow)
            'Get new last row of destination
            dLastRow = .range("B" & Rows.Count).End(xlUp).Row + 1
            'Put workbook name into first column
            .range("A" & oLastRow & ":A" & dLastRow - 1) = Book_Name.Value
            'Save
            oLastRow = dLastRow
'---------------------------------------------------------------------------------------------
 
        End If
        End If
        'next sheet in list
        Next Sheet_Name
        'close workbook
            ActiveWorkbook.Close True
        End If
        
        'Next workbook
        Next Book_Name
        
    End With
    
 
'------
    ' Message to user that import is complete
    MsgBox ("Data refresh complete"), vbInformation, "Done!"
'------
    ' set all to nothing
    Set Book_Name = Nothing
    Set Sheet_Name = Nothing
    Set DestinationSheet = Nothing
    Set SourceSheet = Nothing
    Set WorkBookList = Nothing
    Set WorkSheetList = Nothing
    
'------
    ' turn functions back on
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
        .EnableEvents = True
    End With
 
 
End Sub

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-09-15 at 07:46:11ID24733072
Tags

VBA

,

combining workbooks

,

merge

Topics

Microsoft Excel Spreadsheet Software

,

Microsoft Office Suite

,

Automation

Participating Experts
2
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. How many worksheets in a workbook
    How many worksheets can you have in any one Workbook??? (Excel 97) Cheers, Stu
  2. For every worksheet in the workbook, select cell a1
    I have a workbook with multiple worksheets and chart sheets. I have a clean up routine that does some formatting. I'd like to end it with each worksheets a1 cell selected.
  3. Autofilter on a protected shared workbook Excel 2000
    I have a workbook which worked beautifully as a protected workbook with autofilters and VBA-driven command buttons, until I turned it into a shared workbook. This workbook has an autofilter set up on some (not all) of the columns, and the data in these columns must be protec...
  4. Protecting workbook / worksheet with VBA
    Experts, I have a form that will be sent and password protected. However, I am using a print button that puts on an autofilter on a specific row, filters for non zeros, selects range, prints..etc. In order to make this work, I had to use the protection code in the workbook...
  5. Combining worksheets and workbooks
    I have a workbook with a number of worksheets. I want to combine the existing sheets on a new sheet such that the sheets are one above the other and starting from column B. Column A should be filled with the name of the original worksheet against the data for that worksheet. ...
  6. Named cells in workbook in multiple worksheets
    Excel 2003/SP1 XPSP2 My workbook, say workbook B, includes name definitions for the same name but which refer to different cells according to each worksheet. E.g. a cell name defined in worksheet 1 refers to cell <XXnn> and the same cell name defined in worksheet ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: sirplusPosted on 2009-09-15 at 10:34:11ID: 25337250

I am wondering why the use of excel when it seems access would be better suited with queries that can exactly customise what you return without tampering the data at all.
One slip and its gone
Its easy to sling excel data to access and viceversa
I can help with the excel code if you are determined, only its 3 am and I will look tomorrow if thats ok?

 

by: patrickabPosted on 2009-09-15 at 13:01:46ID: 25338738

jasocke2,

If you don't have Access and are not used to using it, don't even think about it. It's expensive, it's learning curve is steep and it is so far from user-friendly as to beggar belief. Of course the Access officionados here will tell you that it's a doddle - what else would you expect!

Stick with Excel and make sure you keep a master copy of the data is my advice.

Patrick



 

by: patrickabPosted on 2009-09-15 at 15:38:04ID: 25340325

jasocke2,

You could use the sort of syntax given below in which a discontinuous range is built. You can then copy, delete or whatever the whole range rng in one step.

Patrick

For Each celle In rng1
    If LCase(celle) = "yes" Then
        Set rng = Union(rng, celle)
        Set rng = celle
    End If
Next celle
 
rng.Copy destination

                                              
1:
2:
3:
4:
5:
6:
7:
8:

Select allOpen in new window

 

by: jasocke2Posted on 2009-09-16 at 01:38:07ID: 25343237

Thanks for you replies.

sirplus - Due to the circumstances I am forced into using Excel to perform this task so any help would be much appreciated - Thanks.

patrickab - How would I adapt that snippet of code to fit into my code as above?

Thanks again.

 

by: patrickabPosted on 2009-09-16 at 04:22:45ID: 25344215

jasocke2,

>How would I adapt that snippet of code to fit into my code as above?

I'm afraid I assumed it was all your code and as a result believed you would be able to make use of my code snippet however you wanted. Unfortunately I don't have the time to help you right now.

Patrick

 

by: jasocke2Posted on 2009-09-16 at 04:26:32ID: 25344235

patrickab - The code has been pieced together from various sources as I am still trying to get my head around VBA.

Thanks for you reply though, I will have a look at the code you provided.

 

by: jasocke2Posted on 2009-09-16 at 08:36:52ID: 25346781

Let me try and explain it a bit better:

What I am trying to do is from workbook A (to workbook x) copy the range A1:E1 and paste into the master workbook in range B1:F1 if in workbook A column C isn't blank, keep looping until there is no more data in workbook A, column A, always pasting into the next available row in master workbook.

My current code as above does something simular to this but copies all the data as a block (ie A1:E100) where I would like to do it a row at a time so that column C can be checked for blanks and if it has move onto next row.

All workbooks that the data is coming from are in the same format, data starting from A3:E1000 (no more)

Hope this helps describe it a bit better.

Thanks.

 

by: sirplusPosted on 2009-09-16 at 09:16:39ID: 25347265

I thought you said this worked?

Well it's bulletproof now & runs like a wooly goat!        
:-)

 

by: sirplusPosted on 2009-09-16 at 09:32:55ID: 25347448

Add as much or little data as you want the only thing that mattars is that column A has data in every casse of every record
I could easily make it flexible so you can import whatever extra fields you decide to add in the future all automatically without change to the code

What is imported to keep it fast is the values only -not formats
Any formatting it to be applied to the final sheet

I use a similar sheet to track our sales too but names etc are loaded from arrays hard coded inside the module that way all passwords are out of sight

 

by: sirplusPosted on 2009-09-17 at 00:57:29ID: 25353663

How did you go?

 

by: jasocke2Posted on 2009-09-17 at 01:26:00ID: 31628950

Spot on!

 

by: jasocke2Posted on 2009-09-17 at 01:27:45ID: 25353831

sirplus -

Worked a charm, exactly what I was after.

Thanks for all the help ;-)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...