We help IT Professionals succeed at work.

How do I extract data from Excel worksheets?

169 Views
Last Modified: 2017-03-08
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
Comment
Watch Question

Mike in ITIT System Administrator
Distinguished Expert 2017

Commented:
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

CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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?
CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Author

Commented:
Thank you as always great job.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.