• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

delete all non-empty rows

ysherriff

Member
Can someone help me with a more efficient way to use the last row statement. I keep havign to change the destination cell number everytime if i use a different workbook or parameter. The issue i am having is highlighed in red.

LastRow = ActiveSheet.Range("D94").End(xlUp).Row
If LastRow > 1 Then
sh.Range("B9:L" & LastRow).Copy

'activate generator workbook
currentWB.Activate

'activate master worksheet
TargetSh.Activate

TargetSh.Range(DestCell.Address).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set DestCell = DestCell.Offset(LastRow - 10)
dataWB.Close False


I have attached the spreadsheet. The macro name is mod_consolidate

Thank you
 
There is no red highlighting!

Untested, but try changing:
Code:
sh.Range("B9:L" & LastRow).Copy
to:
Code:
set SourceRng = sh.Range("B9:L" & LastRow)
SourceRng.copy

later, change:
Code:
Set DestCell = DestCell.Offset(LastRow - 10)
to:
Code:
Set DestCell = DestCell.Offset(SourceRng.rows.count)

As an aside, again untested, you might be able to condense:
Code:
'activate generator workbook
currentWB.Activate

'activate master worksheet
TargetSh.Activate

TargetSh.Range(DestCell.Address).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

to one line:
Code:
DestCell.PasteSpecial Paste:=xlPasteValues

The main unknown I have is the variability of the line:
sh.Range("B9:L" & LastRow).Copy above, but in the file you attached:
sh.Range("B6:N" & LastRow).Copy

Two differences; the width and the start row. Are the source sheets all different? Is there someway the start row and width of the range to copy can be determined from the sheet itself? There may be a header cell we could look for? Maybe width could be determined by a .End(xlToRight) or .End(xlToLeft). Perhaps upload 2 samples of source sheets so that we can look for a way to do this.
 
Back
Top