How do I copy multiple ranges to the clipboard? - vba

I have searched for this answer and many answers are close, but none of them are exactly what I need.
I need to copy the contents of 3 ranges into the clipboard and I've tried different methods to no avail. Below is an example of my code and one method...
Sub COPY_UPDATES()
Dim CopyPart1 As Range, CopyPart2 As Range, CopyPart3 As Range, AllParts As Range
Set CopyPart1 = Range("$B$2:$B$14").SpecialCells(xlCellTypeConstants)
Set CopyPart2 = Range("$A$2:$A$10000").SpecialCells(xlCellTypeConstants)
Set CopyPart3 = Range("$B$17")
Set AllParts = Union(CopyPart1, CopyPart2, CopyPart3)
'Set AllParts = Range("CopyPart1.Address, CopyPart2.Address, CopyPart3.Address")
'Set AllParts = Range(CopyPart1, CopyPart2, CopyPart3)
'Debug.Print CopyPart1 'Run-time error '13': Type mismatch
'Debug.Print CopyPart2 'Run-time error '13': Type mismatch
Debug.Print CopyPart3 'Immediate prints the value I need
'Debug.Print AllParts 'Run-time error '13': Type mismatch
AllParts.Copy 'Run-time error '1004': This action won't work on multiple selections
MsgBox "SQL UPDATES have been copied to the Clipboard." & vbNewLine & _
"Paste into SMSS (New Query) and Execute (F5)"
End Sub
The only Debug.Print that does NOT set off an error is CopyPart3, the single cell range. Taking SpecialCells out of the other two doesn't seem to matter either. I seen other cases talk about setting up a loop, but is that something I need?
Again, I have searched and searched and just cannot find this particular solution.
I just want those ranges stacked up so I can paste them all at once. Thanks!

I got what I needed using #Variatus's suggestion from the comments in my original post, which is basically copy and pasting each range into another range first, then copy that new range of combined ranges as one. Below is my new code with factors in there to clean out empty cells and format.
Sub COPY_UPDATES()
Dim CopyPart1 As Range, CopyPart2 As Range, CopyPart3 As Range, AllParts As Range
Dim CP2_LRow As Long
CP2_LRow = Cells(Rows.Count, 1).End(xlUp).Row
Set CopyPart1 = Range("$B$2:$B$12")
Set CopyPart2 = Range("$A$2:$A$" & CP2_LRow)
Set CopyPart3 = Range("$B$14")
CopyPart1.Copy
Range("$B$20:$B$32").PasteSpecial xlPasteValues 'removes cell format on paste
CopyPart2.Copy
Range("$B$33:$B$" & CP2_LRow + 32).PasteSpecial xlPasteValues 'removes cell format on paste
CopyPart3.Copy
Range("$B$" & CP2_LRow + 32 + 1).PasteSpecial xlPasteValues 'removes cell format on paste
Set AllParts = Range("$B$20:$B$" & CP2_LRow + 32 + 1)
'below copies the 3 pasted ranges over one column to remove blanks from CopyParts pastes
AllParts.SpecialCells(xlCellTypeConstants).Copy Destination:=Range("$C$20")
'delete the original 3 CopyPart pastes to shift newly copy/pasted values with no blanks to AllParts range
AllParts.Delete Shift:=xlToLeft
Dim AP_LRow As Long, AllPartsFinal As Range
AP_LRow = Cells(Rows.Count, 2).End(xlUp).Row
Set AllPartsFinal = Range("$B$20:$B$" & AP_LRow)
'last copy to clipboard to be pasted
AllPartsFinal.Copy
MsgBox "SQL UPDATES have been copied to the Clipboard." & vbNewLine & _
"Paste into SMSS (New Query) and Execute (F5)"
End Sub

Related

Excel VBA 1004 Error with named range formula

