'VBA to only retrieve rows with 1 or more populated cells
I have a code that retrieves data from those sheets that are not hidden to a summary sheet. Issue is that the other sheets contain both list drop down menus and some of them contain IF statements based on the input. But no "real" data populated into those cells if not specified.
The summary sheet is expected to only retrieve those rows with actual data specified in it. The sheets contain borders, so perhaps that could be an issue?
Sub CreateSummary()
Const dName As String = "Summary"
Const dFirstCellAddress As String = "A1"
Const sColsAddress As String = "B:N"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Application.ScreenUpdating = False
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
dws.Move After:=wb.Sheets(wb.Sheets.Count)
dws.UsedRange.Clear
Dim dfCell As Range: Set dfCell = dws.Range(dFirstCellAddress)
Dim sws As Worksheet
Dim srg As Range
Dim i As Long
Dim n As Long
For i = 1 To Worksheets.Count - 1 ' exclude 'dws' (last worksheet)
Set sws = wb.Worksheets(i)
If sws.Visible = xlSheetVisible Then
n = n + 1
Set srg = Intersect(sws.UsedRange, sws.Columns(sColsAddress))
If n > 1 Then ' all but the first worksheet; exclude headers
Set srg = srg.Resize(srg.Rows.Count - 1).Offset(1)
Else ' first worksheet
srg.Rows(1).Copy
dfCell.PasteSpecial xlPasteColumnWidths
End If
srg.Copy dfCell
Set dfCell = dfCell.Offset(srg.Rows.Count)
'Else ' worksheet is not visible; do nothing
End If
Next i
Application.Goto Reference:=dws.Cells(1), Scroll:=True
Application.ScreenUpdating = True
MsgBox "Summary created.", vbInformation
End Sub
Solution 1:[1]
This Excel function within a VBA code can determine if a row is empty or it has atleast 1 cell filled.
If WorksheetFunctions.CountA(sws.Rows(rownumber)) > 0 then
'This row has value in atleast a column
End If
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | OfficeTricks.Com |
