I hope you're still around Tetra.
If we run this macro below with only ONE file open it works perfectly!
If multiple files are open, then the filename doesn't get created (i.e. -.csv vs. TueNov01-61.csv), it's doesn't close as it should and it invariably only processes about a 3rd of the records in the file.
We had to keep parameters open because filenames are always changing, not static. I'm sure this has created the problem.
The idea is to convert the active workbook a .csv or .xls file with these columns...
FirstName LastName HomePhone TimeZone StreetAddress City State ZipCode Email IP TimeandDate Gender BTC Priority Reason MostImportant HowMuch Groupname
To a .csv file with the name (i.e. TueNov01-61.csv) and reducing column fields down to these (combining several into a couple columns)...
FirstName LastName Email CompanyName HomePhone StreetAddress City State ZipCode WorkPhone Notes
And both files simply closing quitely with the source file unchanged.
With only ONE open active workbook, it works like a charm!
I sure hope someone can help.
If we run this macro below with only ONE file open it works perfectly!
If multiple files are open, then the filename doesn't get created (i.e. -.csv vs. TueNov01-61.csv), it's doesn't close as it should and it invariably only processes about a 3rd of the records in the file.
We had to keep parameters open because filenames are always changing, not static. I'm sure this has created the problem.
The idea is to convert the active workbook a .csv or .xls file with these columns...
FirstName LastName HomePhone TimeZone StreetAddress City State ZipCode Email IP TimeandDate Gender BTC Priority Reason MostImportant HowMuch Groupname
To a .csv file with the name (i.e. TueNov01-61.csv) and reducing column fields down to these (combining several into a couple columns)...
FirstName LastName Email CompanyName HomePhone StreetAddress City State ZipCode WorkPhone Notes
And both files simply closing quitely with the source file unchanged.
With only ONE open active workbook, it works like a charm!
Code:
Option Explicit
Sub CreateMobilCTI_ImportFile()
Dim wb As Workbook
Dim wk1 As Worksheet
Dim wk2 As Worksheet
Dim i As Long, j As Long
Dim mb As String, rply As String
If Workbooks.count >= 2 Then
Set wb = Workbooks(2)
Else
Set wb = ThisWorkbook
mb = "There is no any other Spreadsheet or csv file open" & Chr(10) & "to process the project, please open csv file " & Chr(10) & Chr(10) & "Please Press OK for Exit"
Exit Sub
End If
wb.Activate
If wb.Worksheets.count <= 1 Then
wb.Worksheets.Add(, Worksheets(1)).Name = 2
End If
If wb.Worksheets.count >= 3 Then
For j = wb.Worksheets.count To 3 Step -1
wb.Worksheets(j).Delete
Next j
End If
Set wk1 = Worksheets(1)
Set wk2 = Worksheets(2)
Application.DisplayAlerts = False
Application.ScreenUpdating = False
i = 1
Dim pth As String
Do Until IsEmpty(wk1.Cells(i, 1))
wk2.Cells(i, 1) = wk1.Cells(i, 1) ' FirstName
wk2.Cells(i, 2) = wk1.Cells(i, 2) ' LastName
wk2.Cells(i, 5) = wk1.Cells(i, 3) ' HomePhone
If i = 1 Then
wk2.Cells(i, 6) = "StreetAddress" ' StreetAddress
Else
wk2.Cells(i, 6) = wk1.Cells(i, 5)
End If
wk2.Cells(i, 7) = wk1.Cells(i, 6) ' City
wk2.Cells(i, 8) = wk1.Cells(i, 7) ' State
If i = 1 Then
wk2.Cells(i, 9) = "ZipCode" ' ZipCode
Else
wk2.Cells(i, 9) = wk1.Cells(i, 8)
End If
wk2.Cells(i, 3) = wk1.Cells(i, 9) ' Email
If i = 1 Then
wk2.Cells(i, 4) = "CompanyName"
Else
wk2.Cells(i, 4) = wk1.Cells(i, 12) & ":" & wk1.Cells(i, 13) ' CompanyName
End If
If i = 1 Then
wk2.Cells(i, 11) = "Notes"
Else
wk2.Cells(i, 11) = wk1.Cells(i, 11) & ":" & wk1.Cells(i, 14) & ":" & wk1.Cells(i, 15) & ":" & wk1.Cells(i, 16) & ":" & wk1.Cells(i, 17) ' Notes
End If
If i = 1 Then
wk2.Cells(i, 10) = "WorkPhone"
Else
wk2.Cells(i, 10) = "" ' WorkPhone
End If
Range("I:I").NumberFormat = "00000"
i = i + 1
Loop
i = 2
Dim fn1 As String, fn2 As String
' Creating file name
fn1 = Mid(wb.Worksheets(1).Cells(i, 18), 1, 3) & Mid(wb.Worksheets(1).Cells(i, 18), 5, 3) & Mid(wb.Worksheets(1).Cells(i, 18), 9, 2) & " - " & Mid(wb.Worksheets(1).Cells(i, 10), 1, 2)
' Creating path to save
' pth = ActiveWorkbook.Path ' At that time it is path - i.e.Windows default "where this file was saved"
pth = "C:\President Files\Leads\Leads Temp\fulfillment\ACTIVE" ' You can change path just like as too
' Check Existing file & Saving
For j = 1 To 10
If Dir(pth & "\" & fn1 & ".csv") = "" Then
wk1.Delete
wb.SaveAs Filename:=pth & "\" & fn1 & ".csv", FileFormat:=xlCSV ', ConflictResolution:=xlLocalSessionChanges ' currentBook.Close SaveChanges:=False
wb.Close
Exit Sub
Else
fn2 = Mid(wb.Worksheets(1).Cells(i, 18), 1, 3) & Mid(wb.Worksheets(1).Cells(i, 18), 5, 3) & Mid(wb.Worksheets(1).Cells(i, 18), 9, 2) & "-" & CLng(Mid(wb.Worksheets(1).Cells(i, 10), 1, 2)) + j
If Dir(pth & "\" & fn2 & ".csv") = "" Then
rply = InputBox("File " & pth & "\" & fn1 & ".csv" & vbCrLf & "already exist, Would you like to change this file" & vbCrLf & "with following available name or change name as you like", "CSV File Name", pth & "\" & fn2 & ".csv", vbOKCancel)
If rply = "" Then
MsgBox "File name is not valid"
wk1.Delete
wb.Close
Exit Sub
Else
fn1 = fn2
End If
End If
End If
Next j
wb.Close
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
I sure hope someone can help.