How Would You Analyse This Data

Parra

Well-known Member
Joined
Feb 21, 2002
Messages
752
BAKERSFIELD KUVI (UPN FP) 11-15-03.xls
ABCDEFGHIJKL
1CABLECOMPANYTotSubs%HispHispSubsKSTVKJTVKTLAKCALKCOPKWBTKYRSKNAS
2ADVANCENEWHOUSECOMMINC92,00048.1%44,2524173031
3ADVANCENEWHOUSECOMMINC6,80021.5%1,4624232422
4COUNTRYCABLE-BEARVALLEY6,80021.5%1,4628
5COXCBLCOMMUNICATIONSINC27,40037.1%10,1654122913
6MEDIACOMLLC6,4006.4%4104
7SUNTELCOMMUNICATIONSLLC3,97813.5%53745
8COUNTRYCABLE-BEARVALLEY92,00048.1%44,2524173031
9COXCBLCOMMUNICATIONSINC6,80021.5%1,4624232422
10MEDIACOMLLC6,80021.5%1,4628
11SUNTELCOMMUNICATIONSLLC6,4006.4%4104
Raw Data


I know how to get the data into access, that not the problem.

The problem is that I will have to import over 100 spreadsheets like what is above. The headers of Columns A through D will remain the same, but the green headers will change.

What I need to do is for example is to do an analysis of KSTV, if the cell below it is populated, then get what is in column D, for all the rows. Then KJTV, then KTLA etc.

I need some suggestions on how you would go about this, I am drawing a blank.

Thanks
Parra

Thanks Parra
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
1-Push all your spreadsheets into a single directory location.
2-Run VBA to look through the directory and write all the names to a table in your database.
3-Sequentially, open links to each file and then either:
a) Append the data to a master table
b) Parse the data (process as needed) and append results to master table
c) Close link and move to next file until done

This is pretty involved actually but, what the heck, right?

This should get you started. One of these days I'll go back and rewrite this to use better function names.

Call ReturnAllFiles to create a list of files in a given directory to tblFiles.
ObjectExists is part of a test to ensure the table exists. This gives you a OpenFolder dialog box. Header portion are API declarations. Drop the below into a new code module and modify to adjust to your needs.

Code:
Public Type BROWSEINFO
    hOwner As Long
    pidlRoot As Long
    pszDisplayName As String
    lpszTitle As String
    ulFlags As Long
    lpfn As Long
    lParam As Long
    iImage As Long
End Type

'32-bit API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
  Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long

Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

Public Function ReturnAllFiles(Optional ByVal selDir As String) As Boolean
Dim DirName As String
Dim TempName As String, FileNum As Integer
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL, strTBL As String

On Error GoTo HandleErr
strTBL = "tblFiles"

Set dbs = CurrentDb
If ObjectExists("Table", strTBL) Then
  strTBL = "tblFiles"
  strSQL = "DELETE * FROM " & strTBL
  DoCmd.RunSQL strSQL
Else
  ' Create the Table
End If
'C:\DirectoryLocation
strSQL = "SELECT * FROM tblFiles"
Set rs = dbs.OpenRecordset(strSQL, dbOpenDynaset)
   
    FileNum = FreeFile
    If selDir <> "C:\" Then
      DirName = selDir
    Else
      DirName = GetDirectory2() & "\"
      If Len(DirName) = 0 Then
        ReturnAllFiles = False
        Exit Function
      End If
    End If
    
    TempName = Dir$(DirName, vbDirectory)

    While Len(TempName)
        If (TempName <> ".") And (TempName <> "..") Then    'get rid of "." and ".."
            TempName = DirName & TempName
            'GetAttr is a built-in function
            If GetAttr(TempName) <> vbDirectory Then
                'Debug.Print TempName
                rs.AddNew
                rs.Fields(0).Value = TempName
                rs.Update
            End If
        End If
        TempName = Dir$
    Wend
    
    Close #FileNum

ReturnAllFiles = True

Set rs = Nothing
Set dbs = Nothing
End Function

