I got a good part a script and from here I do not know where to start.
In the script below I open a file move the columns and save the file in a new folder with date and time in front of it.
What I would like to do is copy those columns to a new File
I do not mind changing the way this script goes I can change it completly
Set objArgs = WScript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
For I = 0 to objArgs.Count - 1
CmplName = Year(Now()) & Month(Now()) & Day(Now()) & "H" & Hour(Now()) & "_"
FullName = objArgs(I)
FileName = Left(objArgs(I), InstrRev(objArgs(I), ".") )
RdyPath = "OrReady"
FNPathLen = InstrRev(FullName, "\")
FNLen = Len(FullName)
SNLen = FNLen-FNPathLen
ShortFullName = Right(FullName, SNLen)
ShortFileName = Left(ShortFullName, InstrRev(ShortFullName, ".") )
AdSavPath = Left(FullName, FNPathLen) & RdyPath & "\"
If fso.FolderExists(AdSavPath) Then
Else
fso.CreateFolder(AdSavPath)
End If
Set objExcel = CreateObject("Excel.application")
set objExcelBook = objExcel.Workbooks.Open(FullName)
objExcel.application.visible=false
objExcel.application.displayalerts=false
Set Cols = objExcel.Range("C1","C100000")
Set TCols = objExcel.Range("R1","R100000")
Cols.Cut
TCols.Insert
Set Cols = objExcel.Range("B1","B100000")
Set TCols = objExcel.Range("F1","F100000")
Cols.Cut
TCols.Insert
NewFile = AdSavPath & CmplName & ShortFileName & "xlsx"
objExcel.Workbooks(ShortFullName).SaveAs _
AdSavPath & CmplName & ShortFileName & "xlsx", 51
objExcel.Application.Quit
objExcel.Quit
Set objExcel = Nothing
set objExcelBook = Nothing
If fso.FileExists(NewFile) Then
MsgBox NewFile & " Exist Original File will be deleted => " & FullName
fso.DeleteFile(FullName)
Else
MsgBox " File Was Not Created "& NewFile & " (Did not Exist) Did not Delete Original File"
End If
Next
Related
I have code in Access that opens an excel template then saves it based on filepath. The template is then closed and I have put code in that will open the recently saved file. However, the file that opens is the temp.xlsm.
I have spent hours trying to figure out where I am going wrong but no luck, unless I am asking google the wrong question.
This is the part of my code that opens the template, saves it as, and then closes the template to reopen the saved file. Debugs are giving the correct file paths so I can't understand why the temp file keeps opening.
Dim objXLApp As Object
Dim objXLBook As Object
Dim objXLBookNew As Object
Dim FilePath As String
Dim strPath As String
Set objXLApp = CreateObject("Excel.Application")
FilePath = Forms!fm_MainMenu!fm_FilePath_Template.Form!File_Path & "\" & Forms!fm_MainMenu!fm_FilePath_Template.Form!File_Name & "." & Forms!fm_MainMenu!fm_FilePath_Template.Form!File_Type
Debug.Print FilePath
Set objXLBook = objXLApp.Workbooks.Open(FilePath)
objXLApp.Application.Visible = True
Maxletter = DMax("LetterID", "tbl_ReportNo", "[JobNo]= '" & [Forms]![fm_JobHeader]![Job_No] & "'")
Me.ReportNo = DLookup("Letter", "tbl_LetterID", "Letter_ID = " & Maxletter)
objXLBook.ActiveSheet.Range("AI13") = [Forms]![fm_JobHeader]![Job_No] & DLookup("Letter", "tbl_LetterID", "Letter_ID = " & Maxletter)
objXLBook.ActiveSheet.Range("N15") = [Forms]![fm_JobHeader]![Client].Column(1)
objXLBook.ActiveSheet.Range("AF14") = [Forms]![fm_JobHeader]![PONo]
objXLBook.ActiveSheet.Range("S17") = [Forms]![fm_JobHeader]![JobDescription]
objXLBook.ActiveSheet.Range("AT15") = Now()
objXLBook.ActiveSheet.Range("T13") = Me.Discipline.Column(1)
objXLBook.ActiveSheet.Range("Z13") = Me.Type.Column(1)
objXLBook.ActiveSheet.Range("AT16") = Me.RequestWONo
objXLBook.ActiveSheet.Range("S19") = Me.SiteLocation.Column(1)
Dim path_ As String
path_ = Forms!fm_MainMenu!fm_FilePath_Report.Form!File_Path & "\" & [Forms]![fm_JobHeader]![Job_No] & DLookup("Letter", "tbl_LetterID", "Letter_ID = " & Maxletter)
Dim name_ As String
name_ = [Forms]![fm_JobHeader]![Job_No] & DLookup("Letter", "tbl_LetterID", "Letter_ID = " & Maxletter) & ".xlsm"
Debug.Print path_
Debug.Print name_
With CreateObject("Scripting.FileSystemObject")
If Not .FolderExists(path_) Then .CreateFolder path_
End With
objXLBook.SaveCopyAs FileName:=path_ & "\" & name_
objXLBook.Close False
Set objXLBook = Nothing
Dim path2_ As String
path2_ = Forms!fm_MainMenu!fm_FilePath_Report.Form!File_Path & "\" & [Forms]![fm_JobHeader]![Job_No] & DLookup("Letter", "tbl_LetterID", "Letter_ID = " & Maxletter)
Dim name2_ As String
name2_ = [Forms]![fm_JobHeader]![Job_No] & DLookup("Letter", "tbl_LetterID", "Letter_ID = " & Maxletter) & ".xlsm"
Set objXLApp = CreateObject("Excel.Application")
Set objXLBookNew = objXLApp.Workbooks.Open(path2_ & "\" & name2_)
Debug.Print path2_
Debug.Print name2_
objXLApp.Application.Visible = True
DoCmd.Hourglass False ' turn off hourglass
Set objXLBookNew = Nothing
Set db = Nothing
Set rec = Nothing
Debug of file_ C:\Users\OneDrive\Current Working Docs\Reports Database\ABC123AY
Debug of path_ ABC123AY.xlsm
Debug of file2_ C:\Users\OneDrive\Current Working Docs\Reports Database\ABC123AY
Debug of path2_ ABC123AY.xlsm
But file that is being opened is
C:\Temp\Temp.xlsm
Maybe I have been looking at this too long and the obvious is staring me in the face but I am gaining frustration. Any help very much appreciated.
I have used hyperlinks.add several times now and never had any problems with it.
Now I added a line of code: SourceBook.Sheets(ESN & "_SV" & SV).Hyperlinks.Add Anchor:=Range("A" & i), _
Address:=ToPath & NewName to my base code (which you can find under here). This should add a link to the newly created document.
The problem is that excel always says it cannot open the file. The link I enter via code is right, as I copied it with debug.print and it opened the file without a problem.
It came to my attention that the hyperlink I added was modified by excel when I hold my mouse over the hyperlink. I wonder how this is possible.
A second problem I encounterd is that when I enter the hyperlink manually and navigate manually to the file to make sure it takes the right file, excel still modifies my link and says "cannot open specified file".
Anyone an idea what might go wrong here? Thanks!
Code:
`Application.ScreenUpdating = False
Dim i, j, FSO As Object, SV, ESN, PartName, ToPath, FromPath, NewName, MsgBoxAnswer, TargetBook As Workbook, SourceBook As Workbook
Dim OS, PN, SN, ProjectNumber, Customer, StartDate, EndDate, LastRowCMM
ESN = ActiveWorkbook.ActiveSheet.Range("G2").Value
SV = ActiveWorkbook.ActiveSheet.Range("K2").Value
ProjectNumber = ActiveWorkbook.ActiveSheet.Range("A3").Value
Customer = ActiveWorkbook.ActiveSheet.Range("G3").Value
Set FSO = CreateObject("scripting.filesystemobject")
PGB.Min = 0
PGB.Value = 0
PGB.Max = 22
'Create main folder
If SV <> 1 Then
SV = "(SV " & SV & ")"
ToPath = "U:\tmo\vanmolle\fiches constat\Fiches constats #" & ESN & " " & SV
Else
ToPath = "U:\tmo\vanmolle\fiches constat\Fiches constats #" & ESN
End If
If FSO.folderexists(ToPath) = True Then
MsgBoxAnswer = MsgBox("Folder already created.", vbExclamation, "Folder exists.")
Exit Sub
End If
FSO.createfolder (ToPath)
'Create all Excel files & fill them in
For i = 6 To 27
FromPath = "U:\tmo\VANMOLLE\Fiches constat\Template fiches constat LEAP.xlsm"
If SV <> 1 Then
ToPath = "U:\tmo\vanmolle\fiches constat\Fiches constats #" & ESN & " " & SV & "\"
Else
ToPath = "U:\tmo\vanmolle\fiches constat\Fiches constats #" & ESN & "\"
End If
FSO.copyfile Source:=FromPath, Destination:=ToPath
NewName = "#" & ESN & "_" & ActiveWorkbook.ActiveSheet.Range("A" & i) & ".xlsm"
If SV <> 1 Then
FromPath = "U:\tmo\vanmolle\fiches constat\Fiches constats #" & ESN & " " & SV & "\Template fiches constat LEAP.xlsm"
Else
FromPath = "U:\tmo\vanmolle\fiches constat\Fiches constats #" & ESN & "\Template fiches constat LEAP.xlsm"
End If
Name FromPath As ToPath & NewName
Set SourceBook = ThisWorkbook
Set TargetBook = Workbooks.Open(ToPath & NewName)
TargetBook.Sheets("Sheet1").Activate
PartName = SourceBook.ActiveSheet.Range("A" & i).Value
OS = SourceBook.ActiveSheet.Range("D" & i).Value
PN = SourceBook.ActiveSheet.Range("B" & i).Value
SN = SourceBook.ActiveSheet.Range("C" & i).Value
If SN = "" Then SN = "N/A"
StartDate = SourceBook.ActiveSheet.Range("G" & i).Value
EndDate = SourceBook.ActiveSheet.Range("H" & i).Value
'check for right CMM
'LastRowCMM = TargetBook.Sheets("Révision CMM").Range("B6").End(xlDown).Row
'For j = 1 To LastRowCMM
'If PartName = TargetBook.Sheets("Révision CMM").Range("A" & j).Value Then ActiveWorkbook.ActiveSheet.Range("A23").Value = ActiveWorkbook.Sheets("Révision CMM").Range("B" & j).Value
'Next j
TargetBook.ActiveSheet.Range("B9").Value = PartName
TargetBook.ActiveSheet.Range("B10").Value = OS
TargetBook.ActiveSheet.Range("B11").Value = "# " & ESN
TargetBook.ActiveSheet.Range("B12").Value = PN
TargetBook.ActiveSheet.Range("B13").Value = SN
TargetBook.ActiveSheet.Range("E9").Value = StartDate
TargetBook.ActiveSheet.Range("E10").Value = EndDate
TargetBook.ActiveSheet.Range("B14").Value = ProjectNumber
TargetBook.ActiveSheet.Range("B15").Value = Customer
TargetBook.ActiveSheet.PageSetup.PrintArea = "$A$1:$E$39"
TargetBook.Close True
'Add hyperlink
SourceBook.Sheets(ESN & "_SV" & SV).Hyperlinks.Add Anchor:=Range("A" & i), _
Address:=ToPath & NewName
Application.Wait (Now + TimeValue("00:00:01"))
Progress.PGB.Value = i - 5
Progress.Lbl.Caption = "File " & i - 5 & " of 22 copied."
Next i
Application.ScreenUpdating = True`
First thing first - declare each variable explicitly. E.g.:
Dim i as Long, j as Long, FSO As Object, SV as String, ESN as String and etc.
The way in your code - Dim i, j, SV, ESN, PartName, ToPath they are declared as variant.
Second thing second - try something really very small to debug further. E.g. write this small piece:
Sub TestMe()
With Worksheets(1)
.Hyperlinks.Add anchor:=.Range("A1"), Address:="C:\Users\UserName\Desktop\test.docx"
End With
End Sub
and check whether it works. If it doesn't, debug further, check whether cells are locked or anything similar.
I have implemented the below script but could not save the same as UTF-8 format.
How to save the converted CSV from Excel in UTF-8 CSV format using VBScript?
Dim strExcelFileName
Dim strCSVFileName
strExcelFileName = "C:\Users\test.xlsx"
Set fso = CreateObject("Scripting.FileSystemObject")
strScript = WScript.ScriptFullName
strScriptPath = fso.GetAbsolutePathName(strScript & "\..")
LPosition = InStrRev(strExcelFileName, "\")
If LPosition = 0 Then
strExcelFileName = strScriptPath & "\" & strExcelFileName
strScriptPath = strScriptPath & "\"
Else
strScriptPath = Mid(strExcelFileName, 1, LPosition)
End If
Set objXL = CreateObject("Excel.Application")
Set objWorkBook = objXL.Workbooks.Open(strExcelFileName)
objXL.DisplayAlerts = False
For Each sheet In objWorkBook.Sheets
If objXL.Application.WorksheetFunction.CountA(sheet.Cells) <> 0 Then
sheet.SaveAs strScriptPath & sheet.Name & cell & ".csv", 6 'CSV
End If
Next
objWorkBook.Close
objXL.Quit
Set objXL = Nothing
Set objWorkBook = Nothing
Set fso = Nothing
Try 62 instead of 6 in the save line like this:
sheet.SaveAs strScriptPath & sheet.Name & cell & ".csv", 62
I want to open workbook up to variable in the archive list.
If I don't have the file in the archive, I want it to show a message box, but it did not work.
strVariable = Left(PictureNo, 4)
d = "Teknik Resim Arsiv Listesi_" & strVariable & ".xls"
Dim Ret
Ret = Workbooks.Open(ThisWorkbook.Path & Application.PathSeparator & d)
If Ret = False Then
MsgBox "Not Found"
End If
Check for the existence of the file before attempting to open it:
strVariable = Left(PictureNo, 4)
d = "Teknik Resim Arsiv Listesi_" & strVariable & ".xls"
If Dir(ThisWorkbook.Path & Application.PathSeparator & d) = "" Then
MsgBox "Not Found"
Else
Dim wb As Workbook
Set wb = Workbooks.Open(ThisWorkbook.Path & Application.PathSeparator & d)
End If
Hi I am trying to create a code that checks for part of a file name and then will save the document with a increment of that file name, for example the below is creating a file name then using left to get just the part I want to check if it exists in any form but how do I check if a filename contains that info
TicketNumber = "0"
FileE = 1
UName = Range("C1")
On Error GoTo ErrorDocumentName
With New FileSystemObject
Do While FileE = 1
FileName = "REM" & TicketNumber & " - " & UName & ".xlsm"
Pos = InStr(FileName, "-")
LeftFN = Left(FileName, Pos - 2)
LeftFP = ThisWorkbook.Path & "\" & LeftFN
FileP = ThisWorkbook.Path & "\" & FileName
DisplayName = "REM" & TicketNumber
If .FileExists(LeftFP & WildC) Then
TicketNumber = TicketNumber + 1
ElseIf Not .FileExists(LeftFP & WildC) Then
FileE = 0
End If
Loop
End With
On Error GoTo ErrorRunning
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:=FileP, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
ActiveSheet.Name = "Incident"
ActiveWorkbook.Save
Application.DisplayAlerts = True
Exit Sub