我在我的智慧结束时来找你帮忙。
我要完成的是将所选目录中的多个工作簿中的单个工作表(唯一的工作表)重命名为Excel中的工作簿文件名。
我找到了可以在单个工作簿中使用的代码,但是我不知道如何让它在用户选择的目录中的多个工作簿上工作,或者从同一目录中的批处理/ vbs文件运行。< / p>
以下是我在单个工作簿上使用的代码:
WebDriverWait wait5s = new WebDriverWait(driver,5);
driver.get("https://pepsicopartners.com/");
WebElement lnk_goto_login = wait5s.until(ExpectedConditions.elementToBeClickable(By.id("loginDown")));
lnk_goto_login.click();
WebElement email = driver.findElement(By.id("j_username"));email.sendKeys("Abhishek@cognizant.com");
WebElement pwd = driver.findElement(By.id("j_password"));pwd.sendKeys("P@ssw0rd123");
WebElement accept_ToU = driver.findElement(By.id("terms"));accept_ToU.click();
WebElement btn_login = driver.findElement(By.xpath("//*[@id=\"loginForm\"]/button"));btn_login.click();
Thread.sleep(5000);
我希望能够对用户选择的文件夹中的所有文件或与批处理文件/ vbs可执行文件位于同一目录中的文件执行此操作,如果有办法通过运行批处理或vbs来执行此操作文件?
如果有问题或我遗失了什么,请告诉我,我会尽我所能回答。
非常感谢任何帮助。
答案 0 :(得分:1)
如果所选的任何工作簿上有超过1张工作表,则需要进行修改。如果只有一张,ActiveSheet
就足够了。
Sub RenameSheet()
Dim CurrentBook As Workbook
Dim ImportFiles As FileDialog
Dim FileCount As Long 'Count of workbooks selected
Dim wbName As String
'Open File Picker
Set ImportFiles = Application.FileDialog(msoFileDialogOpen)
With ImportFiles
.AllowMultiSelect = True
.Title = "Pick Files to Adjust"
.ButtonName = ""
.Filters.Clear
.Filters.Add ".xlsx files", "*.xlsx"
.Show
End With
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Cycle through books
For FileCount = 1 To ImportFiles.SelectedItems.Count
Set CurrentBook = Workbooks.Open(ImportFiles.SelectedItems(FileCount))
wbName = Replace(CurrentBook.Name, ".xlsx", "")
CurrentBook.Activate
ActiveSheet.Name = wbName
CurrentBook.Close True
Next FileCount
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:0)
我不得不调整几行但是效果很好。谢谢urdearboy。
Sub RenameSheet()
Dim CurrentBook As Workbook
Dim ImportFiles As FileDialog
Dim FileCount As Long 'Count of workbooks selected
Dim wbName As String
'Open File Picker
Set ImportFiles = Application.FileDialog(msoFileDialogOpen)
With ImportFiles
.AllowMultiSelect = True
.Title = "Pick Files to Adjust"
.ButtonName = ""
.Filters.Clear
.Filters.Add ".xlsx files", "*.xlsx"
.Show
End With
Application.DisplayAlerts = False
Application.DisplayAlerts = False
'Cycle through books
For FileCount = 1 To ImportFiles.SelectedItems.Count
Set CurrentBook = Workbooks.Open(ImportFiles.SelectedItems(FileCount))
wbName = Replace(CurrentBook.Name, ".xlsx", "") ' had to rework this line to the original
CurrentBook.Activate
ActiveSheet.Name = wbName
CurrentBook.Close True
Next FileCount ' had to change this to FileCount to remove the error "invalid next control variable"
Application.DisplayAlerts = True
Application.DisplayAlerts = True
End Sub