VBA Copy range from another workbook if cell is not blan

jakel27

New Member
Joined
Jun 1, 2022
Messages
39
Office Version
  1. 2019
Platform
  1. Windows
Hi all,

I want to copy values from Workbook B to Workbook A but only the rows in the range without a blank cell in column U in workbook B (S2:U5) in my code.

I would like the range to not be defined, but rather to the last row in column U. Then paste these values to Workbook A starting from cell L5.

------------------------------------------------------------------------------------------------ Code 1 (works without dynamic range)

Workbooks.Open ("Workbook B.xlsm") 'This is my file location for Workbook B

Workbooks("Workbook B.xlsm").Sheets("Timestamp").Range("S2:U5").Copy 'This is my file location for Workbook B

ThisWorkbook.Sheets("Toyota").Range("L5").PasteSpecial Paste:=xlPasteValues

----------------------------------------------------------------------------------------------- Code 2 (my attempt but doesn't work)
Sub Timestamp()

Workbooks.Open ("Workbook B") 'This is my file location for Workbook B

Dim erow As Long, lastrow As Long, i As Long

lastrow = Timestamp.Cells(Rows.Count, 21).End(xlUp).Row

For i = 2 To lastrow
If Timestamp.Cells(i, 21) <> "" Then
Sheets("Timestamp").Range(Cells(i, 19), Cells(i, 21)).Copy
ThisWorkbook.Sheets("Toyota").Activate
ThisWorkbook.Sheets("Toyota").Range("L5").PasteSpecial Paste:=xlPasteValues
End If

Next i

End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Can you show us an example of what your data looks like, along with your expected results?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Hi Joe,

Workbook B - I want to copy range S:U for rows where U is not blank.
1654638080851.png


I want to paste these range values into Workbook A starting from cell L5. ie. In this scenerio I only wanted to see VP591 and VP598
1654638209452.png
 

Attachments

  • 1654638193339.png
    1654638193339.png
    8.2 KB · Views: 0
Upvote 0
If this original code really works:
VBA Code:
Workbooks.Open ("Workbook B.xlsm") 'This is my file location for Workbook B

Workbooks("Workbook B.xlsm").Sheets("Timestamp").Range("S2:U5").Copy 'This is my file location for Workbook B

ThisWorkbook.Sheets("Toyota").Range("L5").PasteSpecial Paste:=xlPasteValues

and if the there are no blanks in column U in the middle of your data (they are all at the bottom like you showed in your example), then try this:

VBA Code:
Workbooks.Open ("Workbook B.xlsm") 'This is my file location for Workbook B

Workbooks("Workbook B.xlsm").Sheets("Timestamp").Range("S2:U" & Cells(Rows.Count, "U").End(xlUp).Row).Copy 'This is my file location for Workbook B

ThisWorkbook.Sheets("Toyota").Range("L5").PasteSpecial Paste:=xlPasteValues
 
Last edited:
Upvote 0
Solution
If this original code really works:
VBA Code:
Workbooks.Open ("Workbook B.xlsm") 'This is my file location for Workbook B

Workbooks("Workbook B.xlsm").Sheets("Timestamp").Range("S2:U5").Copy 'This is my file location for Workbook B

ThisWorkbook.Sheets("Toyota").Range("L5").PasteSpecial Paste:=xlPasteValues

and if the there are no blanks in column U in the middle of your data (they are all at the bottom like you showed in your example), then try this:

VBA Code:
Workbooks.Open ("Workbook B.xlsm") 'This is my file location for Workbook B

Workbooks("Workbook B.xlsm").Sheets("Timestamp").Range("S2:U" & Cells(Rows.Count, "U").End(xlUp).Row).Copy 'This is my file location for Workbook B

ThisWorkbook.Sheets("Toyota").Range("L5").PasteSpecial Paste:=xlPasteValues
Perfect thank you.

I don't believe I will have any gaps in my rows. I'll keep testing for it, but if there is a gap in rows, is it hard to adapt this code to accommodate this?

I won't require it atm but will ask forum for help probably if I do.

Thanks again for your help
 
Last edited by a moderator:
Upvote 0
If this original code really works:
VBA Code:
Workbooks.Open ("Workbook B.xlsm") 'This is my file location for Workbook B

Workbooks("Workbook B.xlsm").Sheets("Timestamp").Range("S2:U5").Copy 'This is my file location for Workbook B

ThisWorkbook.Sheets("Toyota").Range("L5").PasteSpecial Paste:=xlPasteValues

and if the there are no blanks in column U in the middle of your data (they are all at the bottom like you showed in your example), then try this:

VBA Code:
Workbooks.Open ("Workbook B.xlsm") 'This is my file location for Workbook B

Workbooks("Workbook B.xlsm").Sheets("Timestamp").Range("S2:U" & Cells(Rows.Count, "U").End(xlUp).Row).Copy 'This is my file location for Workbook B

ThisWorkbook.Sheets("Toyota").Range("L5").PasteSpecial Paste:=xlPasteValues
Turns out I do have blank rows in my U column. Is there a way to copy the rows that have values only?
 
Last edited by a moderator:
Upvote 0
Let's copy it all over, and then delete the blank rows afterwards, something like this (this avoids having to use loops, which are usually slower and less efficient):
VBA Code:
Workbooks.Open ("Workbook B.xlsm") 'This is my file location for Workbook B

Workbooks("Workbook B.xlsm").Sheets("Timestamp").Range("S2:U" & Cells(Rows.Count, "U").End(xlUp).Row).Copy 'This is my file location for Workbook B

ThisWorkbook.Sheets("Toyota").Range("L5").PasteSpecial Paste:=xlPasteValues

Dim lastrow As Long
    
'Find last row with data in column N
lastrow = ThisWorkbook.Sheets("Toyota").Cells(Rows.Count, "N").End(xlUp).Row

'Delete rows that have blanks in column N
ThisWorkbook.Sheets("Toyota").Range("N5:N" & lastrow).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
 
Upvote 0

Forum statistics

Threads
1,214,419
Messages
6,119,389
Members
448,891
Latest member
tpierce

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