Sub import_multiple_s2p()
Dim xFilesToOpen As Variant
Dim I As Integer
Dim xWb As Workbook
Dim xTempWb As Workbook
Dim xDelimiter As String
Dim xScreen As Boolean
xScreen = Application.ScreenUpdating
Application.ScreenUpdating = False
xDelimiter = "|"
xFilesToOpen = Application.GetOpenFilename("Text Files (*.s2p), *.s2p", , "Import *.s2p files", , True)
If TypeName(xFilesToOpen) = "Boolean" Then
MsgBox "No files were selected", , "Import *.s2p files"
GoTo ExitHandler
End If
I = 1
Set xTempWb = Workbooks.Open(xFilesToOpen(I))
xTempWb.Sheets(1).Copy
Set xWb = Application.ActiveWorkbook
xTempWb.Close False
xWb.Worksheets(I).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=False, Semicolon:=False, _
Comma:=False, Space:=True, _
Other:=True, OtherChar:="|"
'Rows("1:6").Delete
Do Until ActiveCell.Value = "#"
Selection.EntireRow.Delete
Loop
Cells(1, 1).Value = "MHZ"
Cells(1, 2).Value = "S11 MAGNITUDE"
Cells(1, 3).Value = "S11 PHASE"
Cells(1, 4).Value = "S21 MAGNITUDE"
Cells(1, 5).Value = "S21 PHASE"
Cells(1, 6).Value = "S12 MAGNITUDE"
Cells(1, 7).Value = "S21 PHASE"
Cells(1, 8).Value = "S22 MAGNITUDE"
Cells(1, 9).Value = "S22 PHASE"
Range("A2", Range("A2").End(xlDown)).NumberFormat = "0000"
Range("B2", Range("B2").End(xlDown)).NumberFormat = "00.000000"
Range("D2", Range("D2").End(xlDown)).NumberFormat = "00.000000"
Range("F2", Range("F2").End(xlDown)).NumberFormat = "00.000000"
Range("H2", Range("H2").End(xlDown)).NumberFormat = "00.000000"
Range("C2", Range("C2").End(xlDown)).NumberFormat = "000.0000"
Range("E2", Range("E2").End(xlDown)).NumberFormat = "000.0000"
Range("G2", Range("G2").End(xlDown)).NumberFormat = "000.0000"
Range("I2", Range("I2").End(xlDown)).NumberFormat = "000.0000"
Do While I < UBound(xFilesToOpen)
I = I + 1
Set xTempWb = Workbooks.Open(xFilesToOpen(I))
With xWb
xTempWb.Sheets(1).Move after:=.Sheets(.Sheets.Count)
.Worksheets(I).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=False, Semicolon:=False, _
Comma:=False, Space:=True, _
Other:=True, OtherChar:=xDelimiter
'Rows("1:6").Delete
Do Until ActiveCell.Value = "#"
Selection.EntireRow.Delete
Loop
Cells(1, 1).Value = "MHZ"
Cells(1, 2).Value = "S11 MAGNITUDE"
Cells(1, 3).Value = "S11 PHASE"
Cells(1, 4).Value = "S21 MAGNITUDE"
Cells(1, 5).Value = "S21 PHASE"
Cells(1, 6).Value = "S12 MAGNITUDE"
Cells(1, 7).Value = "S21 PHASE"
Cells(1, 8).Value = "S22 MAGNITUDE"
Cells(1, 9).Value = "S22 PHASE"
Range("A2", Range("A2").End(xlDown)).NumberFormat = "0000"
Range("B2", Range("B2").End(xlDown)).NumberFormat = "00.000000"
Range("D2", Range("D2").End(xlDown)).NumberFormat = "00.000000"
Range("F2", Range("F2").End(xlDown)).NumberFormat = "00.000000"
Range("H2", Range("H2").End(xlDown)).NumberFormat = "00.000000"
Range("C2", Range("C2").End(xlDown)).NumberFormat = "000.0000"
Range("E2", Range("E2").End(xlDown)).NumberFormat = "000.0000"
Range("G2", Range("G2").End(xlDown)).NumberFormat = "000.0000"
Range("I2", Range("I2").End(xlDown)).NumberFormat = "000.0000"
End With
Loop
ExitHandler:
Application.ScreenUpdating = xScreen
Set xWb = Nothing
Set xTempWb = Nothing
Exit Sub
End Sub
Here is an example of how an .s2p file looks, the formatting is the same (with the first column denoting the frequency, then 8 columns of data following), but the data can go on indefinitely, and there can be any number of comments at the top. It is just a text file with .s2p as the extenstion.
Bookmarks