I am tinkering with a program but I get an error that I cant place.
Error is 1004
Code:
Source_1_Criteria = "Factuur"
Source_1_Name = Range("MDM_MDM_Tool_List").Find(what:=Source_1_Criteria).Offset(0, 2).Value
Source_1_Area = Range("MDM_MDM_Tool_List").Find(what:=Source_1_Criteria).Offset(0, 4).Value
ActiveWorkbook.Names.Add Name:=Source_1_Name, RefersTo:=Source_1_Area
The last line gives me the error.
I know its something in the 3 line.
The result in the 3 line results in error with current contents in the cell
=VERSCHUIVING(archief!$A$2;0;0;1;AANTALARG(archief!$A$2:archief!$Y$2))
When I change the contents to =archief!$A$2 the code works.
But when I manually copy/paste the formula in a named range it works....
So to my calculation:
Formula = Works
Code = Works
those 2 together gives
error 1004
The formula you should have in place of =VERSCHUIVING(archief!$A$2;0;0;1;AANTALARG(archief!$A$2:archief!$Y$2)) should be this one:-
=ADRES(2;1;;;"Archief") & ":" & ADRES(2;AANTALARG(Archief!$A$2:$Y$2))
Below is code which I tested thoroughly. It works with the formula I supplied yesterday (above). You can use Edit/Replace to restore your original variable names in the code below.
Option Explicit
Private Sub Dutch()
' 05 Jan 2018
Dim Wb As Workbook
Dim Ws As Worksheet
Dim SrcCrit As String
Dim SrcName As Variant
Dim SrcArea As String
Dim Sp() As String
Dim Rng As Range
Set Wb = ActiveWorkbook ' could be ThisWorkbook
Set Ws = ActiveSheet ' could be another sheet
SrcCrit = "Factuur"
Set Rng = Ws.Range("ToolList").Find(What:=SrcCrit)
If Not Rng Is Nothing Then
SrcName = Rng.Offset(0, 2).Value
' Debug.Print SrcName
SrcArea = Rng.Offset(0, 4).Value
' Debug.Print SrcArea
Sp = Split(SrcArea, "!")
If UBound(Sp) Then
If Left(Sp(0), 1) = "'" Then Sp(0) = Mid(Sp(0), 2)
If Right(Sp(0), 1) = "'" Then Sp(0) = Left(Sp(0), Len(Sp(0)) - 1)
Else
Sp = Split(Ws.Name & "|" & Sp(0), "|")
End If
Set Rng = Wb.Worksheets(Sp(0)).Range(Sp(1))
' Debug.Print Rng.Worksheet.Name, Rng.Address
On Error Resume Next
Wb.Names(SrcName).Delete
On Error GoTo 0
Wb.Names.Add Name:=SrcName, RefersTo:=Rng
' With Wb.Names(SrcName)
' Debug.Print .Name, .RefersTo
' End With
End If
End Sub
I found that error 1004 is a little complicated in the above context because such an error will occur when a faulty Name exists in the workbook, such as may have been created by your original code. In that case the error indicates the existence of an unrecognised element in the workbook which may be totally unrelated to the code you are intending to test run. 1004 errors of this nature are commonly caused by compatibility issues, not even related to names. Google for _xlfn IERROR
I got a persistent 1004 error while developing the above code because I had named the offsets from the found range as "first" to "fourth" which allocated "second" to the new named range. Wb.Names("Second").Name correctly returned "second" but Wb.Names(ScrName).Name trigered error 1004 - just your complaint. The error went away when I found out that SECOND() is a worksheet function and changed the name in the offset cell.

How to append address of current cell to range object

