EXCEL VBA: Class Module Variables value location depending on if function

六眼飞鱼酱① 提交于 2021-01-29 09:58:09

问题


I have created a dictionary to combine several bits of data for one event. For example, NameA can have four entries depending on the value in column 5. It can be A, B, C, D. However i would like to display all this information from A, B, C, D on one row in a table for NameA as the end product.

So i have created a dictionary with a class module to show this, with the class listing all possible variables to display in final output. Depending on the entries (A, B, C, D) the value in column 1 can be equal to different variables in the class.

SO the dictionary will add one NameA and the standard info which source doesn't change but then if NameA is exists already, it will add the existing info for A, B, C, D to said values, each from a different source location.

E.g. If entry A, the value is column 1 is equal to PQL, if entry B, the value is 1IW, if C, 2IW and D is PPL.

I have used an if function however when writing the data it will not write anything for the variables within the IF statements e.g. PQL, 1IW, 2IW and PPL, it just remains blanks for each NameA. As the varibales are not filled.

Whole module

Sub Dictionary()

    Dim dict As Dictionary

    Set dict = ReadData()

    Call WriteDict(dict)

End Sub


Function ReadData()

    Dim dict As New Dictionary
    Dim DataWs As Worksheet: Set DataWs = ThisWorkbook.Sheets("DATA")
    Dim PoolOfWeekWs As Worksheet: Set PoolOfWeekWs = ThisWorkbook.Sheets("Pool of the week")
    
    Dim LastrowData As Long: LastrowData = DataWs.range("A" & Rows.Count).End(xlUp).row
    Dim LastColData As Long: LastColData = DataWs.Cells(1 & DataWs.Columns.Count).End(xlToLeft).Column
    
    Dim LastColDataString As String: LastColDataString = Split(Cells(1, LastColData).Address, "$")(1)
    
    Dim DataRange As range: Set DataRange = DataWs.range("A1:" & LastColDataString & LastrowData)
    Dim DataArr As Variant: DataArr = DataWs.range("A1:AO" & LastrowData)
    
    Dim range As range: Set range = DataWs.range("A1").CurrentRegion
    
    Dim i As Long
    Dim CandidateProcessID As String, CandidateName As String, FirstName As String, ProcessStatus As String, FirstITWDate As String, PQLDate As String, XP As String, oCandidate As ClsCandidate
    
    For i = 2 To range.Rows.Count
        If range.Cells(i, 35).Value <> "NOK" Then
    
            CandidateProcessID = range.Cells(i, 10).Value
            FirstName = range.Cells(i, 17).Value
            ProcessStatus = range.Cells(i, 9).Value
            CandidateName = range.Cells(i, 16).Value
            Nationality = range.Cells(i, 39).Value
            XP = range.Cells(i, 24).Value
            SalaryExpectation = range.Cells(i, 48).Value
            Email = range.Cells(i, 18).Value
            PhoneNum = range.Cells(i, 19).Value
            ROName = range.Cells(i, 46).Value
            YearsExp = range.Cells(i, 24).Value
            Sector = range.Cells(i, 48).Value
            ProcessType = range.Cells(i, 35).Value
            InterviewScore = range.Cells(i, 37).Value
            DetailedSkills = range.Cells(i, 28).Value
            SkillsSummary = range.Cells(i, 29).Value
            NameofCM = range.Cells(i, 44).Value
            ROName = range.Cells(i, 46).Value
             
            If range.Cells(i, 13) = "Prequalification" Then PQLDate = range.Cells(i, 11).Value
    
            If range.Cells(i, 13) = "Candidate Interview 1" Then
                    FirstITWDate = range.Cells(i, 11).Value
                    BM1ITW = range.Cells(i, 44).Value
                    ProposedSalary = range.Cells(i, 48).Value
            End If
            If range.Cells(i, 13) = "Candidate Interview 2+" Then
                    SecondITWDate = range.Cells(i, 11).Value
                    ProposedSalary = range.Cells(i, 48).Value
            End If
            If range.Cells(i, 13) = "Candidate Interview 2*" Then
                    PPLDate = range.Cells(i, 11).Value
                    ProposedSalary = range.Cells(i, 48).Value
            End If
    
            If range.Cells(i, 13) = "Signature Interview" Then
                    SignatureInterview = range.Cells(i, 11).Value
            End If
        
        If dict.Exists(CandidateProcessID) = True Then
            Set oCandidate = dict(CandidateProcessID)
                oCandidate.YearsExp = oCandidate.YearsExp
                oCandidate.NameofCM = oCandidate.NameofCM
                oCandidate.ROName = oCandidate.ROName
                oCandidate.DetailedSkills = oCandidate.DetailedSkills
                oCandidate.Nationality = oCandidate.Nationality
                oCandidate.CandidateName = oCandidate.CandidateName
                oCandidate.FirstName = oCandidate.FirstName
                oCandidate.PQLDate = oCandidate.PQLDate
        Else
            Set oCandidate = New ClsCandidate
            dict.Add CandidateProcessID, oCandidate
                oCandidate.YearsExp = oCandidate.YearsExp + YearsExp
                oCandidate.NameofCM = oCandidate.NameofCM + NameofCM
                oCandidate.ROName = oCandidate.ROName + ROName
                oCandidate.DetailedSkills = oCandidate.DetailedSkills + DetailedSkills
                oCandidate.Nationality = oCandidate.Nationality + Nationality
                oCandidate.CandidateName = oCandidate.CandidateName + CandidateName
                oCandidate.FirstName = oCandidate.FirstName + FirstName
                oCandidate.PQLDate = oCandidate.PQLDate + oCandidate.PQLDate
        End If
        
        End If
    Next i
    
    Set ReadData = dict
