Requirement:
The user has this code that copy range and paste to other sheets:
Sub CopyRange() Application.ScreenUpdating = False Dim copySheet As Worksheet Dim pasteSheet As Worksheet Set copySheet = Worksheets("Data") Set pasteSheet = Worksheets("Report") copySheet.Range("A105:F110").Copy pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues Application.CutCopyMode = False Application.ScreenUpdating = True End Sub
However, when the copy range has only values in range A105:F105 and range below are blanks, it copies all range to worksheet("Report"). And when i add another data, it started in the row where the upper row is empty.
What is the missing code that only copy range with values and paste it to worksheet("Report")? So that when the user adds another data, it will paste next to it...
Solution:
Code:
Sub CopyRange() Dim x, y(), i As Long, ii As Long x = Sheets("Data").[a105:f110] For i = 1 To UBound(x, 1) If x(i, 1) <> "" Then ReDim Preserve y(1 To 6, 1 To i) For ii = 1 To 6 y(ii, i) = x(i, ii) Next Else: Exit For End If Next With Sheets("Report") .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(UBound(y, 2), 6) = Application.Transpose(y) End With End Sub
Solution from KjBox.
my loop less alternative
Option Explicit Sub Test() With Sheets("Data").Range("A104:F110") .AutoFilter .AutoFilter Field:=1, Criteria1:="<>" .Offset(1, 0).Copy End With With Sheets("Report") .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False End With With Sheets("Data").Range("A104:F110") .AutoFilter End With End Sub
Solution from pike.
Obtained from the OzGrid Help Forum.
Solution provided by KjBox and pike.
See also: Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions and Index to new resources and reference sheets
See also:
How to find last non blank cell & not affect split screen view |
How to compare two columns in excel, inserting blank rows moving associated data |
How to end a macro if cell blank or continue if populated |
How to auto populate blank cells using VBA |
Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.