Converting Excel Formula to VBA Code With Dynamic Reference - vba

I have in my code variable named MyCol that gets the number of the column by month that was selected in a userform. for example October is in column U and November is in column V. I have a formula that I recorded and the month that was chosen is part of it but it is a problem because RC format is with specific reference and my variable is an integer.
I want that the formula will be dynamic.
This is the formula (column U means month that was chosen):
=IFNA(IF(VLOOKUP(AA2,sheet1!F:F,1,0)=AA2,U2,0),0)
That is the relavant part of the code:
Dim MonthName As String
Dim myCol As Integer
MonthName = ListMonth.Value
With MainWB.Worksheets("sheet2")
.Activate
.Range("L1:W1").Find(MonthName, , xlValues, xlWhole).Activate
End With
ActiveCell.Select
myCol = Selection.Column
Range("AB2").Select
ActiveCell.FormulaR1C1 = _
"=IFNA(IF(VLOOKUP(RC[-1],sheet1!C[-22],1,0)=RC[-1],RC[" & myCol & "],0),0)"
Range("AB2").AutoFill Destination:=Range("AB2:AB" & MLR), Type:=xlFillDefault

Replace your :
Range("AB2").Select
ActiveCell.FormulaR1C1 = _
"=IFNA(IF(VLOOKUP(RC[-1],sheet1!C[-22],1,0)=RC[-1],RC[" & myCol & "],0),0)"
With:
Range("AB2").FormulaR1C1 = _
"=IFNA(IF(VLOOKUP(RC[-1],sheet1!C[-22],1,0)=RC[-1],RC[-" & Range("AB2").Column - myCol & "],0),0)"
However, if you want to make your code run faster, and also avoid all the unecessary Activate, ActiveCell, Select, try the code below:
Dim FindRng As Range
MonthName = ListMonth.Value
With MainWB.Worksheets("sheet2")
Set FindRng = .Range("L1:W1").Find(MonthName, , xlValues, xlWhole)
End With
If Not FindRng Is Nothing Then
myCol = FindRng.Column
Else ' find was not successful finding the month name
MsgBox "Unable to find " & MonthName, vbCritical
Exit Sub
End If
Range("AB2").FormulaR1C1 = _
"=IFNA(IF(VLOOKUP(RC[-1],sheet1!C[-22],1,0)=RC[-1],RC[-" & Range("AB2").Column - myCol & "],0),0)"

Related

VBA: How to drag formulaS to the last used row?

