Dark91zc
Board Regular
- Joined
- Aug 1, 2013
- Messages
- 62
Hello All and thank you.
I am new to programming in excel and have been getting help from one other forum. this is the link to my question on the other forum Changing and adding punctuation in a split macro?. i am trying to learn about the split code a little more in depth.
what i want to know is how can i add more signs or characters to split on?
this is the code i am working with right now and it is splitting into the 3 columns fine.
But i need it to split on the ^ sign also and add one more column. This is that i came up with but i am getting lost as for what i am not doing correct. (my add might be working against me).
This is what my complete code looks like right now.
I am trying to go from this
DOE^JOHN_12349876_19450125
SUE^JEFF_95157535_19181221
SMITH^LEE_84269713_19631124
to this
[TABLE="class: cms_table_grid, width: 500"]
<tbody>[TR]
[TD]last name[/TD]
[TD]first name[/TD]
[TD]mdr[/TD]
[TD]date of birth[/TD]
[/TR]
[TR]
[TD]DOE[/TD]
[TD]JOHN[/TD]
[TD]12349876[/TD]
[TD]19451201[/TD]
[/TR]
[TR]
[TD]JANE[/TD]
[TD]MARY[/TD]
[TD]00560803[/TD]
[TD]19430222[/TD]
[/TR]
</tbody>[/TABLE]
Thanks for the help and sorry for being a newb.
I am new to programming in excel and have been getting help from one other forum. this is the link to my question on the other forum Changing and adding punctuation in a split macro?. i am trying to learn about the split code a little more in depth.
what i want to know is how can i add more signs or characters to split on?
this is the code i am working with right now and it is splitting into the 3 columns fine.
Code:
If InStr(SubFolder.Name, "_") Then
Elements = Split(SubFolder.Name, "_")
Cells(r, 1) = Elements(0)
Cells(r, 2) = Elements(1)
Cells(r, 3) = Elements(2)
' Cells(r, 2).Formula = FileItem.Size
' Cells(r, 3).Formula = FileItem.Type
' Cells(r, 4).Formula = FileItem.DateCreated
' Cells(r, 5).Formula = FileItem.DateLastAccessed
' Cells(r, 6).Formula = FileItem.DateLastModified
' Cells(r, 7).Formula = FileItem.Attributes
' Cells(r, 8).Formula = FileItem.ShortPath & FileItem.ShortName
r = r + 1 ' next row number
End If<code></code><code></code>
But i need it to split on the ^ sign also and add one more column. This is that i came up with but i am getting lost as for what i am not doing correct. (my add might be working against me).
Code:
If InStr(SubFolder.Name, "_", "^") Then
Elements = Split(SubFolder.Name, "_", "^")
Cells(r, 1) = Elements(0)
Cells(r, 2) = Elements(1)
Cells(r, 3) = Elements(2)
Cells(r, 4) = Elements(3)
' Cells(r, 2).Formula = FileItem.Size
' Cells(r, 3).Formula = FileItem.Type
' Cells(r, 4).Formula = FileItem.DateCreated
' Cells(r, 5).Formula = FileItem.DateLastAccessed
' Cells(r, 6).Formula = FileItem.DateLastModified
' Cells(r, 7).Formula = FileItem.Attributes
' Cells(r, 8).Formula = FileItem.ShortPath & FileItem.ShortName
r = r + 1 ' next row number
End If
This is what my complete code looks like right now.
Code:
Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Sub ImportFilesInFolder()
'Workbooks.Add ' create a new workbook for the file list
' add headers
With Range("A1")
.Formula = "Folder contents:"
.Font.Bold = True
.Font.Size = 12
End With
Range("A3").Formula = "Last Name:"
Range("B3").Formula = "First Name:"
Range("C3").Formula = "MDR:"
Range("D3").Formula = "Date of Birth:"
' Range("E3").Formula = "Date Last Accessed:"
' Range("F3").Formula = "Date Last Modified:"
' Range("G3").Formula = "Attributes:"
' Range("H3").Formula = "Short File Name:"
Range("A3:H3").Font.Bold = True
Msg = "Select a location containing the files you want to list."
'Directory = GetDirectory(Msg)
'list all files included subfolders
ListFilesInFolder GetDirectory(Msg), True
End Sub
Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
' lists information about the files in SourceFolder
' example: ListFilesInFolder "C:\FolderName\", True
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
Dim FileItem As Scripting.File
Dim r As Long
Dim Elements As Variant
Dim Temp As String
Set FSO = New Scripting.FileSystemObject
Set SourceFolder = FSO.GetFolder(SourceFolderName)
r = Range("A65536").End(xlUp).Row + 1
For Each SubFolder In SourceFolder.SubFolders
' display file properties
'Temp = StrReverse(SubFolder.Name)
'Temp = Right(Temp, Len(Temp) - InStr(Temp, "."))
'Temp = StrReverse(Temp)
If InStr(SubFolder.Name, "_", "^") Then
Elements = Split(SubFolder.Name, "_", "^")
Cells(r, 1) = Elements(0)
Cells(r, 2) = Elements(1)
Cells(r, 3) = Elements(2)
Cells(r, 4) = Elements(3)
' Cells(r, 2).Formula = FileItem.Size
' Cells(r, 3).Formula = FileItem.Type
' Cells(r, 4).Formula = FileItem.DateCreated
' Cells(r, 5).Formula = FileItem.DateLastAccessed
' Cells(r, 6).Formula = FileItem.DateLastModified
' Cells(r, 7).Formula = FileItem.Attributes
' Cells(r, 8).Formula = FileItem.ShortPath & FileItem.ShortName
r = r + 1 ' next row number
End If
Next SubFolder
If IncludeSubfolders Then
For Each SubFolder In SourceFolder.SubFolders
ListFilesInFolder SubFolder.path, True
Next SubFolder
End If
Columns("A:D").AutoFit
Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
ActiveWorkbook.Saved = True
End Sub
Function GetDirectory(Optional Msg) As String
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
bInfo.pidlRoot = 0&
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
Else
bInfo.lpszTitle = Msg
End If
bInfo.ulFlags = &H1
x = SHBrowseForFolder(bInfo)
If x = 0 Then End
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function
I am trying to go from this
DOE^JOHN_12349876_19450125
SUE^JEFF_95157535_19181221
SMITH^LEE_84269713_19631124
to this
[TABLE="class: cms_table_grid, width: 500"]
<tbody>[TR]
[TD]last name[/TD]
[TD]first name[/TD]
[TD]mdr[/TD]
[TD]date of birth[/TD]
[/TR]
[TR]
[TD]DOE[/TD]
[TD]JOHN[/TD]
[TD]12349876[/TD]
[TD]19451201[/TD]
[/TR]
[TR]
[TD]JANE[/TD]
[TD]MARY[/TD]
[TD]00560803[/TD]
[TD]19430222[/TD]
[/TR]
</tbody>[/TABLE]
Thanks for the help and sorry for being a newb.