+ Reply to Thread
Results 1 to 12 of 12

VBA script to extract data based on multiple columns

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-13-2013
    Location
    Saudi Arabia
    MS-Off Ver
    2010, 2013
    Posts
    192

    VBA script to extract data based on multiple columns

    Hello Again,

    Below is my script it is working find on single column but i need to have combination of multiple columns but i failed.

    Can someone enhance the below script. Copy of workbook is attached for your reference.

    Problem Here
    myval = data(i, 15)
            If Not myval Like "J03*" Then
            'If Not myval Like "J03*" Then
        Else
            If errstr = "" Then errstr = "Error 2" Else errstr = errstr & ", " & "Error 2"
            result(j, 15) = temp
        End If
    Original code:-
    
    Option Compare Text
    
    Sub ProblemLog()
    
    With Application
            .DisplayAlerts = False
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
    Dim data, result, rng2color As Range, rcount As Long, colcount As Long, j As Long, i As Long, myval, errstr As String
    Const temp = "#%$#"
    
    If ActiveSheet.Name = "Claims Data" Or ActiveSheet.Name = "Template" Then
    
    data = Range("A1", Cells(Rows.Count, 1).End(xlUp)).Resize(, 46)
    
    rcount = UBound(data)
    colcount = UBound(data, 2)
    
    ReDim result(1 To rcount, 1 To colcount + 1)
    
    result(1, 1) = "Provider"
    result(1, 2) = "Box Number"
    result(1, 3) = "Claim Number"
    result(1, 4) = "Treatment Date"
    result(1, 5) = "Discharge Date"
    result(1, 6) = "Benefit Type"
    result(1, 7) = "Member PIN"
    result(1, 8) = "Member Name"
    result(1, 9) = "Member Class"
    result(1, 10) = "Policy  Number"
    result(1, 11) = "Policy Holder Name"
    result(1, 12) = "Policy Benefit"
    result(1, 13) = "Network Type"
    result(1, 14) = "Policy Type"
    result(1, 15) = "Diagnosis"
    result(1, 16) = "Gross"
    result(1, 17) = "Discount"
    result(1, 18) = "Deductible"
    result(1, 19) = "Rejected Amount"
    result(1, 20) = "Approved Amount"
    result(1, 21) = "Total difference"
    result(1, 22) = "Created By "
    result(1, 23) = "Created Date"
    result(1, 24) = "Created Time"
    result(1, 25) = "Pre-Auth Limit"
    result(1, 26) = "Approval Number"
    result(1, 27) = "Approval Date"
    result(1, 28) = "Auditor"
    result(1, 29) = "Difference Reason"
    result(1, 30) = "Description"
    result(1, 31) = "Claim Caution"
    result(1, 32) = "Marital Status"
    result(1, 33) = "Gender"
    result(1, 34) = "Insured Type"
    result(1, 35) = "Age"
    result(1, 36) = "Chronic"
    result(1, 37) = "Pre Existing"
    result(1, 38) = "Fraud"
    result(1, 39) = "Emergency"
    result(1, 40) = "Follow Up"
    result(1, 41) = "File Number"
    result(1, 42) = "Notes"
    result(1, 43) = "Claim Status"
    result(1, 44) = "Recovery Amount"
    result(1, 45) = "Approved VAT Amount"
    result(1, 46) = "Rejected VAT Amount"
    result(1, 47) = "Remarks"
    
    
    j = 1
    
    For i = 2 To rcount
        j = j + 1
    
        myval = data(i, 15)
        If Not myval Like "Z00*" Then
        Else
            errstr = "Error 1"
            result(j, 15) = temp
        End If
        
        myval = data(i, 15)
            If Not myval Like "J03*" Then
            'If Not myval Like "J03*" Then
        Else
            If errstr = "" Then errstr = "Error 2" Else errstr = errstr & ", " & "Error 2"
            result(j, 15) = temp
        End If
    
        If errstr <> "" Then
            For n = 1 To colcount
                result(j, n) = result(j, n) & data(i, n)
            Next
            result(j, 47) = errstr
            errstr = ""
        Else: j = j - 1
        End If
    Next
    Application.ScreenUpdating = 0
    Application.ReplaceFormat.Interior.ColorIndex = 6
    
    '*******************************************************************************************************************************************************************
    
    Sheets.Add(Before:=Sheets("Claims Data")).Name = "ErrorLog"
    With Worksheets("ErrorLog")
        .Range("a1").Resize(j, colcount + 1) = result
        With .Range("a1").Resize(, colcount + 1)
            .Interior.ColorIndex = 55
            .Font.ColorIndex = 2
            .Font.Bold = 1
            .HorizontalAlignment = xlCenter
        End With
        With .UsedRange
            .Font.Name = "Calibri"
            .Font.Size = 9
            .Borders.LineStyle = xlContinuous
            .Offset(, 1).Resize(, 47).Replace what:=temp, replacement:="", lookat:=xlPart, ReplaceFormat:=True
            .Offset(, 1).Resize(, 47).Replace what:=temp, replacement:="", lookat:=xlPart
        End With
    End With
    
    Else
    Response = MsgBox("This option will not for sheet: " & UCase(ActiveSheet.Name) & vbCrLf + "Please select/active below worksheets to use this option." _
    + vbCrLf + " " + vbCrLf + "1. DATA Worksheet" + vbCrLf + "2. TEMPLATE Worksheet" + vbCrLf + " " + vbCrLf + "                                      Abdul Aleem - Lets Make Life Easier...", vbOKOnly + vbInformation, "Underwriting Department")
    
    End If
    
    '*******************************************************************************************************************************************************************
    
    With Application
            .DisplayAlerts = True
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: VBA script to extract data based on multiple columns

    Hi Abdur_rahman,

    What your code basically does is copy any Diagnosis (column #15) that starts with J03 to a new sheet. What do you want it to do ? Which other column should the code look against or consider ?

  3. #3
    Forum Contributor
    Join Date
    09-13-2013
    Location
    Saudi Arabia
    MS-Off Ver
    2010, 2013
    Posts
    192

    Re: VBA script to extract data based on multiple columns

    Thank you Nankw for your response.

    I need to check if the J03 diagnosis in column O and if the Age in column AI is less then 6 then extract the data.

    I just need the idea only how should i combine it.

    I have tries like If Not myval Like "J03*" and data(i, 35) < 6 Then but doesn't work.

  4. #4
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: VBA script to extract data based on multiple columns

    If you need to extract data that start with J03 & age < 6 get rid of the Not

    Try the below
    If myval Like "J03*" And Data(i, 35) < 6 Then

  5. #5
    Forum Contributor
    Join Date
    09-13-2013
    Location
    Saudi Arabia
    MS-Off Ver
    2010, 2013
    Posts
    192

    Re: VBA script to extract data based on multiple columns

    No it doesn't work it is extracting other diagnosis also. Ex: J40, J06, etc..

  6. #6
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: VBA script to extract data based on multiple columns

    Ok, how about this ?

                  If Not myVal Like "J03*" Then 'And Data(i, 35) < 6 Then
                  'If Not myval Like "J03*" Then
              Else
                If Data(i, 35) < 6 Then
                   If errStr = "" Then errStr = "Error 2" Else errStr = errStr & ", " & "Error 2"
                   result(j, 15) = temp
                End If
              End If

  7. #7
    Forum Contributor
    Join Date
    09-13-2013
    Location
    Saudi Arabia
    MS-Off Ver
    2010, 2013
    Posts
    192

    Re: VBA script to extract data based on multiple columns

    Thanks nankwa it works.

    If i need to valid 3 columns then again i have add another if condition.

    Thanks.

  8. #8
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: VBA script to extract data based on multiple columns

    Honestly your code is a bit confusing ... Why do you use Not then nothing Else 'your code' ??

    Instead of this
    If Not myVal Like "Z00*" Then
    'nothing here
    Else
        errStr = "Error 1"
        result(j, 15) = temp
    End If
    
    myVal = Data(i, 15)
    If Not myVal Like "J03*" Then
    'nothing here
    Else
       If errStr = "" Then errStr = "Error 2" Else errStr = errStr & ", " & "Error 2"
       result(j, 15) = temp
    End If
    Why not just write this ?
    If myVal Like "Z00*" Then
        errStr = "Error 1"
        result(j, 15) = temp
    End If
    
    myVal = Data(i, 15)
    If myVal Like "J03*" And Data(i, 35) < 6 Then
       If errStr = "" Then errStr = "Error 2" Else errStr = errStr & ", " & "Error 2"
       result(j, 15) = temp
    End If

  9. #9
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: VBA script to extract data based on multiple columns

    Why so complicated? errStr can only get 1 value per row in this setup. Like "J03 *" is the same as left (value, 3).

    Sub VenA()
      With Sheets("Claims Data").Cells(1).CurrentRegion
        ar = .Resize(, .Columns.Count + 1)
        ar(1, 47) = "Remarks"
      End With
      
      For j = 2 To UBound(ar)
        If LCase(Left(ar(j, 15), 3)) = "j03" And ar(j, 35) < 6 Then ar(j, 47) = "Error 2"
        If LCase(Left(ar(j, 15), 3)) = "z00" Then ar(j, 47) = "Error 1"
      Next j
      
      If IsError(Evaluate("'Errorlog'!A1")) Then Sheets.Add(Sheets("Claims Data")).Name = "Errorlog"
        With Sheets("Errorlog")
          .UsedRange.Clear
        .Cells(1).Resize(UBound(ar), 47) = ar
        With .Cells(1).CurrentRegion
          .AutoFilter 47, ""
          .Offset(1).EntireRow.Delete
          .AutoFilter
          With .Rows(1)
            .Interior.ColorIndex = 55
            .Font.ColorIndex = 2
            .Font.Bold = 1
            .HorizontalAlignment = xlCenter
          End With
          .Font.Name = "Calibri"
          .Font.Size = 9
          .Borders.LineStyle = xlContinuous
        End With
      End With
    End Sub

  10. #10
    Forum Contributor
    Join Date
    09-13-2013
    Location
    Saudi Arabia
    MS-Off Ver
    2010, 2013
    Posts
    192

    Re: VBA script to extract data based on multiple columns

    Thanks to both of you.

    nankw83 i try your idea but it works on some matching and some skip.

    Vraag let me try your idea and get back you with feedback.

    Thanks once again.

  11. #11
    Forum Contributor
    Join Date
    09-13-2013
    Location
    Saudi Arabia
    MS-Off Ver
    2010, 2013
    Posts
    192

    Re: VBA script to extract data based on multiple columns

    Thanks for your help.

    Might be i have to approach in different way. So i will close this topic and open a new topic. I need to have a list on 1 sheet and validate those items and export the data.

    Thank once again.

  12. #12
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: VBA script to extract data based on multiple columns

    Glad to help & thanks for the reps

+ 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. [SOLVED] Extract data from table based on criteria applied to multiple columns
    By Feremartinez in forum Excel General
    Replies: 9
    Last Post: 11-21-2018, 12:34 PM
  2. [SOLVED] Extract Unique values based from multiple columns
    By kobiashi in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 06-14-2018, 10:10 AM
  3. [SOLVED] vba script to calculate data across multiple columns
    By pchappo in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-14-2017, 04:50 AM
  4. [SOLVED] Extract data based on criteria from multiple columns
    By madmoo84 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-08-2017, 07:29 AM
  5. [SOLVED] macro needed to extract specific columns out of multiple columns with their row data
    By genetist in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-07-2014, 12:49 AM
  6. Replies: 11
    Last Post: 10-30-2013, 05:04 AM
  7. Extract Data based on conditions on multiple columns
    By checkoncomp in forum Excel General
    Replies: 6
    Last Post: 07-03-2013, 01:54 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