Hello C# experts, Can you help me convert this code from Excel VBA to C# ? I am
ID: 3789351 • Letter: H
Question
Hello C# experts,
Can you help me convert this code from Excel VBA to C# ? I am new to C#
Basically this program does open each file.CSV then read the specific cells(row,column) then rename this csv.file according cells(row, column). Loop entired folder
I have a following Excel VBA below need convert to C#. Thank you for help
'this program is rename a file in a subfolder, which is unknow subfolder name
' default directory "C:"
Option Explicit
Public Sub Process_Orders()
Dim Fso As Scripting.FileSystemObject
Set Fso = New Scripting.FileSystemObject
Process_CSV_Files Fso, "C:"
Set Fso = Nothing
End Sub
Sub Process_CSV_Files(Fso As Scripting.FileSystemObject, folderPath As String)
Application.ScreenUpdating = False
Dim Folder As Scripting.Folder, Subfolder As Scripting.Folder, File As Scripting.File, File1 As Scripting.File
Dim Msheet As Long 'count number of sheet for row copy
Dim MyPath, DirOldFile, DirNewFile As String
Dim StrDate As String
Dim wb As Workbook
Dim datasheet As Worksheet
Dim OldFileName, NewFileName As String
Set Folder = Fso.GetFolder(folderPath)
' 'search files in subfolder
For Each Subfolder In Folder.SubFolders
MyPath = Subfolder.Path & "" 'set the subfolder path
For Each File In Subfolder.Files 'each file in subfolder
OldFileName = File.Name 'csv file name
DirOldFile = MyPath & OldFileName ' set old file path
If InStr(OldFileName, ".csv") Then
If Dir(DirOldFile) <> "" Then 'if file exist
'Open File.Path workbook
Set wb = Workbooks.Open(File.Path)
Set datasheet = wb.Worksheets(1) 'set worksheet1 as open
NewFileName = datasheet.Range("B13") & ".csv" 'NewFileName is assigned in worksheet B13
DirNewFile = MyPath & NewFileName 'set new file path
wb.Close False 'close csv file
If OldFileName = NewFileName Then 'if new file name exist then rename date time and ID in csv
StrDate = Format(Now(), "yyyy-MM-dd hh:mm:ss")
Name DirOldFile As MyPath & StrDate & "_" & NewFileName
Else ' rename csv with device id
Name DirOldFile As DirNewFile
End If 'End If check NewFile exists
End If 'end if DirFile
End If 'end if Instr
Next 'next file in subfolder
Next 'next subfolder in folder
Application.ScreenUpdating = True
End Sub
Explanation / Answer
public void Process_Orders() {
Scripting.FileSystemObject Fso;
Fso = new Scripting.FileSystemObject();
Process_CSV_Files;
Fso;
"C:\";
Fso = null;
}
void Process_CSV_Files(Scripting.FileSystemObject Fso, string folderPath) {
Application.ScreenUpdating = false;
Scripting.Folder Folder;
Scripting.Folder Subfolder;
Scripting.File File;
Scripting.File File1;
long Msheet;
// count number of sheet for row copy
string MyPath;
string DirOldFile;
string DirNewFile;
string StrDate;
Workbook wb;
Worksheet datasheet;
string OldFileName;
string NewFileName;
Folder = Fso.GetFolder(folderPath);
// 'search files in subfolder
foreach (Subfolder in Folder.SubFolders) {
MyPath = (Subfolder.Path + "\");
foreach (File in Subfolder.Files) {
// each file in subfolder
OldFileName = File.Name;
// csv file name
DirOldFile = (MyPath + OldFileName);
// set old file path
if ((OldFileName.IndexOf(".csv") + 1)) {
if ((Dir(DirOldFile) != "")) {
// if file exist
// Open File.Path workbook
wb = Workbooks.Open(File.Path);
datasheet = wb.Worksheets(1);
// set worksheet1 as open
NewFileName = (datasheet.Range("B13") + ".csv");
DirNewFile = (MyPath + NewFileName);
// set new file path
wb.Close;
false;
if ((OldFileName == NewFileName)) {
// if new file name exist then rename date time and ID in csv
StrDate = Format(Now(), "yyyy-MM-dd hh:mm:ss");
Name;
(((MyPath)(DirOldFile))
+ (StrDate + ("_" + NewFileName)));
}
else {
// rename csv with device id
Name;
((DirNewFile)(DirOldFile));
}
// End If check NewFile exists
}
// end if DirFile
}
// end if Instr
}
// next file in subfolder
}
// next subfolder in folder
Application.ScreenUpdating = true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.