VBA Help - Runtime Error 13 Mismatch

Jambi46n2

Active Member
Joined
May 24, 2016
Messages
260
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have code in VBA that is giving me problems and am hoping someone can help!

Here's what I'm trying to accomplish:
1. I have a sheet titled "Upload Result"
2 Search under the column titled "Courseno" for anything that contains a "#"
3. If a # is found, move the data into an "Exclusions_MM_DD_YY_HH.MM.SS" sheet.

I keep encountering my runtime error on this line:
VBA Code:
    cellValue = SourceSheet.Cells(CurrentRow, "Courseno").Value

Here is my code in full:
VBA Code:
Sub MoveExcludedRows()

    Dim SourceSheet As Worksheet
    Dim TargetSheet As Worksheet
    Dim ExclusionSheet As Worksheet
    Dim LastRow As Long
    Dim CurrentRow As Long
    Dim Timestamp As String
  
    ' Define the source, target, and exclusion sheets
    Set SourceSheet = ThisWorkbook.Worksheets("Upload Result")
    Timestamp = Format(Now, "MM_DD_YY_HH.MM.SS")
  
    ' Check if the Exclusion sheet already exists, if not, create it
    On Error Resume Next
    Set ExclusionSheet = ThisWorkbook.Worksheets("Exclusions_" & Timestamp)
    On Error GoTo 0
  
    If ExclusionSheet Is Nothing Then
        Set ExclusionSheet = ThisWorkbook.Sheets.Add
        ExclusionSheet.Name = "Exclusions_" & Timestamp
    End If
  
    ' Find the last row in the source sheet
    LastRow = SourceSheet.Cells(SourceSheet.Rows.Count, "A").End(xlUp).Row
  
    ' Copy the header row to the exclusion sheet
    SourceSheet.Rows(1).Copy ExclusionSheet.Rows(1)
  
' Loop through the data rows in the source sheet
For CurrentRow = LastRow To 2 Step -1 ' Loop from bottom to top to avoid issues with row deletion
    On Error Resume Next
    Dim cellValue As Variant
    cellValue = SourceSheet.Cells(CurrentRow, "Courseno").Value
  
    If Not IsError(cellValue) Then
        If CStr(cellValue) = "#" Then
            ' Copy the entire row to the exclusion sheet
            SourceSheet.Rows(CurrentRow).Copy ExclusionSheet.Rows(ExclusionSheet.Cells(ExclusionSheet.Rows.Count, "A").End(xlUp).Row + 1)
          
            ' Delete the row from the source sheet
            SourceSheet.Rows(CurrentRow).Delete
        End If
    End If
    On Error GoTo 0
Next CurrentRow

End Sub

My locals window states cellValue = Empty.

The values under Courseno are like this:
20-HSTR-F3-0037
20-HSTR-F3-0064
20-HSTR-F3-0069
# DATA NOT FOUND

The # DATA NOT FOUND is where I want the entire row moved into the Exclusions sheet.

I've also tried: If CStr(cellValue) = "# DATA NOT FOUND" Then
The code runs and says it completed without errors but nothing happens to the # DATA NOT FOUND portions.

Thanks in advance for any help you can provide.

Also open to new code entirely if it works!
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
VBA Code:
cellValue = SourceSheet.Cells(CurrentRow, "Courseno").Value

Where it says "Courseno" it should be either the Column letter (in quotation marks) or its Index number, not the contents of the first row.

Exactly the same as you are using in the line below
Rich (BB code):
LastRow = SourceSheet.Cells(SourceSheet.Rows.Count, "A").End(xlUp).Row
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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