Good day, please help me.
The scenario is like this, I have 7 different formulas assigned to 7 different top rows after the header. What I want to achieved is to drag this formulas down to the last used rows simultaneously. I successfully wrote a code on doing this but it is a static code, I want to do it in a dynamical way because every month the amount of data is different so my static code is not reliable.
Here is the code that I have wrote:
'format border
ActiveSheet.Range("BK1", "BQ22").Select
borderMeFn
Dim strFormulas_OR1_ASR_DATA_DETAILS(1 To 7) As Variant
strFormulas_OR1_ASR_DATA_DETAILS(1) = "=COUNTIF(LSR_WISOR_USERS_" & month & ".xlsx!$C:$C,J2)" 'WISOR_COUNT - counts the PON_VER in LSR_WISOR_USER file.
strFormulas_OR1_ASR_DATA_DETAILS(2) = "=IFERROR(VLOOKUP(J2,LSR_WISOR_USERS_" & month & ".xlsx!$C:$E,3,FALSE),"""")"
strFormulas_OR1_ASR_DATA_DETAILS(3) = "=IFERROR(VLOOKUP(H2,CPXLIST_" & month & ".xlsx!$A:$B,2,FALSE),"""")"
strFormulas_OR1_ASR_DATA_DETAILS(4) = "=IFERROR(VLOOKUP(H2, DDVRFY_" & month & ".xlsx!$A:$B,2,FALSE),"""")"
strFormulas_OR1_ASR_DATA_DETAILS(5) = "=IFERROR(VLOOKUP(H2,HTG_" & month & ".xlsx!$A:$D,4,FALSE),"""")"
strFormulas_OR1_ASR_DATA_DETAILS(6) = "=IFERROR(VLOOKUP(H2,RPON_" & month & ".xlsx!$A:$B,2,FALSE),"""")"
strFormulas_OR1_ASR_DATA_DETAILS(7) = "=IFERROR(VLOOKUP(H2,PROV_PLAN_" & month & ".xlsx!$A:$F,6,FALSE),"""")"
'apply formulas to designated cells
With ActiveWorkbook.Sheets("Sheet1")
.Range("BK2:BQ2").formula = strFormulas_OR1_ASR_DATA_DETAILS
End With
Worksheets("Sheet1").Range("BK2:BQ22").FillDown
'Range("BK2:BQ" & LastRow).FillDown
Thank you in advance.
Try the code below, explanations inside the code's comments:
Dim LastRow As Long, LastCell As Range
'apply formulas to designated cells
With ThisWorkbook.Worksheets("Sheet1")
.Range("BK2:BQ2").Formula = strFormulas_OR1_ASR_DATA_DETAILS
' use Find function to get last row
Set LastCell = .Cells.Find(What:="*", After:=.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
If Not LastCell Is Nothing Then
LastRow = LastCell.Row
Else
MsgBox "Error!", vbCritical
End If
.Range("BK2:BQ" & LastRow).FillDown
End With

What is wrong with my order of operations in VBA code?

I have this Excel spreadsheet that I am trying to create a working macro (in VBA) that when a cell containing a date (dates in order by a row in the B column), and this cell is in a specific color, and this cell is active, and the user clicks on a button, the macro searches for all the dates equal to the date in the active cell and to its color. Then in column H, the number value of the respective rows to the found dates are added up and stored into a variable called totalValue Then afterwards, the date, description, and the totalValue are copied over to another sheet and pasted in the next available predefined row.
I know that the color sort works for one color, I am using more than one color layout. The problem is when I run the macro, it seems to add all the number values in Column H within the date and it does not filter out the colors. But, when I take out block of code for "if color equals this, then do math" in lines 52 & 53 (ElseIf rFound.Style.Name = "Shipping" Then totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet" ) then the color value for the code above that in lines 49 & 50 works (ElseIf rFound.Style.Name = "Office" Then totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet"), but not the code above that lines 46 & 47 unless I take out the code in lines 49 & 50 as well, otherwise it would still add all the values in Column E.
What am I doing wrong? How can I fix it so that it can find the dates in a set color and be able to have several set colors available for use without this addition problem?
The code in question starts at 'BEGINNING OF HELP SEGMENT and ends at 'END OF HELP SEGMENT. The code above, between 'BEGINNING of Search function for HELP SEGMENT and 'ENG of Search function for HELP SEGMENT is the gathering of search parameters.
Here is my code:
Sub Copy_and_Move_Jul()
'
' Copy_and_Move From July Payable Ledger to Jul Summary Macro
'
'BEGINNING of Search function for HELP SEGMENT
'********************************************
'Declare Var
Const AllUsedCellsColumnB = False
Dim rFound As Range, SearchRange As Range
Dim cellValue As Variant, totalValue As Variant
' Get the H value of active row and set it to totalValue
cellValue = Range("H" & ActiveCell.Row)
totalValue = cellValue
' GET & SEARCH FOR COLOR AND DATE OF ACTIVE CELL, AND GET THE VALUES IN COLUMN H AND RETURN VALUE TO "totalValue"
' set search range
Set SearchRange = Range("B7:B56")
' If there is no search range, show Msg
If Intersect(SearchRange, ActiveCell) Is Nothing Then
SearchRange.Select
MsgBox "You must select a cell in the date column before continuing", vbInformation, "Action Cancelled"
Exit Sub
End If
' Get search criteria & set it to rFound
Set rFound = SearchRange.Find(What:=ActiveCell.Value, _
After:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
SearchFormat:=False)
'********************************************
ENG of Search function for HELP SEGMENT
' BEGINNING OF HELP SEGMENT
'********************************************************************************************************************
' If rFound is not Nothing, then do math. If rFound is Nothing, then findnext
If Not rFound Is Nothing Then
Do
If rFound.Style.Name = "Marketing" Then
totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet"
ElseIf rFound.Style.Name = "Inventory" Then
totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet"
ElseIf rFound.Style.Name = "Office" Then
totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet"
ElseIf rFound.Style.Name = "Shipping" Then
totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet"
End If
Set rFound = SearchRange.FindNext(rFound)
' Loop till all matching cells are found
Loop While Not rFound Is Nothing And rFound.Address <> ActiveCell.Address
End If ' End of the Color & Date search
'********************************************************************************************************************
' END OF HELP SEGMENT
'Select & copy Columns B - I of Row of Active Cell
Range("B" & ActiveCell.Row & ":G" & ActiveCell.Row).Select
Selection.Copy
'Go to "Summary" Sheet & Paste data in next available empty Row
Sheets("Summary").Select
Range("B56").End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
'Select Column D & delete unneeded Qty # and input a "y" for "Expsense"
Range("D" & ActiveCell.Row).Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "y"
'Set Value of Column H
Range("E" & ActiveCell.Row) = totalValue
'Goto Column C, Check Cell Style and input where supplies came from
Range("C" & ActiveCell.Row).Select
If Worksheets("Summary").Range("C" & ActiveCell.Row).Style.Name = "Marketing" Then
ActiveCell.FormulaR1C1 = "Marketing Supplies"
ElseIf Worksheets("Summary").Range("C" & ActiveCell.Row).Style.Name = "Inventory" Then
ActiveCell.FormulaR1C1 = "Inventory Supplies"
ElseIf Worksheets("Summary").Range("C" & ActiveCell.Row).Style.Name = "Office" Then
ActiveCell.FormulaR1C1 = "Office Supplies"
ElseIf Worksheets("Summary").Range("C" & ActiveCell.Row).Style.Name = "Shipping" Then
ActiveCell.FormulaR1C1 = "Shipping Supplies"
End If
End Sub
Here is a picture, before taking out the code in lines 52 & 53, I hope this helps with my explanation as to what is happening:
Here is a picture, after taking out the code in lines 52 & 53, this is what it's supposed to do:
Much appreciation in advance!
Start by checking if all the style names in the search range have the expected values:
Sub styleNames()
Dim cl As Range, SearchRange As Range
Set SearchRange = Range("B7:B56")
For Each cl In SearchRange
If cl.Value <> vbNullString Then _
Debug.Print " row: " & cl.Row & " style name: " & cl.Style.name
Next cl
End Sub
If they do, then you know for sure it's your code which is the problem. Try rewriting it in a simpler and less convoluted way by introducing conditional statements in the for each loop instead.

Excel VBA - Vlookup

I have two worksheets (sheet1 and sheet2). Both contain a column with header "ID" (columns are not always in the same position so need to be found).
Needed is a vlookup in a new column before the "ID" column.
This is what I got so far
sub vlookup ()
FIND COLUMNS WITH "ID"-HEADER
'Set variables for Column Sku
'note: cfind1 is for sheet 1 and cfind 2 is for sheet 2
Dim col As String, cfind1 As Range, cfind2 As Range
column = "ID"
Worksheets(1).Activate
Set cfind1 = Cells.Find(what:=column, lookat:=xlWhole)
Worksheets(2).Activate
Set cfind2 = Cells.Find(what:=column, lookat:=xlWhole)
'CREATE COLUMN WITH VLOOKUP
'activate worksheet 1
Worksheets(1).Activate
'add column before sku-column
cfind1.EntireColumn.Insert
'Select cell 1 down and 1 to left of sku-cell.
cfind1.Offset(1, -1).Select
'Add VlookUp formulas in active cell
ActiveCell.Formula = "=VLOOKUP(LookUpValue, TableArray,1,0)"
'(Lookup_Value should refer to one cell to the right
(= cfind1.Offset (1, 0)??)
'Table_Array should refer to the column in sheet(2) with header "id"
'Autofill Formula in entire column
'???
End Sub
Everything is working fine until the "vlookup-part"
I managed to put a formula in the correct cell, but I just can't get the formula to work.
How can I set lookup_value as "one cell to the right" in the same sheet
and "table_array" as the column with header "ID" in worksheet(2)?
And how can I finally autofill the vlookup formula throughout the whole column?
It would be great if anybody can help me out with the correct vlookup formula / variables and the autofilling.
You could also use something similar to below should you want to avoid using the worksheet
curr_stn = Application.WorksheetFunction.VLookup(curr_ref, Sheets("Word_Specifications").Range("N:O"), 2, False)
Valuse/variables will need to be changed of course. lookup_value,Array (range), Column number, Exact match.
Exact match needs false and similar match needs true
Try below full code
Sub t()
Dim col As String, cfind1 As Range, cfind2 As Range
Column = "ID"
Worksheets(1).Activate
Set cfind1 = Cells.Find(what:=Column, lookat:=xlWhole)
Worksheets(2).Activate
Set cfind2 = Cells.Find(what:=Column, lookat:=xlWhole)
'CREATE COLUMN WITH VLOOKUP
'activate worksheet 1
Worksheets(1).Activate
'add column before sku-column
cfind1.EntireColumn.Insert
'Select cell 1 down and 1 to left of sku-cell.
cfind1.Offset(1, -1).Select
'Add VlookUp formulas in active cell
LookUp_Value = cfind1.Offset(1, 0).Address(False, False)
Table_Array = Col_Letter(Worksheets(2).Cells.Find(what:=Column, lookat:=xlWhole).Column) & ":" & Col_Letter(Worksheets(2).Cells.Find(what:=Column, lookat:=xlWhole).Column)
ws_name = Worksheets(2).Name
Col_index_num = 1
Range_Lookup = False
ActiveCell.Formula = "=VLOOKUP(" & LookUp_Value & ", " & ws_name & "!" & Table_Array & ", " & Col_index_num & ", " & Range_Lookup & ")"
'Autofill Formula in entire column
lastrow = Range(cfind1.Address).End(xlDown).Row
Range(cfind1.Offset(1, -1).Address).AutoFill Destination:=Range(cfind1.Offset(1, -1).Address & ":" & Col_Letter(cfind1.Offset(1, -1).Column) & lastrow), Type:=xlFillDefault
End Sub
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
Haven't done this before but my approach would be to use the cell or range.formula property and build the string that you would write in the cell. for example:
myrange.formula = "=Vlookup("&Lookup_Value&","&Table_Array&","&Col_index_num&","&Range_Lookup&")"

Varying ranges in sum and criteria range for IF statments

Hey guys I need help with my novice skills at VBA code writing. I would like to modify the code below to accommodate the varying ranges of data rows for my sum range and criteria range in a SUMIF statment.
Sub sumifstate ()
Set critRange = Range("K2", Selection.End(xlUp).Offset(-1, 0)).Select
Set sumRange = Range("L2", Selection.End(xlUp).Offset(-1, 0)).Select
Set critRange2 = Range("M2", Selection.End(xlUp).Offset(-1, 0)).Select
Set sumRange2 = Range("N2", Selection.End(xlUp).Offset(-1, 0)).Select
Range("K41").Select
ActiveCell.FormulaR1C1 = _
"= (SUMIF("critRange",""*DF*"","sumRange")+SUMIF("critRange2,""*DF*"","sumRange2")"
End Sub ()
I hope I was specific enough if not let me know what other information you might need. Thank you!
There is no guarantee that all four of those columns have data in the same last row. However, each SUMIF requires that all ranges have the same size. Use the same formula for each of the pairs. You are also not evaluating hte formula within VBA, merely constructing a string together. You can use the ranges' addresses as strings for this.
Sub sumifstate()
Dim lr As Long, critRange As String, sumRange As String, critRange2 As String, sumRange2 As String
lr = Cells(Rows.Count, "L").End(xlUp).Row
critRange = Range("K2:K" & lr).Address
sumRange = Range("L2:L" & lr).Address
lr = Cells(Rows.Count, "N").End(xlUp).Row
critRange2 = Range("M2:M" & lr).Address
sumRange2 = Range("N2:N" & lr).Address
Range("K41").Formula = _
"=SUMIF(" & critRange & ", ""*DF*"", " & sumRange & ")+SUMIF(" & critRange2 & ", ""*DF*"", " & sumRange2 & ")"
End Sub
The formula produced will be dynamic but in this format.
=SUMIF($K$2:$K$10, "*DF*", $L$2:$L$10)+SUMIF($M$2:$M$10, "*DF*", $N$2:$N$10)
Hi,
So where you've got R[-39] or R[-3], you want to insert a variable for your actual calculated rows?
intStartRow = GetStartRow() ' i.e. however you define it, set a variable
intEndRow = GetEndRow()
Then replace where needed:
ActiveCell.FormulaR1C1 = _
"=(SUMIF(R[" & intStartRow & "]C:R[" & intEndRow & "]C, ...
i.e. use concatenate symbol "&",
Either that or use "=Offset" formula combined with Counta: that allows you to have "growing/shrinking" ranges without having to rerun your code

