+ Reply to Thread
Results 1 to 1 of 1

Cannot get code to not be nothing after .Find

Hybrid View

  1. #1
    Registered User
    Join Date
    12-08-2020
    Location
    United States
    MS-Off Ver
    2011
    Posts
    6

    Cannot get code to not be nothing after .Find

    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.
    Last edited by hamsterpopcorn; 12-09-2020 at 08:11 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 02-26-2020, 07:59 PM
  2. VBA code to find the code in both Payer and receiver and do concontate
    By julielara in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-04-2016, 01:59 AM
  3. Replies: 6
    Last Post: 02-21-2016, 04:15 AM
  4. Find and Display results VBA Code - Loop to find all Matches
    By i2rule in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-16-2014, 03:14 PM
  5. [SOLVED] How to code the selection.find not to crash when the data to find is blank
    By evertjvr in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-25-2012, 03:04 PM
  6. trouble with vba code for Find, Insert copied cells and find next
    By jgelbach in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-26-2012, 01:45 PM
  7. code to find, copy and paste until find new, then repeat
    By siddharthariver in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-12-2010, 04:02 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1