VBA to Copy Rows Until Blank Row

Shanaka Fernando

New Member
Joined
Sep 30, 2021
Messages
12
Office Version
  1. 2010
Platform
  1. Windows
how to copy row value until blank row to my VBA Code.( only highlighted area)

1634128919677.png


Sub MoveToDestination()

Dim destinationSheet As Worksheet, destRow As Long, destCol As Variant
Dim ws As Worksheet, wsRow As Long, wsCol As Variant
Dim columnHeader As Variant

Set destinationSheet = ThisWorkbook.Worksheets("data")

For Each ws In ThisWorkbook.Worksheets
If Not ws Is destinationSheet Then
For Each columnHeader In Array("ITEM#", "Item description")
With ws
wsCol = Application.Match(columnHeader, .Rows(17), 0)
If Not IsError(wsCol) Then
wsRow = .Cells(.Rows.Count, wsCol).End(xlUp).Row
destCol = Application.Match(columnHeader, destinationSheet.Rows(1), 0)
destRow = destinationSheet.Cells(destinationSheet.Rows.Count, destCol).End(xlUp).Row + 1
.Range(.Cells(18, wsCol), .Cells(wsRow, wsCol)).Copy destinationSheet.Cells(destRow, destCol)
Else
MsgBox "Column heading " & columnHeader & " not found in row 1 of " & .Name
End If
End With
Next
End If
Next
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
If Item Description is always next to the Item# then you can just use Resize.
Note that I changed If Not ws Is destinationSheet Then to If Not ws.Name = destinationSheet.Name Then since it causing error. I wonder if yours was working.
VBA Code:
Sub MoveToDestination()

Dim destinationSheet As Worksheet, destRow As Long, destCol As Variant, x As Long
Dim ws As Worksheet, wsRow As Long, wsCol As Variant
Dim columnHeader As Variant

Set destinationSheet = ThisWorkbook.Worksheets("data")

For Each ws In ThisWorkbook.Worksheets
    If Not ws.Name = destinationSheet.Name Then
        For Each columnHeader In Array("ITEM#")
            With ws
                wsCol = Application.Match(columnHeader, .Rows(17), 0)
                If Not IsError(wsCol) Then
                    wsRow = .Cells(.Rows.Count, wsCol).End(xlUp).Row
                    destCol = Application.Match(columnHeader, destinationSheet.Rows(1), 0)
                    destRow = destinationSheet.Cells(destinationSheet.Rows.Count, destCol).End(xlUp).Row + 1
                    .Cells(18, wsCol).Resize(wsRow - 18 + 1, 2).Copy destinationSheet.Cells(destRow, destCol)
                Else
                    MsgBox "Column heading " & columnHeader & " not found in row 1 of " & .Name
                End If
            End With
        Next
    End If
Next

End Sub
 
Upvote 0
THANKS FOR YOUR REPLY

i need to change the above my vba code to copy row value until first blank row

example - need copy
header "Item description" until "women bikini 2p hang"

how to change my code

1634136717639.png
 
Upvote 0
Try this
Change
.Cells(18, wsCol).Resize(wsRow - 18 + 1, 2).Copy destinationSheet.Cells(destRow, destCol)

to
.Cells(17, wsCol).Resize(wsRow - 17 + 1, 2).Copy destinationSheet.Cells(destRow, destCol)
 
Last edited:
Upvote 0
thanks for your reply
' it s not working
please support me

i need stop copy range until first blank row in column "A" how can i change above my vba code
ex - no need to copy values in highlighted area

1634542757307.png
 
Upvote 0
I was looking at your code and your sheet showed your data started on row 17 like below
PUMA.xlsm
ABCDEF
16
17BARCODE#ITEM#Item descriptionSizexs
18'8720245131049F603021001009010PUMA WOMEN BIKINI 2P HANGS
19'8720245131056F603021001009020PUMA WOMEN BIKINI 2P HANGM
20'8720245131063F603021001009030PUMA WOMEN BIKINI 2P HANGL
21'8720245131070F603021001009040PUMA WOMEN BIKINI 2P HANGXL
22
23Total qty :336
24Total cn :4
25Total gross weight:0
26Total net weight:-4.8
27Total net net weight:-5
28Total volume in cubic mts:0
29
Sheet1


Your destination sheet is named Data. which I believed row 2 (probably excluding headers)

Do you want to copy column A, B, and C only? Like this? Please clarify
PUMA.xlsm
ABCDE
1BARCODE#ITEM#Item descriptionSizexs
2'8720245131049F603021001009010PUMA WOMEN BIKINI 2P HANG
3'8720245131056F603021001009020PUMA WOMEN BIKINI 2P HANG
4'8720245131063F603021001009030PUMA WOMEN BIKINI 2P HANG
5'8720245131070F603021001009040PUMA WOMEN BIKINI 2P HANG
6
data
 
Upvote 0
my destination sheet is Data

1634569324676.png


row 17 is header of all sheets.( i have multiple sheet with a difference sheet name in the workbook and the worksheets are changed (with name) Daily)

* no need copy below area (example - stop copy row column A until 1st blank row). i have attached two sheet Picture.

1634569231458.png


1634568707402.png


1634569147470.png
 

Attachments

  • 1634568809384.png
    1634568809384.png
    188.9 KB · Views: 8
Upvote 0
Sorry for late response. I was on leave yesterday.

For this line
VBA Code:
wsCol = Application.Match(columnHeader, .Rows(17), 0)
      If Not IsError(wsCol) Then
If columnHeader is not found, the execution will stop right there. So, there is no chance that If Not IsError(wsCol) will be executed.

Therefore I rewrote the code.
VBA Code:
Option Compare Text

Sub MoveToDestination()

Dim wsRow As Long, wsCol As Long, DestRow As Long, DestCol As Long
Dim colHeader As Variant, ArryColDest() As Variant
Dim rngHeader As Range, rngDest As Range
Dim ws As Worksheet, wsDest As Worksheet

Application.ScreenUpdating = False

Set wsDest = ActiveWorkbook.Worksheets("Data")
Set rngDest = wsDest.Range("A1", "C1")

' Setup Destination Header
wsDest.Range("A1") = "Item Description"
wsDest.Range("B1") = "Item #"
wsDest.Range("C1") = "Color"

For Each ws In ActiveWorkbook.Sheets
    Set rngHeader = ws.Range("A17", "M17")
    DestRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Row + 1
    If Not ws.Name = wsDest.Name Then
        For Each colHeader In Array("Item Description", "Item #", "Color")
            DestCol = Application.Match(colHeader, rngDest, 0)
            With ws
                If Not IsError(Application.Match(colHeader, rngHeader, 0)) Then
                    wsCol = Application.Match(colHeader, rngHeader, 0)
                    If Not .Cells(18, wsCol) = "" Then
                        wsRow = .Cells(17, wsCol).End(xlDown).Row
                    End If
                    .Range(.Cells(18, wsCol), .Cells(wsRow, wsCol)).Copy
                    wsDest.Cells(DestRow, DestCol).PasteSpecial (xlPasteValuesAndNumberFormats)
                Else
                    MsgBox "Column heading " & colHeader & " not found in row 1 of " & .Name
                    Err.Clear
                    Exit For
                End If
            End With
        Next
    End If
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,496
Messages
6,113,995
Members
448,539
Latest member
alex78

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