VBA: Create a named ranged and delete a row from said name range based on value

IIII

New Member
Joined
Jan 26, 2021
Messages
18
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Hi,

I have this small section that I'm hoping to create a name-range from. In the sheet below, it is the bordered area with red text.

I've been scratching my head trying to use
VBA Code:
Cells(Range("A1").End(xlDown).Row + 2, 2)
- which gets me to the first cell in the table, in this case, B19, however, I'm not sure how to do the rest using lines like
VBA Code:
Cells(Rows.Count, "A").End(xlUp).Row
as I'm not sure what to use as the guide to find the last row in this situation. Please note that the column lengths of the data above the bordered area are dynamic.

Also, with the bordered area below, is it possible to delete rows for anything that has a value of zero? - I do not mind if this doesn't get solved as the above query is my main problem. Thanks anyway.

Please advise if further information is required. Many thanks in advance for any help with this one. Cheers.

Book1
ABCDEFG
1DateShiftstart timeFinish TimeHoursKmsCosts
2Wed, 06 Apr 2022Lorem ipsum8:00 AM12:00 PM4340.50
3Wed, 06 Apr 2022Lorem ipsum12:30 PM3:30 PM3220.50
4Thu, 07 Apr 2022Lorem ipsum9:00 AM10:00 AM100.00
5Thu, 07 Apr 2022TEST SERVICES10:00 AM1:00 PM300.00
6Fri, 08 Apr 2022TEST SERVICES9:00 AM12:30 PM3.500.00
7Sat, 09 Apr 2022Lorem ipsum6:00 AM9:30 AM3.51680.50
8Sat, 09 Apr 2022TEST SERVICES10:00 AM1:00 PM300.50
9Mon, 11 Apr 2022Lorem ipsum9:00 AM10:00 AM100.00
10Mon, 11 Apr 2022Lorem ipsum10:00 AM4:00 PM61990.50
11Tue, 12 Apr 2022Lorem ipsum9:00 AM3:30 PM6.51430.50
12Wed, 13 Apr 2022Lorem ipsum9:00 AM12:00 PM31310.50
13Wed, 13 Apr 2022Lorem ipsum12:30 PM3:30 PM3220.50
14Thu, 14 Apr 2022Lorem ipsum9:00 AM10:00 AM100.00
15Thu, 14 Apr 2022TEST SERVICES10:00 AM1:30 PM3.500.00
16Fri, 15 Apr 2022Lorem ipsum9:30 AM1:30 PM4200.50
17Sat, 16 Apr 2022Lorem ipsum6:00 AM9:30 AM3.5640.50
18
19Weekday Hours39
20Weekday Hours3.5
21Weekday Hours8.5
22Weekday Hours1.5
23Weekday Hours0
24Weekday Hours0
25Weekday Hours0
26Weekday Hours0
27Weekday Hours803
28
Michael Scarn
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I am not sure if you mean an Excel Named Range or just a VBA variable name. I have assumed the latter for now. See if this gets you started.
VBA Code:
Sub GetRange()

    Dim sht As Worksheet
    Dim rngWkDayHrs As Range
    Dim FirstRow As Long, LastRow As Long, i As Long
    
    Set sht = ActiveSheet
    With sht
        FirstRow = .Cells(1, "A").End(xlDown).Row + 2
        LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
        Set rngWkDayHrs = .Range(.Cells(FirstRow, "B"), .Cells(LastRow, "C"))
    
        For i = LastRow To FirstRow Step -1
            If .Cells(i, "C") = 0 Then .Cells(i, "C").EntireRow.Delete
        Next i
    End With

End Sub
 
Upvote 0
I am not sure if you mean an Excel Named Range or just a VBA variable name. I have assumed the latter for now. See if this gets you started.
VBA Code:
Sub GetRange()

    Dim sht As Worksheet
    Dim rngWkDayHrs As Range
    Dim FirstRow As Long, LastRow As Long, i As Long
   
    Set sht = ActiveSheet
    With sht
        FirstRow = .Cells(1, "A").End(xlDown).Row + 2
        LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
        Set rngWkDayHrs = .Range(.Cells(FirstRow, "B"), .Cells(LastRow, "C"))
   
        For i = LastRow To FirstRow Step -1
            If .Cells(i, "C") = 0 Then .Cells(i, "C").EntireRow.Delete
        Next i
    End With

End Sub

Hi Alex - Thank you for responding.

My apologies for leaving out those details in my initial post. What I actually need is an Excel Named Range for the two columns in the bordered area in my example sheet like:

VBA Code:
Range("range" & 'Some range for last row').Name = "Weekday"
Range("range" & 'Some range for last row').Name = "Hours"

I just don't know how to do the above named-range VBA code, for this particular section.

Thanks again for your help.
 
Upvote 0
I added creating the names to the previous code:
VBA Code:
Sub GetRange()

    Dim wb As Workbook
    Dim sht As Worksheet
    Dim rngWkDayHrs As Range
    Dim FirstRow As Long, LastRow As Long, i As Long
    
    Set wb = ActiveWorkbook
    Set sht = ActiveSheet
    With sht
        FirstRow = .Cells(1, "A").End(xlDown).Row + 2
        LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
        Set rngWkDayHrs = .Range(.Cells(FirstRow, "B"), .Cells(LastRow, "C"))
        
        wb.Names.Add Name:="Weekday", RefersTo:=rngWkDayHrs.Columns(1)
        wb.Names.Add Name:="Hours", RefersTo:=rngWkDayHrs.Columns(2)
    
        For i = LastRow To FirstRow Step -1
            If .Cells(i, "C") = 0 Then .Cells(i, "C").EntireRow.Delete
        Next i
    End With
    
End Sub
 
Upvote 0
Solution
I added creating the names to the previous code:
VBA Code:
Sub GetRange()

    Dim wb As Workbook
    Dim sht As Worksheet
    Dim rngWkDayHrs As Range
    Dim FirstRow As Long, LastRow As Long, i As Long
   
    Set wb = ActiveWorkbook
    Set sht = ActiveSheet
    With sht
        FirstRow = .Cells(1, "A").End(xlDown).Row + 2
        LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
        Set rngWkDayHrs = .Range(.Cells(FirstRow, "B"), .Cells(LastRow, "C"))
       
        wb.Names.Add Name:="Weekday", RefersTo:=rngWkDayHrs.Columns(1)
        wb.Names.Add Name:="Hours", RefersTo:=rngWkDayHrs.Columns(2)
   
        For i = LastRow To FirstRow Step -1
            If .Cells(i, "C") = 0 Then .Cells(i, "C").EntireRow.Delete
        Next i
    End With
   
End Sub

@Alex Blakenburg - Sorry I could've just added that and wasn't paying attention properly...need to sleep. Anyhow, mate, you are a legend!! This works like a charm! Thank you very much for solving both issues for me! Cheers 🍻
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
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