Pasted to the last row

Athopp

New Member
Joined
Jul 12, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,
The purpose of this code is to copy all values generated from a formula in column 'B' and paste those values in column 'D'. Next the code copies all the values generated from a formula in column 'C' to the next available row in column 'D'. In the end, I should see all values for column 'B' and 'C' in column 'D' with no blank rows.

The problem is: the code seems to create tones of blank rows in column D between the values generated from B and C.

Example of desired output below:

Column BColumn CColumn D
TextB1TextC1TextB1
TextB2TextC2TextB2
TextB3TextB3
TextB4TextB4
TextC1
TextC2

Code below (except of relevant pieces; no Dims)

ActiveSheet.Range("$A$1:$E$50").AutoFilter Field:=2, Criteria1:="<>"
Range("B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveWindow.LargeScroll ToRight:=1
ActiveWindow.SmallScroll ToRight:=1
Range("D2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
ActiveWindow.SmallScroll ToRight:=-2
Call Application.Calculate
ActiveSheet.Range("$A$1:$E$50").AutoFilter Field:=2
LastRow = ActiveWorkbook.Worksheets("Rename_Folders").Cells(Rows.Count, 4).End(xlUp).Row
NextRowExport = LastRow + 1

ActiveWorkbook.Worksheets("Rename_Folders").Range("C2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

ActiveWorkbook.Worksheets("Rename_Folders").Range("D" & NextRowExport).PasteSpecial xlPasteValues
Call Application.Calculate
Columns("B:C").EntireColumn.Hidden = True
Range("D2").Activate
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
VBA Code:
ActiveSheet.Range("$A$1:$E$50").AutoFilter Field:=2, Criteria1:="<>"
Range("B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveWindow.LargeScroll ToRight:=1
ActiveWindow.SmallScroll ToRight:=1
Range("D2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
ActiveWindow.SmallScroll ToRight:=-2
Call Application.Calculate
ActiveSheet.Range("$A$1:$E$50").AutoFilter Field:=2
LastRow = ActiveWorkbook.Worksheets("Rename_Folders").Cells(Rows.Count, 4).End(xlUp).Row
NextRowExport = LastRow + 1

ActiveWorkbook.Worksheets("Rename_Folders").Range("C2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

'Remove this line - ActiveWorkbook.Worksheets("Rename_Folders").Range("D" & NextRowExport).PasteSpecial xlPasteValues

'checks if D2 is blank, if so pastes values in D2
If IsEmpty(Range("d2")) Then
    Range("d2").PasteSpecial xlPasteValues
    Else
        If IsEmpty(Range("d2").Offset(1)) Then 'checks if D3 is blank, if so pastes new data in D3 (not checking for this causes an error)
            Range("d2").Offset(1).PasteSpecial xlPasteValues
        Else
            Range("d2").End(xlDown).Offset(1).PasteSpecial xlPasteValues 'goes to bottom of data set, moves down 1 row then pastes values
        End If
End If

Call Application.Calculate
Columns("B:C").EntireColumn.Hidden = True
Range("D2").Activate
 
Upvote 0
Assuming Row 2 is the starting row of your data ... How about:

VBA Code:
Sub Test()
'
    Range("B2:B50").SpecialCells(xlCellTypeBlanks).Delete                                                               ' Delete Blank cells in column B
    Range("D2").Resize(WorksheetFunction.CountA(Range("B2:B50")), 1) = _
            Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row).Value                                                 ' Copy cells from Column B to Column D
'
    Call Application.Calculate
'
    Range("C2:C50").SpecialCells(xlCellTypeBlanks).Delete                                                               ' Delete Blank cells in column C
    Range("D" & Range("D" & Rows.Count).End(xlUp).Row + 1).Resize(WorksheetFunction.CountA(Range("C2:C50")), 1) = _
            Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row).Value                                                 ' Copy cells from Column C to Column D
'
    Call Application.Calculate
'
    Columns("B:C").EntireColumn.Hidden = True
'
    Range("D2").Activate
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top