VBA - Set Range Between Two Dates Using Search Function

I'm trying to get my VBA code to search through a column for a user-inputted value (on a form) and set a range based on the values.
I need the code to scan DOWN through the column until it finds the value (which is a date) and then scan UP through the column to get the second part of the range. I need it to be like this because there might be multiple instances of the same date and they all need to be accounted for.
I've tried this:
StartRange = ws.Cells.Find(What:=StartDate, SearchOrder:=xlRows, _
SearchDirection:=xlNext, LookIn:=xlValues)
EndRange = ws.Cells.Find(What:=EndDate, SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues)
But it's not working the way I had expected and is erroring. (Edit: the WS has been defined, so I know that's not the issue). I don't even know if I'm going about this the right way
I'm feeling defeated :(
Any help would be appreciated, Thanks in advance!
Edit:
I've yet to try any of the suggestions as I am away from my project at the moment, but I feel I need to clarify a few things.
The dates will always be in chronological order, I have a script that organises them on sheet activation
I need to be able to error handle dates that do not appear in the database, I also need the script to be able to "skip over" dates that don't exist. Ie, 1st 1st 1st, 3rd, 3rd, 5th. If my start and end dates were the 1st and 5th, the entire example would be the range.
Thanks for your help so far guys though, I appreciate it!
EDIT2:
I've tried a few answers and have added this in to my code, but it is now failing on a Range_Global fail.
Dim startrange, endrange, searchrange As Range
LookUpColumn = 2
With ws.Columns(LookUpColumn)
Set startrange = .Find(What:=Me.R_Start.Value, _
After:=ws.Cells(.Rows.count, LookUpColumn), _
SearchOrder:=xlRows, _
SearchDirection:=xlNext, LookIn:=xlValues)
Set endrange = .Find(What:=Me.R_End.Value, _
After:=ws.Cells(5, LookUpColumn), _
SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues)
searchrange = Range(startrange, endrange)
MsgBox searchrange.Address
End With
Any suggestions?
Using Find is the right way to do this type of thing, you just need to get a few details right.
Use Set to assign range references. Eg Set StartRange = ... (and make sure to Dim StartRange as Range). Ditto EndRange and SearchRange
Specify a After cell. Note that by default this is the Top Left cell of the search range, and the search begins after this cell. If your StartDate happens to be in cell A1 (and another cell) then leaving as default will return the wrong result
Limit the search range to the column of interest.
Dim all your variables. Each variaqble needs its own As (and use Option Explicit)
End result
Dim startrange As Range, endrange As Range, searchrange As Range
Dim LookUpColumn as Long
LookUpColumn = 2
With ws.Columns(LookupColumn)
' Make sure lookup column data is type Date
Set searchrange = .SpecialCells(xlCellTypeConstants)
searchrange.Value = searchrange .Value
Set searchrange = Nothing
Set StartRange = .Find(What:=CDate(StartDate), _
After:=.Cells(.Rows.Count, LookupColumn), _
SearchOrder:=xlRows, _
SearchDirection:=xlNext, LookIn:=xlValues)
Set EndRange = .Find(What:=CDate(EndDate), _
After:=.Cells(1, LookupColumn), _
SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues)
End With
Set searchrange = Range(startrange, endrange)
MsgBox searchrange.Address
Let's start with this and see what needs to be fine tuned.
This code will look for a date (based on input) and find the position of that date in a column. Same with the "EndDate" and then creates a range on that column between the 2 positions.
Sub ARange()
Dim Sh As Worksheet: Set Sh = Sheets("Sheet1")
Dim i, j As Integer
LookupColumn = "A" 'Define the LookupColum / If you find using column index to be simpler then you need to switch the search from (range) to (cells)
StartDate_Value = Sh.Range("B2").Value 'Use whatever you need to define the input values
EndDate_Value = Sh.Range("C2").Value 'Use whatever you need to define the input values
For i = 1 To 30000
If Sh.Range(LookupColumn & i).Value = EndDate_Value Then EndDate_Row = i
Next i
For j = EndDate_Row To 1 Step -1
If Sh.Range(LookupColumn & j).Value = StartDate_Value Then StartDate_Row = j
Next j
Dim MyDateRange As Range: Set MyDateRange = Sh.Range(LookupColumn & StartDate_Row & ":" & LookupColumn & EndDate_Row)
MsgBox "MyDateRange = " & LookupColumn & StartDate_Row & ":" & LookupColumn & EndDate_Row
End Sub
Another approach should imply looking for the EndDate from bottom upwards (as in Excel's column values) and for the StartDate from top to bottom. like this:
For i = 30000 to 1 step -1
For j = 1 To 30000
And the 3rd (the charm):for the EndDate from top to bottom and for the StartDate from top to bottom. like this:
For i = 1 to 30000
For j = 1 To 30000
And the 4th (The One):
For i = 1 to 30000
For j = 30000 to 1 Step -1
On my home laptop the search on the 30.000 cells is instant (under 1s).
Give it a try and based on the feedback we can fine tune it.
On the Other hand, I might read your question as for looking To select not all values between the top / bottom position, but any cells with values of dates between the 2 input values neverminind the arrangement of the values within the list (column cells). i.e. If StartDate = 1.Jan.2013 and EndDate = 3.Jan.2013. The code should pick up 1,2 and 3 from the 30.000 list neverminind the position of these 3 dates (which in fact may be found thousands of times). If This is true, the solution may be simpler than the one above.
I don't like the concept of this date search for a couple of reasons..
It makes the assumption that the dates will always be in order
It makes the assumption that both the dates will exist in the list
Whilst these may be valid assumptions in this case, I'm sure there may be instances where this may not be the case...
I don't know the best way to do this but one alternative to consider is using the auto-filter
Something like:
Sub FindDateRange()
Dim sht As Worksheet
Dim column As Long
Set sht = Sheet1
Dim rng As Range, inclusiveRange As Range
Dim startDate As Long, endDate As Long
column = 2
On Error GoTo Err
startDate = DateValue("02/10/2012")
endDate = DateValue("05/10/2012")
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
sht.Cells(1, column).AutoFilter Field:=column, Criteria1:=">=" & startDate, Operator:=xlAnd _
, Criteria2:="<=" & endDate
Set rng = sht.Range(sht.Cells(2, column), sht.Cells(sht.Cells(sht.Rows.Count, column).End(xlUp).Row, column)).SpecialCells(xlCellTypeVisible)
sht.AutoFilterMode = False
If rng.Address = sht.Cells(1, column).Address Then
MsgBox Format(startDate, "dd-mmm-yyyy") & " - " & Format(endDate, "dd-mmm-yyyy") _
& vbCrLf & vbCrLf & "No instances of the date range exist"
Else
Set inclusiveRange = sht.Range(rng.Cells(1, 1), rng.Cells(rng.Count, 1))
MsgBox Format(startDate, "dd-mmm-yyyy") & " - " & Format(endDate, "dd-mmm-yyyy") _
& vbCrLf & vbCrLf & "the range is " & rng.Address & vbCrLf & vbCrLf & _
"inclusive range is " & inclusiveRange.Address
End If
continue:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
Err:
MsgBox Err.Description
GoTo continue
End Sub