Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 30th, 2012, 11:00 AM
Registered User
 
Join Date: Nov 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Copy header row only once in VBA macro

I have multiple workbooks that I'm copying data from and merging into a new workbook. I only want to pull in the heading row (which happens to be row 3, columns A:H) once from the first workbook since the headings are all the same, and then only copy rows 4 to the lastrow from subsequent files.

I am not a programmer, but have everything working except ignoring the heading row in subsequent files when setting the copy range. I think I need to add an "if/then" statement somewhere in here, but I'm not sure. Any help any of you VBA gurus can provide would be greatly appreciated!!!

Below is the part of the code where I believe I need to make an adjustment.

LastRow = WorkBk.Worksheets(1).Cells(Rows.Count, "A").End(xlUp).Row
Set SourceRange = WorkBk.Worksheets(1).Range("A3:H" & LastRow)

Set DestRange = SummarySheet.Range("B" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)

DestRange.Value = SourceRange.Value

NRow = NRow + DestRange.Rows.Count

Thanks!
Julie
 
Old December 9th, 2012, 09:32 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

A simple solution would be to have the HeaderRow in a separate variable

Set HeaderRange = WorkBk.Worksheets(1).Range("A3:H3)

and the rest of range in

Set SourceRange = WorkBk.Worksheets(1).Range("A4:H" & LastRow)

You can now use SourceRange in iterations for all sheets

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
help in how to copy macro in new added sheet zmhnk Excel VBA 1 April 9th, 2012 10:22 PM
Scrollbar in row of Datagrid Except Header Row Theone84 ASP.NET 2.0 Professional 0 August 11th, 2008 12:10 AM
Macro for copy pasting without opening the source Umasriram2 Excel VBA 6 April 9th, 2008 02:27 AM
How to freeze the header row shankhan Classic ASP Databases 1 April 23rd, 2005 02:18 PM
Copy Header and Details to similar tables Mitch Access 2 February 2nd, 2005 07:34 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.