Ensuring blanks cells are truly blank and are not picked up by VBA code finding last row

aldousjg

New Member
Joined
Jan 20, 2021
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I've encountered a problem in a report I am building where the VBA code applying borders to a range ending at the last row of data picks up many rows that are not 'truly blank' underneath the last data value.

Data is being populated with this formula which correctly returns "" if there is no match:

Excel Formula:
=IFERROR(INDEX('ACP Data'!$A:$A,MATCH('Prepared Data'!$A2,'ACP Data'!$D:$D,0),1),"")

The data being fed into this is not a set number of rows, so I have applied this formula all the way down to row 5000 to ensure all data is captured.

This data is then copied into my report sheet using the following VBA code:

VBA Code:
Sheets("Report Data").Activate

    Sheets("Report Data").Range("G2").Select
    Range(Selection, Cells(Rows.Count, "G").End(xlUp)).Select

    Selection.Copy
  
    Sheets("Report").Activate
  
    Sheets("Report").Range("C6").Select
  
    ActiveCell.PasteSpecial Paste:=xlPasteValues, SkipBlanks:=True

Then a border is applied to all data in the report with this VBA code. The intention is for it to only go down to the last data row (row 1176 with my current data) but it is actually going down to row 5004 which I assume is due to the index formula going down to row 5000:

VBA Code:
Dim rngBottomRowStart       As Range
    Dim rngBottomRowEnd         As Range
    Dim rngDataUpperLeftCell    As Range
    Dim dataRange1              As Range

    Set rngDataUpperLeftCell = Sheets("Report").Range("C5")
 
With rngDataUpperLeftCell
        Set rngBottomRowStart = Sheets("Report").Cells(.End(xlDown).Row, .Column)
        Set rngBottomRowEnd = Sheets("Report").Cells(rngBottomRowStart.Row, .End(xlToRight).Column)
    End With
  
    Set dataRange1 = Sheets("Report").Range(rngDataUpperLeftCell, rngBottomRowEnd)
  
    With dataRange1.Borders
    .LineStyle = xlContinuous
    .ColorIndex = 1
    .Weight = xlThin
    End With

Is there a way of ensuring that the borders only go down to the actual last data row?

Please let me know if you need any more info! Thanks in advance.
 
Last edited by a moderator:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try adding this to your initial code
VBA Code:
   Sheets("Report").Range("C6").Select
   ActiveCell.PasteSpecial Paste:=xlPasteValues, SkipBlanks:=True
   With Sheets("Report")
      With .Range("C6", .Range("C" & Rows.Count).End(xlUp))
         .Value = .Value
      End With
   End With
 
Upvote 0
Try
VBA Code:
Sheets("Report").Columns("C").Find("*", , xlValues, , xlByRows, xlPrevious).Row
instead of
VBA Code:
Sheets("Report").Cells(.End(xlDown).Row

Btw, you don't need (want to) to use Select to work with ranges
 
Upvote 0
Hi both,

I have tried Fluff's code suggestion and when applying borders they now go down to row 11 instead of row 5004. I cannot share my actual data as it is confidential but I have added in some random values to show here:

1611233725752.png


All columns can have blanks at any point and be correct - I need my borders to go down to the last row with data in at least 1 column.

I guess I need to amend my borders code which is this:

Dim rngBottomRowStart As Range
Dim rngBottomRowEnd As Range
Dim rngDataUpperLeftCell As Range
Dim dataRange As Range

Set rngDataUpperLeftCell = Sheets("Report").Range("C5")

With rngDataUpperLeftCell
Set rngBottomRowStart = Sheets("Report").Cells(.End(xlDown).Row, .Column)
Set rngBottomRowEnd = Sheets("Report").Cells(rngBottomRowStart.Row, .End(xlToRight).Column)
End With

Set dataRange = Sheets("Report").Range(rngDataUpperLeftCell, rngBottomRowEnd)

With dataRange.Borders
.LineStyle = xlContinuous
.ColorIndex = 1
.Weight = xlThin
End With

Any ideas? Thanks again.
 
Upvote 0
Hi Mark858,

I've copied your suggestion into my code as below:
With rngDataUpperLeftCell
Set rngBottomRowStart = Sheets("Report").Columns("C").Find("*", , xlValues, , xlByRows, xlPrevious).Row, .Column
Set rngBottomRowEnd = Sheets("Report").Cells(rngBottomRowStart.Row, .End(xlToRight).Column)
End With
If I leave the .Column at the end I get an error saying expected end of statement. If I take the .Column out and run the code I get an object required error.

What am I doing wrong here?

Thanks,
 
Upvote 0
You need to remove the .Row ln rngBottomRowStart.Row and the , .Column also the Set =.

I'll write it for you when I get back in.
 
Upvote 0
Try the below and see if it does what you want (I've played with it a bit just as I think it is clearer)

VBA Code:
    Dim rngBottomRowEnd         As Range
    Dim rngDataUpperLeftCell    As Range
    Dim dataRange1              As Range
    Dim LastRow                 As Long
    Dim LastColumn              As Long

    Set rngDataUpperLeftCell = Sheets("Report").Range("C5")
 
    LastRow = Sheets("Report").Columns("C").Find("*", , xlValues, , xlByRows, xlPrevious).Row
    LastColumn = Sheets("Report").Rows(rngDataUpperLeftCell.Row & ":" & LastRow).Find("*", , xlValues, , xlByColumns, xlPrevious).Column
        
    Set rngBottomRowEnd = Sheets("Report").Cells(LastRow, LastColumn)
   
  
    Set dataRange1 = Sheets("Report").Range(rngDataUpperLeftCell, rngBottomRowEnd)
  
    With dataRange1.Borders
        .LineStyle = xlContinuous
        .ColorIndex = 1
        .Weight = xlThin
    End With
 
Upvote 0
Solution

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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