VBA & Macro Problems

Use Excel VBA to Combine Data from Visible Worksheets

Jason MillerJason Miller Dec 2, 2026 868 views

Question details

Combine data from visible Excel worksheets into a YEARLY REPORT sheet while skipping hidden sheets.

Combine Data from Visible Excel Worksheets Using VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A workbook contains multiple source worksheets whose data should be appended to a single report using VBA.
Observed behavior
The report should contain one header followed by data from visible source sheets, without including hidden sheets or the report sheet itself.
Before you start

Create a worksheet named YEARLY REPORT and ensure each source sheet has the same column layout, with headers in row 1. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm), and enable macros only if you trust the code and workbook.

Solution 1Recommended

Append visible worksheet data with a qualified VBA macro

Use this macro when source sheets share a header layout and data starts in row 2.

The macro uses explicit worksheet references and finds the last used row and column instead of relying on End(xlDown) or End(xlToRight). It copies values, not formatting or formulas.

If YEARLY REPORT already contains data, new rows are appended without adding another header. Running the macro again appends the source data again.

1
Open the VBA editor

In the desktop version of Excel, open the workbook and choose Developer > Visual Basic, then select Insert > Module.

2
Add the macro

Paste this code into the module: Sub CombineVisibleSheets() Dim wb As Workbook Dim ws As Worksheet Dim report As Worksheet Dim lastCell As Range Dim lastRow As Long Dim lastCol As Long Dim nextRow As Long Set wb = ThisWorkbook Set report = wb.Worksheets("YEARLY REPORT") For Each ws In wb.Worksheets If ws.Visible = xlSheetVisible And ws.Name <> report.Name Then Set lastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False) If Not lastCell Is Nothing Then lastRow = lastCell.Row If lastRow > 1 Then Set lastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False) lastCol = lastCell.Column Set lastCell = report.Cells.Find(What:="*", After:=report.Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False) If lastCell Is Nothing Then report.Cells(1, 1).Resize(1, lastCol).Value = ws.Cells(1, 1).Resize(1, lastCol).Value nextRow = 2 Else nextRow = lastCell.Row + 1 End If report.Cells(nextRow, 1).Resize(lastRow - 1, lastCol).Value = ws.Cells(2, 1).Resize(lastRow - 1, lastCol).Value End If End If End If Next ws End Sub

3
Run and check the report

In Excel, choose Developer > Macros, select CombineVisibleSheets, and click Run. Open YEARLY REPORT and check the header and appended rows.

Append visible worksheet data with a qualified VBA macro
Workbook and sheet names: ThisWorkbook refers to the workbook containing the macro. Store the macro in the source workbook and use the exact sheet name YEARLY REPORT.
Free Microsoft Office alternative

Work with Excel reports in WPS Spreadsheets

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Excel formats, making it easy to move everyday report work between office suites. It does not directly fix Excel VBA issues; macro support and compatibility can vary by WPS version.

Review and edit consolidated spreadsheet reports in a familiar interfaceOpen and work with Microsoft Excel XLS and XLSX filesUse a free, lightweight office suite for everyday spreadsheet tasks
QA img-9

Frequently Asked Questions

Does the macro include hidden worksheets?

No. The condition ws.Visible = xlSheetVisible includes only visible worksheets and also excludes YEARLY REPORT.

What happens if a source sheet is empty or has only a header?

The macro skips it. A source sheet must have content below row 1 for its rows to be appended.

Will the macro copy formulas and formatting?

No. The assignment uses Value, so it copies displayed values rather than formulas or formatting.

Why does the report contain duplicate rows after I run the macro twice?

The macro appends data each time it runs and does not track previous imports. Review or clear existing report contents before rerunning it if you need a fresh report.