I've been trying to write a function that goes through an Excel worksheet to find a range of cells fulfilling a certain condition (two cells in the same row that have to be equal).
I've written the following code that goes through the Worksheet row by row and checks if the condition is fulfilled.
If a cell is found for which the condition is true I would like the address of the cell to be added to a range.
The output of the function should finally be this range which is subsequently used to populate a dropdown menu in a dialog with the entries fulfilling the condition.
Private Function DetermineRange(WorksheetName As String, Column1 As Integer, Column2 As Integer) As Range
Dim rng As Range
'Go through rows of specified worksheet
For currRow = 1 To Worksheets(WorksheetName).Cells(Rows.Count, 3).End(xlUp).Row
'Compare cells in specified columns of current row
If Worksheets(WorksheetName).Cells(currRow, Column1).Value = Worksheets(WorksheetName).Cells(currRow, Column2).Value _
And Not (Worksheets(WorksheetName).Cells(currRow, Column1).Value = "") Then
'If cells are equal, but not empty, append current adress of current cell to range
If Not rng Is Nothing Then
Set rng = Union(rng, Worksheets(WorksheetName).Cells(currRow, 2))
Else
Set rng = Worksheets(WorksheetName).Cells(currRow, 2)
End If
End If
Next currRow
If Not rng Is Nothing Then
'return found Range
Set DetermineRange = rng
MsgBox ("Range is: " & rng)
Else
'DEBUG: Throw error message if rng is empty,
MsgBox ("DEBUG DetermineRange Function:" & vbCrLf & _
"Error! No corresponding Cells found in Sheet" & WorksheetName)
End If
End Function
Cycling through the rows works fine, however I don't seem to be able to add the addresses for the cells after the condition is checked to the range object.
I have also tried the following, which results in a
Runtime error 424: Object required
'If cells are equal, but not empty, append current address of current cell to range
If Not rng Is Nothing Then
Set rng = Union(rng, Worksheets(WorksheetName).Cells(currRow, 2).Address)
Else
Set rng = Worksheets(WorksheetName).Cells(currRow, 2).Address
End If
I've been looking around, but can't seem to find much information on how to add cells to range objects however...
Maybe one of you could help! Any kind of pointer in the right direction is highly appreciated!
Thanks in advance for any kind of help!
Edit:
I am calling the function like this:
Set NameRng = DetermineRange("Features", ProjectColumn, TCGroupColumn)
cb_FcnName.RowSource = Worksheets(3).Name & "!" & NameRng.Address
But I get the following error:
Runtime Error 380: Not able to set property RowSource
One method is to capture the cell addresses. Concatenate these and use the final value to build a new range.
Example:
Public Function DetermineRange(WorksheetName As String, Column1 As Integer, Column2 As Integer) As Range
Dim rng As Range
Dim currRow As Integer
Dim targetSheet As workSheet ' Shortcut to requested sheet.
Dim matchesFound As String ' Address of matching cells.
' This line will raise an error if the name is not valid.
Set targetSheet = ThisWorkbook.Sheets(WorksheetName)
'Go through rows of specified worksheet
For currRow = 1 To targetSheet.UsedRange.Rows(targetSheet.UsedRange.Rows.Count).Row
'Compare cells in specified columns of current row
If targetSheet.Cells(currRow, Column1).Value <> "" Then
If targetSheet.Cells(currRow, Column1).Value = targetSheet.Cells(currRow, Column2).Value Then
' Capture address of matching cells.
If Len(matchesFound) > 0 Then
matchesFound = matchesFound & "," & targetSheet.Cells(currRow, Column1).Address
Else
matchesFound = targetSheet.Cells(currRow, Column1).Address
End If
End If
End If
Next currRow
' DEBUG: Throw error message if no matches found.
If Len(matchesFound) = 0 Then
Err.Raise vbObjectError + 101, "DetermineRange", "No matching cells found."
End If
' Return range.
Set DetermineRange = targetSheet.Range(matchesFound)
End Function
The code is a little rough and ready. I can't help but feel there are few too many lines. But the basic approach works.

Excel VBA - For Each loop is not running through each cell

I am currently facing an issue in which my 'for each' loop is not moving onto subsequent cells for each cell in the range I have defined when I try to execute the script. The context around the data is below:
I have 3 columns of data. Column L contains employees, Column K contains managers, and column J contains VPs. Column K & J containing managers and VPs are not fully populated - therefore, I would like to use a VBA script & Index Match to populate all the cells and match employees to managers to VPs.
I have created a reference table in which I have populated all the employees to managers to directors and have named this table "Table 4". I am then using the VBA code below to try and run through each cell in column K to populate managers:
Sub FillVPs()
Dim FillRng As Range, FillRng1 As Range, cell As Range
Set FillRng = Range("J2:J2000")
Set FillRng1 = Range("K2:K2000")
For Each cell In FillRng1
If cell.Value = "" Then
ActiveCell.Formula = _
"=INDEX(Table4[[#All],[MGRS]], MATCH(L583,Table4[[#All],[EMPS]],0))"
End If
Next cell
End Sub
I feel that something is definitely wrong with the index match formula as the match cell "L583" is not moving to the next cell each time it runs through the loop; however, I am not sure how to fix it. I also do not know what else is potentially missing. The code currently executes, but it stays stuck on one cell.
Any help is greatly appreciated, and I will make sure to clarify if necessary. Thank you in advance.
The problem is that you are only setting the formula for the ActiveCell.
ActiveCell.Formula = _
"=INDEX(Table4[[#All],[MGRS]], MATCH(L583,Table4[[#All],[EMPS]],0))"
This should fix it
cell.Formula = _
"=INDEX(Table4[[#All],[MGRS]], MATCH(L583,Table4[[#All],[EMPS]],0))"
You'll probably need to adjust L583. It will not fill correctly unless you are filling across all cell.
These ranges should probably be changed so that they are dynamic.
Set FillRng = Range("J2:J2000")
Set FillRng1 = Range("K2:K2000")
You should apply the formula to all the cells in the range
Range("K2:K2000").Formula = "=INDEX(Table4[[#All],[MGRS]], MATCH(L2,Table4[[#All],[EMPS]],0))"
UPDATE: Dynamic Range
Every table in Excel should have at least one column that contain an entry for every record in the table. This column should be used to define the height of the Dynamic Range.
For instance if Column A always has entries and you want to create a Dynamic Range for Column K
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Set rng1 = Range("K2:K" & lastrow)
Or
Set rng1 = Range("A2:A" & Rows.Count).End(xlUp).Offset(0, 10)
UPDATE:
Use Range.SpecialCells(xlCellTypeBlanks) to target the blank cells. You'll have to add an Error handler because SpecialCells will throw an Error if no blank cells were found.
On Error Resume Next
Set rng1 = Range("A2:A" & Rows.Count).End(xlUp).Offset(0, 10).SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If rng1 Is Nothing Then
MsgBox "There were no Blank Cels Found", vbInformation, "Action Cancelled"
Exit Sub
End If
The "L583" was not changing because you were not telling it to. The code below should change the reference as the cell address changes.
Range.Address Property
Sub FillVPs()
Dim FillRng As Range, FillRng1 As Range, cell As Range
Set FillRng = Range("J2:J2000")
Set FillRng1 = Range("K2:K2000")
For Each cell In FillRng1
If cell.Value = "" Then
cell.Formula = _
"=INDEX(Table4[[#All],[MGRS]], MATCH(" & cell.Offset(0,1).Address() & ",Table4[[#All],[EMPS]],0))"
End If
Next cell
End Sub

