Extracting data from cells when non-excel file opened in excel

mrina970

New Member
Joined
Aug 8, 2012
Messages
15
I have a macro that works for extracting specific cells of data from .xlsx files.

Unfortunately, I first have to open each of the .dpt files in excel and re-save them as .xlsx files. Since the .dpt files can open in excel, is there a way to skip the middle step of saving each file as .xlsx and just go right from the .dpt file to taking the data?

Here is the macro.


Sub ExtractData()

Dim row As Long
Dim directory, fileName, initialFolder

' Startup folder to begin searching from
initialFolder = "C:\Exported Data\"

' Can set up headers here as desired to auto populate when run on an empty master file
Range("A1").Select
ActiveCell = "File Name"

Range("B1").Select
ActiveCell = "1700"

Range("C1").Select
ActiveCell = "1648"

Range("D1").Select
ActiveCell = "1583"

Range("E1").Select
ActiveCell = "1550"


' Uncomment this block below with the closing End If + End With to enable choosing a folder
' With Application.FileDialog(msoFileDialogFolderPicker)
'.InitialFileName = Application.DefaultFilePath & "\"
'.Title = "Please select a folder to list Files from"
'.InitialFileName = initialFolder
'.Show
'If .SelectedItems.Count <> 0 Then
'directory = .SelectedItems(1) & "\"

' start injecting data on row 2
row = 2

' Get file list
fileName = Dir(initialFolder)
' For all files found
Do While fileName <> ""

' Check the extension, only process xlsx files
' Also, do not process temporary files designated by prepended ~
fileExtension = Right(fileName, 5)
If StrComp(fileExtension, ".xlsx") = 0 And Left(fileName, 1) <> "~" Then

' used below
targetFileFullPath = initialFolder + fileName

' construct cell to store to
' file names go in colum A
fileNameCell = "A" + Trim(Str(row))
' select row in colum A
Range(fileNameCell).Select
' store the value
ActiveCell = fileName

copyToCell = "B" + Trim(Str(row))
Call CopyFromSheetToCellsInActive(targetFileFullPath, "B1192", copyToCell)

copyToCell = "C" + Trim(Str(row))
Call CopyFromSheetToCellsInActive(targetFileFullPath, "B1219", copyToCell)

copyToCell = "D" + Trim(Str(row))
Call CopyFromSheetToCellsInActive(targetFileFullPath, "B1253", copyToCell)

copyToCell = "E" + Trim(Str(row))
Call CopyFromSheetToCellsInActive(targetFileFullPath, "B1270", copyToCell)

' next row
row = row + 1
End If

' go to next file
fileName = Dir
Loop
'End If
'End With
End Sub


Sub CopyFromSheetToCellsInActive(copyFromSheet, copyFromCell, copyToCell)

' open excel spreadsheet with data to extract
Workbooks.Open (copyFromSheet)
' copy desired value
Range(copyFromCell).Copy
' close excel spreadsheet that had the desired data
ActiveWorkbook.Close

' switch back to the active workbook
ThisWorkbook.Activate
' paste into main workbook
Range(copyToCell).Select
ActiveSheet.Paste


End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,216,746
Messages
6,132,478
Members
449,729
Latest member
davelevnt

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