Jambi46n2
Active Member
- Joined
- May 24, 2016
- Messages
- 260
- Office Version
- 365
- Platform
- 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:
Here is my code in full:
My locals window states cellValue = Empty.
The values under Courseno are like this:
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!
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 | |
|
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: