I am trying to code a macro that will take an ID number that is somewhere in the file name and then find the matching subject number in Column A of a master sheet, where it will then offset and add to that row. There are two files, one called InterviewData10**.xlsx and SCIDdata10**.xslx with the asteriks representing two numbers. I had success with InterviewData using this code:
InterviewFile = Dir(FileDir & "InterviewData*xls*")
Do Until InterviewFile = ""
DoEvents
Set Interview = Workbooks.Open(FileDir & InterviewFile)
Dim InterviewFilepath As String
InterviewFilepath = Interview.Name
Dim SubID As Variant
Set SubID = Master.Sheets("Data").Range("A:A").Find(what:=Mid(InterviewFilepath, InStrRev(InterviewFilepath, "\") + 14, InStrRev(InterviewFilepath, ".") - InStrRev(InterviewFilepath, "\") - 14), LookIn:=xlValues, LookAt:=xlWhole)
If Not IsError(SubID) Then
Interview.Sheets("HAM-D").Range("B23").Copy
SubID.Offset(0, 28).PasteSpecial xlPasteValues
Interview.Sheets("PANSS").Range("C9").Copy
SubID.Offset(0, 29).PasteSpecial xlPasteValues
Interview.Sheets("PANSS").Range("C17").Copy
SubID.Offset(0, 30).PasteSpecial xlPasteValues
Interview.Sheets("PANSS").Range("C34").Copy
SubID.Offset(0, 31).PasteSpecial xlPasteValues
Interview.Sheets("YMRS").Range("C13").Copy
SubID.Offset(0, 32).PasteSpecial xlPasteValues
Else
Master.Sheets("Data").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = SubID
Interview.Sheets("HAM-D").Range("B23").Copy
SubID.Offset(0, 28).PasteSpecial xlPasteValues
Interview.Sheets("PANSS").Range("C9").Copy
SubID.Offset(0, 29).PasteSpecial xlPasteValues
Interview.Sheets("PANSS").Range("C17").Copy
SubID.Offset(0, 30).PasteSpecial xlPasteValues
Interview.Sheets("PANSS").Range("C34").Copy
SubID.Offset(0, 31).PasteSpecial xlPasteValues
Interview.Sheets("YMRS").Range("C13").Copy
SubID.Offset(0, 32).PasteSpecial xlPasteValues
End If
InterviewFile = Dir
Loop
I tried this with the SCIDdata, however it is a lot more conditional than just copying and pasting. It has 1's that when in certain cells need to have a certain word put into the master file. This is the code I am trying to use:
SCIDfile = Dir(FileDir & "SCIDdata*xls*")
Do Until SCIDfile = ""
DoEvents
Set SCID = Workbooks.Open(FileDir & SCIDfile)
Dim SCIDfilePath As String
SCIDfilePath = SCID.Name
Dim SubjectID As Variant
Set SubjectID = Master.Sheets("Data").Range("A:A").Find(what:=Mid(SCIDfilePath, InStrRev(SCIDfilePath, "\") + 9, InStrRev(SCIDfilePath, ".") - InStrRev(SCIDfilePath, "\") - 8), LookIn:=xlValues, LookAt:=xlWhole)
'Pulls Subject ID from file name and matches it to the existing one in master
If Not SubjectID Is Nothing Then
If SCID.Sheets("ALG. III").Range("L6") = 1 Then
SubjectID.Offset(0, 1).Value = "BPI"
ElseIf SCID.Sheets("ALG. III").Range("L7") = 1 Then
SubjectID.Offset(0, 1).Value = "SM"
ElseIf SCID.Sheets("ALG. III").Range("L9") = 1 Then
SubjectID.Offset(0, 1).Value = "BP2"
ElseIf SCID.Sheets("ALG. III").Range("L10") = 1 Then
SubjectID.Offset(0, 1).Value = "Other BP"
Else
MsgBox ("Error: Please check SCID file before continuing")
Exit Sub
End If
Else
MsgBox ("No subject ID was found for this subject.")
End If
SCIDfile = Dir
Loop
When running the code is stopped when I do SubjectID.Offset.
In my efforts to debug, I have found the following:
The file is certainly opening, and data can be copied and pasted.
The mid function is grabbing the correct subject ID number from the name of the file.
It is not "nothing" because if I do simple functions (that do not involve SubjectID) after the first if statement, there is no error and the code will run properly.
When doing debug.Print, for some reason there is nothing in the immediate window. It does not recognize the SubjectID as a cell value, whereas when I do debug.Print on the SubID from InterviewData, it comes up with the ID number for the chosen file. Because of this, it is telling me that I am missing an object (error 91).
I am aware of the problem, but I have no idea how to fix it.
Bookmarks