Dynamic Named Range script is creating range based off the wrong column

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
485
Office Version
  1. 365
Platform
  1. Windows
Good morning. I have been working with this piece of code to create a named range. I want to only scan Column L (12) for any text value and create the named range that it based only on values (ignoring formulas). The script below does create a named range with the correct name, but it seems to be determining the last row with a value on Column A instead of Column L which creates a named range with unwanted extra spaces.

range1.png


range2.png


Here's the code:

VBA Code:
Sub SendFloorRequestRange()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
 
    Dim myWorksheet As Worksheet
 
    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myFirstColumn As Long
    Dim myLastColumn As Long
 
    Dim myNamedRangeDynamic As Range
 
    Dim myRangeName As String
 
    Set myWorksheet = ThisWorkbook.Worksheets("SendFloorRequest")
 
    myFirstRow = 1
    myFirstColumn = 12
 
    myRangeName = "FloorPlanRequestRange"
 
    With myWorksheet.Cells
 
        myLastRow = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        myLastColumn = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
 
        Set myNamedRangeDynamic = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
 
End With
 
    ThisWorkbook.Names.Add Name:=myRangeName, RefersTo:=myNamedRangeDynamic
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
 
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try replacing:
VBA Code:
myLastRow = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

with:
VBA Code:
myLastRow= myWorksheet.Range("L" & Rows.Count).End(xlUp).Row
 
Upvote 0
Try replacing:
VBA Code:
myLastRow = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

with:
VBA Code:
myLastRow= myWorksheet.Range("L" & Rows.Count).End(xlUp).Row

That is keying off the formulas, not the text. How do I ignore the formulas using your code?
 
Upvote 0
How about:
VBA Code:
myLastRow= myWorksheet.[match(2,1/(l:l<>""))]

Those are 'L's after the 2,1/ part. ;)
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,178
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