I would like to be able to copy my data to the workbook.
I have built the code from here:
Copying a range from all files within a folder and pasting into master workbook
which currently looks like this:
Sub CopyData()
Dim wbSource As Workbook
Dim datTarget As Worksheet
Dim datSource As Worksheet
Dim strFilePath, strfile As String
Dim strPath As String
Set datTarget = ThisWorkbook.Sheets("Survey")
strPath = GetPath
If Not strPath = vbNullString Then
strfile = Dir$(strPath & "*.xlsx", vbNormal)
Do While Not strfile = vbNullString
Set wbSource = Workbooks.Open(strPath & strfile)
Set datSource = wbSource.Sheets("Sheet1")
Call Copy_Data(datSource, datTarget)
wbSource.Close False
strfile = Dir$()
Loop
End If
End Sub
Sub Copy_Data(ByRef datSource As Worksheet, datTarget As Worksheet)
'QUESTION 1
Const TM_PM As String = "*PM is required*"
Dim que1 As Range
Dim ans1 As Range
Set que1 = Sheets("Sheet1").Range("A1:A100").Find(What:=TM_PM, _
Lookat:=xlPart, LookIn:=xlValues)
If Not que1 Is Nothing Then
'MsgBox ("The question about PM or TM wasn't found")
End If
que1.Copy
datTarget.Range("E1").PasteSpecial xlPasteValuesAndNumberFormats
End With
Dim lrow1 As Long
lrow1 = datTarget.Range("E" & datTarget.Rows.Count).End(xlUp).Row + 1
End Sub
I don't know why am I receiving errors:
Object variable or with variable not set
at the line:
que1.Copy
I tried also something like this:
With que1
.Copy
datTarget.Range("E1").PasteSpecial xlPasteValuesAndNumberFormats
End With
but the error was the same.
Excel VBA Error: Object variable or With block variable not set
I know that the question is common, but I need to know what is wrong in this issue?
UPDATE:
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?