VBA Named Range most efficient way to check if name exists

I have a routine, that fills a calendar with all important events for the commodity markets for each day of the following week. I have a calendar grid laid out on the page and have ten named cells for each day i.e. Monday1, Monday2 and so on (each day only goes up to 10 for now, i.e.Monday10), in each days column. BTW the cells are 2 cells wide and 2 cells deep. Many times there are more than 10 events for a given day. I am trying to test for the named range to see if it exists, if not copy the format of the last named range cell and name that cell the next name in the series.
I am only having two issues with the above, first and foremost is how to test to determine in a name for a named range already exists. I am currently iterating thru the entire list of ThisWorkbook.Names, which has thousands of named ranges in it. Since this iteration could be running over 100 times when the calendar is generating, it is wicked slow (as would be expected). Is there a better, faster way to check if a name already exists as a named range?
The second issue is how to copy the formatting of a 4 cell, merged cell, since the address always comes up as only the top left corner cell so offsetting the range doesn't work appropriately. I hacked around to get this code to at least come up with the right range for the next merged cell group in the column
Set cCell = Range("Thursday" & CStr(y))
'even tho cCell is a 4 cell merged cell, cCell.Address returns the address of top left cell
Set destRange = Range(cCell.Address & ":" & cCell.offset(2, 0).offset(0, 1).Address)
Recording a macro to drag the formatting down, shows this code.
Range("G22:H23").Select
Selection.AutoFill Destination:=Range("G22:H25"), Type:=xlFillFormats
Range("G22:H25").Select
Since Range("G22:H23") is the same as cCell, and Range("G22:H25") is the same as destRange. The following code should work, but doesn't.
Set cCell = Range("Thursday" & CStr(y))
Set destRange = Range(cCell.Address & ":" & cCell.offset(2, 0).offset(0, 1).Address)
cCell.AutoFill Destination:=destRange, Type:=xlFillFormats
Application.CutCopyMode = False
cCell.offset(1, 0).Name = rangeName
FYI, it doesn't work if I select cCell and use Selection.AutoFill either.
Any thoughts on how to copy that cell formatting down the column one cell at a time when needed?
Update:
This now works for copying the formatting down from one merged cell to another of same size. For some reason setting destRange to the whole range (the copy cell and pastecell entire range as the macro recorder showed) didnt work but setting destRange to the cell range that needed formatting, and then doing a union of cCell and destRange worked, and made naming the new range easier.
rangeName = "Friday" & CStr(y + 1)
priorRangeName = "Friday" & CStr(y)
namedRangeExist = CheckForNamedRange(rangeName)
If namedRangeExist = False Then
Set cCell = Range(priorRangeName)
Set destRange = Range(cCell.offset(1, 0).Address & ":" & cCell.offset(2, 0).offset(0, 1).Address)
cCell.AutoFill Destination:=Union(cCell, destRange), Type:=xlFillFormats
Application.CutCopyMode = False
destRange.Name = rangeName
End If
Update #2
There is an issue with naming ranges in a For loop ( the code below is running inside a For loop). The first time the new rangeName is not found, Setting cCell to the prior range name and running through the code to copy the merged cell format and name the new range works fine. Here is the code
rangeName = "Thursday" & CStr(y + 1)
priorRangeName = "Thursday" & CStr(y)
namedRangeExist = DoesNamedRangeExist(rangeName)
If namedRangeExist = False Then
Set cCell = Range(priorRangeName)
Debug.Print "cCell:" & cCell.Address
Set cCell = cCell.MergeArea
Debug.Print "Merged cCell:" & cCell.Address
Set destRange = Range(cCell.offset(1, 0).Address & ":" & cCell.offset(2, 0).offset(0, 1).Address)
Debug.Print "Dest:" & destRange.Address
Debug.Print "Unioned:" & Union(cCell, destRange).Address
cCell.AutoFill Destination:=Union(cCell, destRange), Type:=xlFillFormats
Application.CutCopyMode = False
destRange.name = rangename
End If
results in the following ranges
cCell:$G$22
Merged cCell:$G$22:$H$23
Dest:$G$24:$H$25
Unioned:$G$22:$H$25
but if more than one new named range needs to be created the second time thru this code produces a range area as evidenced by the output shown below
cCell:$G$24:$H$25
so why does cCell's address show as only the upper left cells address when run the first time, but the second time thru cCell's address is shown as the whole merged cell range? And because it does, the next code line produces a range object error
Set cCell = cCell.MergeArea
Eliminating that code line and amending the first Set cCell to this;
Set cCell = Range(priorRangeName).MergeArea
produces the same error. I could kludge this by setting a counter, and if more than one, bypass that code line but that is not the preferred solution.
First and foremost, create a function to call the named range. If calling the named range generate an error the function will return False otherwise it will return True.
Function NameExist(StringName As String) As Boolean
Dim errTest As String
On Error Resume Next
errTest = ThisWorkbook.Names(StringName).Value
NameExist = CBool(Err.Number = 0)
On Error GoTo 0
End Function
As for your second question, I had not problem with the autofill.
I would replce Set destRange = Range(cCell.Address & ":" & cCell.offset(2, 0).offset(0, 1).Address) with Set destRange = cCell.Resize(2,1). It has the same effect but the later is much cleaner.
Application.Evaluate and Worksheet.Evaluate can be used to get error value instead of error :
If Not IsError(Evaluate("Monday1")) Then ' if name Monday1 exists
The error can be ignored or jumped over (but that can result in hard to detect errors) :
On Error GoTo label1
' code that can result in error here
label1:
If Err.Number <> 0 Then Debug.Print Err.Description ' optional if you want to check the error
On Error GoTo 0 ' to reset the error handling
Range.MergeArea can be used to get the Range of merged cell.
I created a function to extend the name ranges and fill in the formatting. The first named range in the series will have to be setup. The Name itself needs to be set to the top left cell in the merged area.
ExtendFillNamedRanges will calculate the positions of the named ranges. If a cell in one of the positions isn't part of a MergedArea it will fill the formatting down from the last named range. It will name that cell. The scope of the names is Workbook.
Sub ExtendFillNamedRanges(BaseName As String, MaxCount As Integer)
Dim x As Integer, RowCount As Integer, ColumnCount As Integer
Dim LastNamedRange As Range, NamedRange As Range
Set NamedRange = Range(BaseName & 1)
RowCount = NamedRange.MergeArea.Rows.Count
ColumnCount = NamedRange.MergeArea.Columns.Count
For x = 2 To MaxCount
Set NamedRange = NamedRange.Offset(RowCount - 1)
If Not NamedRange.MergeCells Then
Set LastNamedRange = Range(BaseName & x - 1).MergeArea
LastNamedRange.AutoFill Destination:=LastNamedRange.Resize(RowCount * 2, ColumnCount), Type:=xlFillDefault
NamedRange.Name = BaseName & x
End If
'NamedRange.Value = NamedRange.Name.Name
Next
End Sub
Here is the test that I ran.
Sub Test()
Application.ScreenUpdating = False
Dim i As Integer, DayName As String
For i = 1 To 7
DayName = WeekDayName(i)
Range(DayName & 1).Value = DayName & 1
ExtendFillNamedRanges DayName, 10
Next i
Application.ScreenUpdating = True
End Sub
Before:
After:
I found this on ozgrid and made a little function out of it:
Option Explicit
Function DoesNamedRangeExist(VarS_Name As String) As Boolean
Dim NameRng As Name
For Each NameRng In ActiveWorkbook.Names
If NameRng.Name = VarS_Name Then
DoesNamedRangeExist = True
Exit Function
End If
Next NameRng
DoesNamedRangeExist = False
End Function
You can put this line in your code to check:
DoesNamedRangeExist("Monday1")
It will return a Boolean value (True / False) so it's easy to use with an IF() statement
As to your question on merged cells, I did a quick macro record on a 2*2 merged cell and it gave me this (made smaller and added comments):
Sub Macro1()
Range("D2:E3").Copy 'Orignal Merged Cell
Range("G2").PasteSpecial xlPasteAll 'Top left of destination
End Sub