End Function

Sub WriteDict(dict As Dictionary)

    Dim key As Variant, oCandidate As ClsCandidate, row As Long
    Set rangeoutput = Sheets("Sheet1").range("A2").CurrentRegion
    row = 2
    
    For Each key In dict
        Set oCandidate = dict(key)
        'Debug.Print key, oCandidate.CandidateName, oCandidate.PQLDate, oCandidate.YearsExp, oCandidate.NameofCM, oCandidate.ROName, oCandidate.DetailedSkills, oCandidate.Nationality
        rangeoutput.Cells(row, 1).Value = oCandidate.Nationality
        rangeoutput.Cells(row, 2).Value = oCandidate.DetailedSkills
        rangeoutput.Cells(row, 3).Value = oCandidate.ROName
        rangeoutput.Cells(row, 4).Value = oCandidate.NameofCM
        rangeoutput.Cells(row, 5).Value = oCandidate.YearsExp
        rangeoutput.Cells(row, 6).Value = oCandidate.PQLDate
        rangeoutput.Cells(row, 7).Value = oCandidate.CandidateName
        rangeoutput.Cells(row, 8).Value = oCandidate.FirstName
        row = row + 1
    Next key

End Sub

Class moudle

Option Explicit

Public CandidateProcessID As String
Public Status As String
Public PQLDate As Date
Public ProcessType As String
Public InterviewScore As String
Public CandidateName As String
Public FirstName As String
Public NameofCM As String
Public BM1ITW As String
Public FirstITWDate As Date
Public DetailedSkills As String
Public SkillsSummary As String
Public XP As Long
Public NP As String
Public Nationality As String
Public SalaryExpectation As Long
Public ProposedSalary As Long
Public SecondITWDate As Date
Public PPLDate As Date
Public Email As String
Public PhoneNum As Long
Public ROName As String
Public BusinessUnit As String
Public RecruiterTregram As String
Public Country As String
Public YearsExp As Long
Public Sector As String
Public SignatureInterview As Date

回答1:


Here:

If dict.Exists(CandidateProcessID) = True Then
   Set oCandidate = dict(CandidateProcessID)  '<<< existing instance
   oCandidate.YearsExp = oCandidate.YearsExp  
   oCandidate.NameofCM = oCandidate.NameofCM  

You just set the property values to their own values? Seems like this is where you should be adding the other values to the existing property values (needs a better process than just + though...), not in the Else block



来源:https://stackoverflow.com/questions/64125396/excel-vba-class-module-variables-value-location-depending-on-if-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!