Hi everyone,
I will begin by apologizing for the lengthy post, but I wanted to make sure I included enough details to get the correct help. I am a long time Excel user, but fairly new to VBA. I have been trying to teach myself how to write a bit of VBA code to import data from text files, manipulate the data, and then export the data to a new text file after it has been optimized. I've been working on the VBA for the first command button on my worksheet (Import Point List) and have been trying to piece together bits of code I've found online. Now I'm approaching a deadline to get this done and I'm a bit stuck. Any assistance would be very much appreciated.
Here's a sample of the data in the text file. It will always be in this format, but the length of the file can vary from several hundred lines to several hundred thousand lines. There are no headers and the data starts on the first row of the file. Coordinates are not fa fixed length and may range from -250.0000 to 250.0000 for values.
sample1.txt
centroidpos[0] = X-5.2826 Y3.3400 Z0.5000
centroidpos[1] = X-5.4326 Y3.3400 Z0.5000
centroidpos[2] = X5.5826 Y-3.3400 Z0.5000
centroidpos[3] = X5.7326 Y-3.3400 Z0.5000
centroidpos[4] = X5.8826 Y3.3400 Z1.0000
centroidpos[5] = X6.0326 Y3.3400 Z1.0000
centroidpos[6] = X-6.1826 Y-3.3400 Z1.0000
centroidpos[7] = X-6.3326 Y-3.3400 Z1.0000
centroidpos[8] = X6.4826 Y3.3400 Z-0.5000
centroidpos[9] = X6.6326 Y3.3400 Z-0.5000
centroidpos[10] = X-6.7826 Y-3.3400 Z-0.5000
centroidpos[11] = X-6.9326 Y-3.3400 Z-0.5000
Desired Worksheet Result
https://1drv.ms/u/s!Ak5_WPnQMSfLiPg0CwFdHWzKJKHsGg
Here's what I'm trying to do:
1) Open a window with file filter so the user can select path and filename(s) to import. I'd like to use something like the following, including the MultiSelect:=True so that the user can import data from 1, 2, 3 or more text files at once and just append the data from each consecutive file to the bottom of each column.
Dim OpenFileName As Variant
' Select Point List file(s) and location to open in Excel
OpenFileName = Application.GetOpenFilename(FileFilter:="Point List Files (*.txt), *.txt", Title:="Select a file or files to import", MultiSelect:=True)
' Cancel data import and return to worksheet if user exits window without selecting a file
If OpenFileName = False Then
Exit Sub
End If
2) Clear any existing data from the Point List Data columns
' Clear any existing point list data and distance calculations
Range("H5:K1048576").ClearContents
3) Import only X, Y, and Z coordinate data from the text file(s), skipping the centroidpos[nn] and dropping the X, Y, and Z from the coordinate data. Data must be pasted to columns H, I, and J starting at row 5 (H5, J5, I5) as numbers with fixed 4 decimal places (nnn.nnnn)
4) Insert a formula starting at K6, then fill down this formula in column K adjacent to all cells populated from the data import.
K5 = blank cell
K6 = SQRT((H6-H5)^2+(I6-I5)^2)
K7 = SQRT((H7-H6)^2+(I7-I6)^2)
K8 = SQRT((H8-H7)^2+(I8-I7)^2)
...
5) Format cells H4:K4 with a light pink fill to show that the data in these columns has not been optimized yet.
Sample code I've pieced together so far, but definitely not complete! I'm not sure how to parse the data and paste it into the worksheet, add the formulas in column K, and format the fill colors for H4:K4. Any help would be awesome.
Private Sub Import_PL_Click()
' Timer Start (calculate the length of time this VBA code takes to complete)
Dim StartTime As Double
Dim SecondsElapsed As Double
StartTime = Timer
' Define variable names and types
Dim SaveCurrentDir As String
Dim DefaultOpenPath As String
Dim OpenFileName As Variant
Dim N As Long
Dim OpenFileNameInLoop As String
Dim DefaultSavePath As String
' Save the current directory
SaveCurrentDir = CurDir
' Set the path to the folder that you want to open by default
' Use DefaultOpenPath = Application.DefaultFilePath or define
' a custom path such as DefaultOpenPath = "C:\Temp"
DefaultOpenPath = "D:\GCT-Development"
' Change drive/directory to DefaultOpenPath
ChDrive DefaultOpenPath
ChDir DefaultOpenPath
' Select point list file and location to open in Excel
OpenFileName = Application.GetOpenFilename(FileFilter:="Point List Files (*.txt), *.txt", Title:="Select a file or files to import", MultiSelect:=True)
' Perform actions with the files selected
If IsArray(OpenFileName) Then
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
For N = LBound(OpenFileName) To UBound(OpenFileName)
' Get only the file name and test to see if it is open.
OpenFileNameInLoop = Right(OpenFileName(N), Len(OpenFileName(N)) - InStrRev(OpenFileName(N), Application.PathSeparator, , 1))
If bIsBookOpen(OpenFileNameInLoop) = False Then
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(OpenFileName(N))
On Error GoTo 0
If Not mybook Is Nothing Then
' Clear any existing point list data and distance calculations
Range("H5:K1048576").ClearContents
End If
Else
MsgBox "We skipped this file : " & OpenFileName(N) & " because it is already open."
End If
Next N
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End If
' Change drive/directory back to SaveCurrentDir.
ChDrive SaveCurrentDir
ChDir SaveCurrentDir
' Timer Stop (calculate the length of time this VBA code took to complete)
SecondsElapsed = Round(Timer - StartTime, 2)
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
End Sub
____________________________________________________________________________
Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Contributed by Rob Bovey
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function
I will begin by apologizing for the lengthy post, but I wanted to make sure I included enough details to get the correct help. I am a long time Excel user, but fairly new to VBA. I have been trying to teach myself how to write a bit of VBA code to import data from text files, manipulate the data, and then export the data to a new text file after it has been optimized. I've been working on the VBA for the first command button on my worksheet (Import Point List) and have been trying to piece together bits of code I've found online. Now I'm approaching a deadline to get this done and I'm a bit stuck. Any assistance would be very much appreciated.
Here's a sample of the data in the text file. It will always be in this format, but the length of the file can vary from several hundred lines to several hundred thousand lines. There are no headers and the data starts on the first row of the file. Coordinates are not fa fixed length and may range from -250.0000 to 250.0000 for values.
sample1.txt
centroidpos[0] = X-5.2826 Y3.3400 Z0.5000
centroidpos[1] = X-5.4326 Y3.3400 Z0.5000
centroidpos[2] = X5.5826 Y-3.3400 Z0.5000
centroidpos[3] = X5.7326 Y-3.3400 Z0.5000
centroidpos[4] = X5.8826 Y3.3400 Z1.0000
centroidpos[5] = X6.0326 Y3.3400 Z1.0000
centroidpos[6] = X-6.1826 Y-3.3400 Z1.0000
centroidpos[7] = X-6.3326 Y-3.3400 Z1.0000
centroidpos[8] = X6.4826 Y3.3400 Z-0.5000
centroidpos[9] = X6.6326 Y3.3400 Z-0.5000
centroidpos[10] = X-6.7826 Y-3.3400 Z-0.5000
centroidpos[11] = X-6.9326 Y-3.3400 Z-0.5000
Desired Worksheet Result
https://1drv.ms/u/s!Ak5_WPnQMSfLiPg0CwFdHWzKJKHsGg
Here's what I'm trying to do:
1) Open a window with file filter so the user can select path and filename(s) to import. I'd like to use something like the following, including the MultiSelect:=True so that the user can import data from 1, 2, 3 or more text files at once and just append the data from each consecutive file to the bottom of each column.
Dim OpenFileName As Variant
' Select Point List file(s) and location to open in Excel
OpenFileName = Application.GetOpenFilename(FileFilter:="Point List Files (*.txt), *.txt", Title:="Select a file or files to import", MultiSelect:=True)
' Cancel data import and return to worksheet if user exits window without selecting a file
If OpenFileName = False Then
Exit Sub
End If
2) Clear any existing data from the Point List Data columns
' Clear any existing point list data and distance calculations
Range("H5:K1048576").ClearContents
3) Import only X, Y, and Z coordinate data from the text file(s), skipping the centroidpos[nn] and dropping the X, Y, and Z from the coordinate data. Data must be pasted to columns H, I, and J starting at row 5 (H5, J5, I5) as numbers with fixed 4 decimal places (nnn.nnnn)
4) Insert a formula starting at K6, then fill down this formula in column K adjacent to all cells populated from the data import.
K5 = blank cell
K6 = SQRT((H6-H5)^2+(I6-I5)^2)
K7 = SQRT((H7-H6)^2+(I7-I6)^2)
K8 = SQRT((H8-H7)^2+(I8-I7)^2)
...
5) Format cells H4:K4 with a light pink fill to show that the data in these columns has not been optimized yet.
Sample code I've pieced together so far, but definitely not complete! I'm not sure how to parse the data and paste it into the worksheet, add the formulas in column K, and format the fill colors for H4:K4. Any help would be awesome.
Private Sub Import_PL_Click()
' Timer Start (calculate the length of time this VBA code takes to complete)
Dim StartTime As Double
Dim SecondsElapsed As Double
StartTime = Timer
' Define variable names and types
Dim SaveCurrentDir As String
Dim DefaultOpenPath As String
Dim OpenFileName As Variant
Dim N As Long
Dim OpenFileNameInLoop As String
Dim DefaultSavePath As String
' Save the current directory
SaveCurrentDir = CurDir
' Set the path to the folder that you want to open by default
' Use DefaultOpenPath = Application.DefaultFilePath or define
' a custom path such as DefaultOpenPath = "C:\Temp"
DefaultOpenPath = "D:\GCT-Development"
' Change drive/directory to DefaultOpenPath
ChDrive DefaultOpenPath
ChDir DefaultOpenPath
' Select point list file and location to open in Excel
OpenFileName = Application.GetOpenFilename(FileFilter:="Point List Files (*.txt), *.txt", Title:="Select a file or files to import", MultiSelect:=True)
' Perform actions with the files selected
If IsArray(OpenFileName) Then
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
For N = LBound(OpenFileName) To UBound(OpenFileName)
' Get only the file name and test to see if it is open.
OpenFileNameInLoop = Right(OpenFileName(N), Len(OpenFileName(N)) - InStrRev(OpenFileName(N), Application.PathSeparator, , 1))
If bIsBookOpen(OpenFileNameInLoop) = False Then
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(OpenFileName(N))
On Error GoTo 0
If Not mybook Is Nothing Then
' Clear any existing point list data and distance calculations
Range("H5:K1048576").ClearContents
End If
Else
MsgBox "We skipped this file : " & OpenFileName(N) & " because it is already open."
End If
Next N
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End If
' Change drive/directory back to SaveCurrentDir.
ChDrive SaveCurrentDir
ChDir SaveCurrentDir
' Timer Stop (calculate the length of time this VBA code took to complete)
SecondsElapsed = Round(Timer - StartTime, 2)
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
End Sub
____________________________________________________________________________
Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Contributed by Rob Bovey
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function