Home > Enterprise >  Object variable or with variable not set for copying
Object variable or with variable not set for copying

Time:12-22

I would like to be able to copy my data to the workbook.

I have built the code from here:

enter image description here

In the "Locals" window I see, that variable is nothing. My situation is derivative from this query:

Defining just part of the string as a constant

Have I done something wrong in the meantime?

CodePudding user response:

Always best to test the result of a Find.

Sub Copy_Data(ByRef datSource As Worksheet, datTarget As Worksheet)
    
    'QUESTION 1
    Const TM_PM As String = "*PM is required*"
    
    Dim que1 As Range
    Set que1 = Sheets("Sheet1").Range("A1:A100").Find(What:=TM_PM, _
                            Lookat:=xlPart, LookIn:=xlValues)
                                         
    If que1 Is Nothing Then
        MsgBox "The question about PM or TM wasn't found", vbExclamation
    Else
        que1.Copy
        datTarget.Range("E1").PasteSpecial xlPasteValuesAndNumberFormats
    End If

End Sub
  • Related