Copying ranges while eliminating blanks yields unexpected results

Unique65

New Member
Joined
Nov 30, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I cannot find anything anywhere that helps me to address what is happening here.
I have data that I have sorted into four columns. The four columns are up to 1000 'rows' of four cells.
Once sorted, the code is supposed to copy that coalesced data to a new range of cells, but only the range of the four cells, IF there are no blanks in that range of four cells.
Basically I am trying to remove any "rows" (of 4 cells) that contain non-visible (or no) data. The rows marked with a red "X" in the left grouping.
Trying to eliminate.png


What ends up happening is that it fills the new location with the last word that was pasted into the first location, under the previous code.
In the image below, the last cell that was populated in the creation of the left grouping, was the word "Acid".
When attempting to remove the rows with blanks, only that word is used to fill the entire available range in the new area. (Far exceeding the length of the expected result.
Weird result.png


The right Red arrow is the last code where "Acid" is placed.
The bracketed code is where I am trying to remove the spaces (rows with a red "x" in the first image above.
I have tried this way, as well as more complex code, none of it works.
The more complex code just copies and pastes the existing data, including the rows with the spaces.

VBA Code.png


Any help would be greatly appreciated!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Assuming column BM:BP are helper columns and you don't actually need them and the real output is to PMIF Workspace A1. You could give this a try.

VBA Code:
Sub CopyCompletedRows()

    Dim shtSrc As Worksheet, shtDest As Worksheet
    Dim rngSrc As Range, rngDest As Range
    Dim rowLastSrc As Long
    Dim arrSrc As Variant, arrDest As Variant
    Dim iSrc As Long, jSrc As Long, iDest As Long
    Dim IsComplete As Boolean
    
    Set shtSrc = Worksheets("BOM")
    Set shtDest = Worksheets("PMIF Workspace")
    
    With shtSrc.Columns("BA:BD")
        rowLastSrc = .Cells.Find(What:="*" _
                    , Lookat:=xlPart _
                    , LookIn:=xlFormulas _
                    , After:=.Cells(1) _
                    , searchorder:=xlByRows _
                    , searchdirection:=xlPrevious).Row
        Set rngSrc = .Resize(rowLastSrc)
        arrSrc = rngSrc.Value
    End With
    
    Set rngDest = shtDest.Range("A1")
    
    ReDim arrDest(1 To UBound(arrSrc, 1), 1 To UBound(arrSrc, 2))
    
    
    For iSrc = 1 To UBound(arrSrc)
        IsComplete = True
        For jSrc = 1 To UBound(arrSrc, 2)
            If arrSrc(iSrc, jSrc) = "" Then
                IsComplete = False
                Exit For
            End If
        Next jSrc
        If IsComplete Then
            iDest = iDest + 1
            For jSrc = 1 To UBound(arrSrc, 2)
               arrDest(iDest, jSrc) = arrSrc(iSrc, jSrc)
            Next jSrc
        End If
    Next iSrc
    
    rngDest.Resize(iDest, UBound(arrDest, 2)).Value = arrDest
    
End Sub
 
Upvote 0
Solution
Assuming column BM:BP are helper columns and you don't actually need them and the real output is to PMIF Workspace A1. You could give this a try.

VBA Code:
Sub CopyCompletedRows()

    Dim shtSrc As Worksheet, shtDest As Worksheet
    Dim rngSrc As Range, rngDest As Range
    Dim rowLastSrc As Long
    Dim arrSrc As Variant, arrDest As Variant
    Dim iSrc As Long, jSrc As Long, iDest As Long
    Dim IsComplete As Boolean
   
    Set shtSrc = Worksheets("BOM")
    Set shtDest = Worksheets("PMIF Workspace")
   
    With shtSrc.Columns("BA:BD")
        rowLastSrc = .Cells.Find(What:="*" _
                    , Lookat:=xlPart _
                    , LookIn:=xlFormulas _
                    , After:=.Cells(1) _
                    , searchorder:=xlByRows _
                    , searchdirection:=xlPrevious).Row
        Set rngSrc = .Resize(rowLastSrc)
        arrSrc = rngSrc.Value
    End With
   
    Set rngDest = shtDest.Range("A1")
   
    ReDim arrDest(1 To UBound(arrSrc, 1), 1 To UBound(arrSrc, 2))
   
   
    For iSrc = 1 To UBound(arrSrc)
        IsComplete = True
        For jSrc = 1 To UBound(arrSrc, 2)
            If arrSrc(iSrc, jSrc) = "" Then
                IsComplete = False
                Exit For
            End If
        Next jSrc
        If IsComplete Then
            iDest = iDest + 1
            For jSrc = 1 To UBound(arrSrc, 2)
               arrDest(iDest, jSrc) = arrSrc(iSrc, jSrc)
            Next jSrc
        End If
    Next iSrc
   
    rngDest.Resize(iDest, UBound(arrDest, 2)).Value = arrDest
   
End Sub
That worked! Thank you!
Now.... my enquiring mind wants to know...
Why didn't what I had initially work?
 
Upvote 0
That worked! Thank you!
Now.... my enquiring mind wants to know...
Why didn't what I had initially work?
Your copy code only has a paste, I can't see the copy command that precedes it. Also there seems to be nothing there that checks that all 4 cells in each row have a value.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,161
Members
453,021
Latest member
Justyna P

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