I would like to amend my Macro to check if there is existing data on any of the sheets. If so, then to create a new sheet and to copy the data in the sheet created
I have attached sample data (macro in sample data)
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Macro to check if existing data
Howard,
You can check to see if there is any data on the sheet with
Dim rng As Range Set rng = ActiveSheet.UsedRange If rng.Cells.Count = 1 And rng = “” Then [COLOR=”#008000″]’SHEET IS BLANK[/COLOR]
There are many ways to add a new worksheet. Here are just a few:
Worksheets.Add Before:=Worksheets(“Sheet2″) [COLOR=”#008000”]’BEFORE A NAMED WORKSHEET[/COLOR] Worksheets.Add After:=Worksheets(Worksheets.Count) [COLOR=”#008000″]’AFTER THE LAST WORKSHEET[/COLOR] Worksheets.Add Before:=Worksheets(1) [COLOR=”#008000″]’BEFORE THE FIRST WORKSHEET[/COLOR]
Copy data from one sheet to another cell by cell:
Dim ws1 As Worksheet Dim ws2 As Worksheet Set ws1 = Worksheets(“Sheet1”) Set ws2 = Worksheets(“Sheet2”) LastRow = ws1.Cells(Rows.Count, 1).End(xlUp).Row For I = 2 To LastRow ws2.Cells(I, 1) = ws1.Cells(I, 1) Next I
OR
Copy data from one sheet to another using copy/paste:
Dim ws1 As Worksheet Dim ws2 As Worksheet Set ws1 = Worksheets(“Sheet1”) Set ws2 = Worksheets(“Sheet2”) ws1.Range(“A1:C6”).Copy With ws2 .Activate .Range(“A1”).Select .Paste End With Application.CutCopyMode = False
HTH,
Maud
Thanks for your input Maud
I would like to amend my code below, so that if I copy more data from a source file, It will copy the data after the last row in Col A
Sub copyDataFromSource() Dim sourceBook As Workbook Dim destinationBook As Workbook Dim sourceSheet As Worksheet Dim destinationSheet As Worksheet Dim fileSource, sourceRow%, sourceRowCount&, destRow% With Application .ScreenUpdating = False End With fileSource = Application.GetOpenFilename If fileSource = False Or IsEmpty(fileSource) Then Exit Sub Set destinationBook = ThisWorkbook Set destinationSheet = destinationBook.Sheets("sheet1") Set sourceBook = Workbooks.Open(fileSource) Set sourceSheet = sourceBook.Sheets(1) sourceRow = sourceSheet.Cells(sourceSheet.Rows.Count, 1).End(xlUp).Row sourceRowCount = sourceRow - 1 destRow = destinationSheet.Cells(destinationSheet.Rows.Count, 1).End(xlUp).Row destinationSheet.Rows(destRow + 1).Resize(sourceRowCount).Insert destRow = destRow With destinationSheet .Range("a" & destRow & ":ae" & destRow + sourceRowCount - 1).Value = sourceSheet.Range("a1:ae" & sourceRow).Value End With sourceBook.Close False With Application .ScreenUpdating = True End With Set sourceBook = Nothing Set destinationBook = Nothing Set sourceSheet = Nothing Set destinationSheet = Nothing End Sub
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.