Link to home
Start Free TrialLog in
Avatar of Conernesto
ConernestoFlag for United States of America

asked on

How do I extract data from Excel worksheets?

I have many Excel workbooks in my path C:\ASIA . All the workbooks are structured the same (the extension on my workbooks is .xlsm). In each workbook I have a sheet named "CompanyInformation". I need to capture from CompanyInformtion the contents from each of the following cells.

Cells    I9, J10, H11, A12, P14, H18, AND H19

Is there a macro that I can run from Excel that can copy the contents in each of the above cells from each tab named CompanyInformation?  The macro would start importing to my current open Excel beginning in cell A1 the contents of cell I9, in cell B2 the contents of cell J10 until you get to cell G which will get the contents of cell H19. The contents of the next workbook would be imported to cell B1 and so on until all the information from all the workbooks is imported.

Thank you

Conernesto
Avatar of Mike in IT
Mike in IT
Flag of United States of America image

This is certainly possible. Do you have any VBA experience? Are you needing a whole solution? IF you need us to program it for you completely we'll need more info I think.
  • Name of current workbook
  • Data types to be copied
  • Is the order important?
  • What are the different cells for (so that we can name the variables appropriately)
  • Can you modify the VBA that we produce to suit your needs?

That being said I think this might do the trick:
Sub LoopAllExcelFilesInFolder()
Dim wb As Workbook, wbMain As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim One, Two, Three, Four, Five, Six, Seven, Counter
Counter = 1

Set wbMain = ActiveWorkbook
'Optimize Macro Speed
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With

'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
  myExtension = "*.xls*"

'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
  Do While myFile <> ""
    'Set variable equal to opened workbook
      Set wb = Workbooks.Open(FileName:=myPath & myFile)
    
    'Ensure Workbook has opened before moving on to next line of code
      DoEvents
      
      One = wb.Sheets("CompanyInformation").Range("I9").Value
      Two = wb.Sheets("CompanyInformation").Range("J10").Value
      Three = wb.Sheets("CompanyInformation").Range("H11").Value
      Four = wb.Sheets("CompanyInformation").Range("A12").Value
      Five = wb.Sheets("CompanyInformation").Range("P14").Value
      Six = wb.Sheets("CompanyInformation").Range("H18").Value
      Seven = wb.Sheets("CompanyInformation").Range("H19").Value
      
      wbMain.Sheets("CompanyInformation").Range("A" & Counter).Value = One
      wbMain.Sheets("CompanyInformation").Range("B" & Counter).Value = Two
      wbMain.Sheets("CompanyInformation").Range("C" & Counter).Value = Three
      wbMain.Sheets("CompanyInformation").Range("D" & Counter).Value = Four
      wbMain.Sheets("CompanyInformation").Range("E" & Counter).Value = Five
      wbMain.Sheets("CompanyInformation").Range("F" & Counter).Value = Six
      wbMain.Sheets("CompanyInformation").Range("G" & Counter).Value = Seven
      
      Counter = Counter + 1
      
    'Ensure Workbook has closed before moving on to next line of code
      DoEvents

    'Get next file name
      myFile = Dir
  Loop

'Message Box when tasks are completed
  MsgBox "Task Complete!"

ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Conernesto

ASKER

Hi Ray,

Your code works.  Can you modify the code so that the contents from cells I9, J10, H11, A12, P14, H18, AND H19 is placed on a row vs. down on a column?
do you mean the data from A1 to A9 will be A1 to I1?

just select all the cells, copy  select A10 and paste special > transpose
Thank you as always great job.