Function ObjectExists(strObjectType As String, strObjectName As String) As Boolean
     Dim db As Database
     Dim tbl As TableDef
     Dim qry As QueryDef
     Dim i As Integer
     
     Set db = CurrentDb()
     ObjectExists = False
     
     If strObjectType = "Table" Then
          For Each tbl In db.TableDefs
               If tbl.Name = strObjectName Then
                    ObjectExists = True
                    Exit Function
               End If
          Next tbl
     ElseIf strObjectType = "Query" Then
          For Each qry In db.QueryDefs
               If qry.Name = strObjectName Then
                    ObjectExists = True
                    Exit Function
               End If
          Next qry
     ElseIf strObjectType = "Form" Or strObjectType = "Report" Or strObjectType = "Module" Then
          For i = 0 To db.Containers(strObjectType & "s").Documents.Count - 1
               If db.Containers(strObjectType & "s").Documents(i).Name = strObjectName Then
                    ObjectExists = True
                    Exit Function
               End If
          Next i
     ElseIf strObjectType = "Macro" Then
          For i = 0 To db.Containers("Scripts").Documents.Count - 1
               If db.Containers("Scripts").Documents(i).Name = strObjectName Then
                    ObjectExists = True
                    Exit Function
               End If
          Next i
     Else
          MsgBox "Invalid Object Type passed, must be Table, Query, Form, Report, Macro, or Module"
     End If
     
End Function

Public Function GetDirectory2(Optional Msg) As String

    Dim bInfo As BROWSEINFO
    Dim path As String
    Dim R As Long, x As Long

'   Root folder = Desktop
    bInfo.pidlRoot = 0&

'   Title in the dialog
    If IsMissing(Msg) Then
        bInfo.lpszTitle = "Select a folder."
    Else
        bInfo.lpszTitle = Msg
    End If
    
'   Type of directory to return
    bInfo.ulFlags = &H1

'   Display the dialog
    x = SHBrowseForFolder(bInfo)
    
'   Parse the result
    path = Space$(512)
    R = SHGetPathFromIDList(ByVal x, ByVal path)
    If R Then
          x = InStr(path, Chr$(0))
        GetDirectory2 = Left(path, x - 1)
    Else
        GetDirectory2 = ""
    End If

End Function
 
Upvote 0
OK, I'm looking to parse the data into a master table.

But how would I do that when the headers in the green column change?

I was thinking of eliminating the data in oranage with the header. The new header would be station1, station2...etc.
 
Upvote 0
The main issue is the data structure. The green headings are not really headings, they are items in a new field (called "Code", or whatever).
Then, when you import into Access you have 6 fields -- the 'unchanging' orange ones, plus "Code" and "Amount".
"Code" is KSTV, KTLA, KJTV, etc.
"Amount" is the value in the orange cells.

So...
1. In Excel, rearrange the data to fit the above. It can be automated by filtering out blanks in each "Code" column in turn, copying a block containing the first 4 fields, pasting the orange values alongside, and writing the "Code" item in column 6.

2. Import this into Access and append each set to the existing table.

Denis
 
Upvote 0
I've used this method before (pure VBA solution) and I'm not 100% certain it's the best/most efficient method, but here goes anyways.

Looking at your original data, which really looks like a summarization (Pivot Table?) of other data...You might be able to use it as is and still using SydneyGeek's idea.

If you first link/import the data into a table, I suppose you *know* that the first 4 columns (0 - 3) are going to be the same thing each time. What you can do is to use relative field references to extract the field name and use it as data, and the value as another field. You'd do something like this:

This accepts a variable number of columns without every specifying a fieldname to use/check reading it all into a variant array. You'll need to write the data into your output tables at this point.

One other thought I had is, you should probably use these data fields in a second relational table. Write the first four values to table 1, add in a unique key value, write that unique key value to a field in table 2 along with each fieldname/fieldvalue combination.

Code:
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strVal() As Variant
Dim strSQL As String

Set dbs = CurrentDB()

Set strSQL = "SELECT * FROM tblName"       ' Change
Set rs =dbs.OpenRecordset(strSQL, dbOpenSnapshot)

With rs
  Do Until rs.EOF
    lnFldCnt = .Fields.Count-1
    ReDim avarVal
    For x = 0 to lnFldCnt
      avarVal(x,0) = .Fields(x).Value
      'avarVal(x,1) = Nothing
      If lnFldCnt >= 4 Then              ' If reading the data fields
        avarVal(x,0) = .Fields(x).Name    'Saves that Fieldname
        avarVal(x,1) = .Fields(x).Value    'Saves that field value (amount)
      End If
    Next x
    
    ' Drop in routine or function/sub call to AddNew data to output table
    ' Clear variant array values
    
    .MoveNext
  Loop
End With

Please feel free to yell at me if I'm making this too complex.

Mike
 
Upvote 0
Thanks for all your recommendations.

I was able to generate the reports I needed, but I had to modify the data due to time constraints.

I most likely will be looking at this more in the coming future, because I think this is a project that will be done on a montly basis. I was just in a rush.

Parra
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,595
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top