VBA- global object failed even though range exists

I'm trying to write up a script which will do the following things in the following order:
Locate a range that is located between 2 cells containing specific strings (DateRng)
Search within this range for a cell (i) that has a value <=Value
Compare two other cells which are offset to "i"
Export a range of rows centered around "i" to different sheets pending the outcome of
the above condition.
I can get steps 2 & 4 to work (if step 2 searched the entire sheet instead of DateRng.
I can also get step 1 to work
However, when I try to put it all together I get an error msg: Method 'Range' of object'_Global' failed.
This makes no sense to me as DateRng is being found.
If someone could advise on this that would be great. I have just started learning VBA so i am wide open to suggestions/corrections for the rest of the script too.
Below I have pasted the offending script with the line the Debugger stops on marked
Sub ReportCells()
Dim LR As Long, i As Long
Dim j, k As Long
Dim StartDate, FinishDate As String
Dim Sh As Worksheet: Set Sh = Sheets("Full chart and primary cals")
Dim CellFound As Range
'Range Extraction Script
'Search location and values
LookupColumn = "B"
StartDate = "2013.01.02 20:00"
FinishDate = "2013.01.09 20:00"
'Find Lower Limit
For j = 1 To 30000
If Sh.Range(LookupColumn & j).Value = FinishDate Then FinishDateRow = j
Next j
'Find Upper Limit
For k = FinishDateRow To 1 Step -1
If Sh.Range(LookupColumn & k).Value = StartDate Then StartDateRow = k - 1
Next k
'Set Range once located
Dim DateRng As Range: Set DateRng = Sh.Range(LookupColumn & StartDateRow & ":" & LookupColumn & FinishDateRow)
MsgBox DateRng.Address
'Find Cell
**With Range("DateRng")**
For i = 1 To LR
Set CellFound = .Find(Sh.Range("M:M").Value <= 25)
If Not CellFound Is Nothing And .Range("CellFound").Offset(0, -4).Value < .Range("CellFound").Offset(-10, -4).Value Then .Offset(-3, 0).Resize(10, 1).EntireRow.Copy Destination:=Sheets("Sheet18").Range("A" & Rows.Count).End(xlUp).Offset(2)
If Not CellFound Is Nothing And .Range("CellFound").Offset(0, -4).Value > .Range("CellFound").Offset(-10, -4).Value Then .Offset(-3, 0).Resize(10, 1).EntireRow.Copy Destination:=Sheets("Sheet19").Range("A" & Rows.Count).End(xlUp).Offset(2)
Next i
End With
End Sub
Edit: So the suggested fix corrected the error, however now I am experiencing what i guess is an error in the logic. The script is not returning any cells to sheet18 or sheet19.
I know there should be cells returned as these cells are able to be found if looked for manually. there are no error messages and the script appears to run normally. I have stripped back the code under the previously offending script and cannot seem to locate where the error in the logic is. Maybe it has something to do with this line(?):
Set CellFound = .Find(Sh.Range("M:M").Value <= 25)
More than likely you have used Range("DateRng") -- a named range -- instead of DateRng which is really the range you have defined on the line just above, by mistake.
I suggest trying the following:
With DateRng
'...
End With
Edit:
The code following that line tells me this is definitely a mistake you have made. When you define a range object you have to use it as is. You cannot call it by passing a string with the variable name to a call to Range() (see "CellFound" for the other mistake).