arina
December 14, 2022, 2:51am
1
Does anyone know how to do the following:
Rename current sheet
I tried using hotkey, but sometimes it works but sometimes it doesn’t. Also tried using Invoke VBA but not working.
Tried Invoke Code C# but got this error.
Tried Invoke Code VB.net based on this Renaming an Excel sheet - #3 by aksh1yadav but got compilation error.
Copy current sheet to the same workbook
Rename copied sheet
I use settings Windows (not Windows Legacy) with Classic Experience hence most custom activity cannot be used.
Thank you.
Yoichi
(Yoichi)
December 14, 2022, 4:01am
2
HI,
How about to use ClosedXml as the following?
using (var varWorkbook = new ClosedXML.Excel.XLWorkbook(workbookPath)){
var activeSheetname = varWorkbook.Worksheets.First(s=>s.TabActive).Name;
var srcSheet = varWorkbook.Worksheet(activeSheetname);
srcSheet.CopyTo(newSheetName);
varWorkbook.Save();
}
Main.xaml (6.9 KB)
Regards,
Anil_G
(Anil Gorthi)
December 14, 2022, 5:33am
3
Hi @arina
Did you try using copy sheet activity?m
You can copy the same sheet and rename as well
Cheers
Hi @arina
Please use the VB.NET code to achieve your goal.
Declare a variable for the current Excel workbook
Dim workbook As Excel.Workbook
Declare a variable for the current Excel sheet
Dim sheet As Excel.Worksheet
Get the current Excel workbook and sheet
workbook = Excel.ActiveWorkbook
sheet = Excel.ActiveSheet
Rename the current sheet
sheet.Name = “New Sheet Name”
Copy the current sheet to the same workbook
sheet.Copy(After:=workbook.Sheets(workbook.Sheets.Count))
Declare a variable for the new sheet
Dim newSheet As Excel.Worksheet
Get the new sheet that was just added to the workbook
newSheet = workbook.Sheets(workbook.Sheets.Count)
Rename the new sheet
newSheet.Name = “New Sheet Name 2”
Thanks!!!
arina
December 21, 2022, 6:14am
5
This method able to complete 2 (Copy current sheet to the same workbook) and 3 (Rename copied sheet) but not 1 (Rename current sheet).
Yoichi
(Yoichi)
December 21, 2022, 7:52am
6
HI,
Can you try yo add srcSheet.Name="newName"
as the following?
using (var varWorkbook = new ClosedXML.Excel.XLWorkbook(workbookPath)){
var activeSheetname = varWorkbook.Worksheets.First(s=>s.TabActive).Name;
var srcSheet = varWorkbook.Worksheet(activeSheetname);
srcSheet.CopyTo(newSheetName);
srcSheet.Name="NewName";
varWorkbook.Save();
}
Main(4).xaml (6.8 KB)
Regards,
arina
December 21, 2022, 8:06am
7
it works !! thank you also somehow adding those lines able to reduce my whole runtime to 40 seconds from 1 minutes 45 seconds
1 Like
system
(system)
Closed
December 24, 2022, 8:06am
8
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.