我在同一个文件夹中有大约300个工作簿,并且我想将单元格B19-B49从1个工作簿复制到同一文件夹中的300个工作簿的其余部分。这是否可能以一种聪明的方式,所以我不需要复制粘贴通过300个不同的文件?将单元格从一个工作簿复制到多个工作簿excel
问候
我在同一个文件夹中有大约300个工作簿,并且我想将单元格B19-B49从1个工作簿复制到同一文件夹中的300个工作簿的其余部分。这是否可能以一种聪明的方式,所以我不需要复制粘贴通过300个不同的文件?将单元格从一个工作簿复制到多个工作簿excel
问候
使用简单的PowerShell脚本将值从源文件复制到目标目录中的文件。只需用值替换顶部的5个变量:
$sourceFile = "c:\tmp\source.xlsx"
$destinationDirectory = "c:\tmp"
$sheetName = "Sheet1"
$rangeToCopyStart = "B19"
$rangeToCopyEnd = "B49"
#----------------------------------------------
# Open Excel source file
#----------------------------------------------
$excelApplication = New-Object -comobject Excel.Application
$excelWorkbook = $excelApplication.Workbooks.Open($sourceFile, 2, $True)
$excelWorksheet = $excelWorkbook.Worksheets.Item($sheetName)
#----------------------------------------------
# Copy the cell value
#----------------------------------------------
"Value to copy:" + $excelWorksheet.Range($rangeToCopyStart, $rangeToCopyEnd).Value2;
"From:" + $sourceFile;
$excelWorksheet.Range($rangeToCopyStart, $rangeToCopyEnd).Copy() | out-null;
$excelWorkbook.Close();
#----------------------------------------------
# Get all Excel files from destination directory
#----------------------------------------------
$Files = Get-ChildItem $destinationDirectory -Filter *.xlsx
Foreach ($Item in $Files) {
$destinationFile = $Item.FullName
#----------------------------------------------
# Skip the source file if it's in the same directory
#----------------------------------------------
If ($sourceFile.ToLower() -eq $destinationFile.ToLower()) { continue; }
$destinationWorkbook = $excelApplication.Workbooks.Open($destinationFile, 2, $False)
$destinationWorkSheet = $destinationWorkbook.Worksheets.Item($sheetName)
#----------------------------------------------
# Paste the value into the destination file
#----------------------------------------------
$destinationWorkSheet.Paste($destinationWorkSheet.Range($rangeToCopyStart, $rangeToCopyEnd));
$destinationWorkbook.Close($True); #save changes and close
"Copied to: " + $destinationFile;
}
#----------------------------------------------
# Quit Excel and release the object
#----------------------------------------------
$excelApplication.Quit();
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelApplication) | out-null;
林全新的PowerShell,所以请原谅我的无知。 运行脚本时,它粘贴到文件中的所有文件都是单元格B19中的“Set-ExecutionPolicy RemoteSigned”。 这是为什么?难道我做错了什么? – Kasper
为避免执行策略出现问题,请在PowerShell ISE而不是PowerShell命令行中运行脚本。您可以在命令行exe(例如C:\ Windows \ System32 \ WindowsPowerShell \ v1.0)的相同位置找到它,然后在ISE中单击“新建”,粘贴脚本,更改您的值并按F5键运行脚本 –
完美地工作,感谢您的帮助。 – Kasper
是的,你可以写在源工作簿中遍历文件的目标文件夹中,打开它(与Workbooks.Open方法)VBA模块,增加所需的细胞并保存。
“嗨下面为您的要求的代码...请注意,您可以更改myextension按照您的要求..
小组Button2_Click( )
“目的:通过用户选定的文件夹中的所有工作簿和循环执行类似的任务
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode: 我路径= mypath中 如果mypath中= “”,则跳转ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xlsx"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Change First Worksheet's Background Fill Blue
wb.Worksheets(1).Range("A1:Z1").Interior.Color = RGB(51, 98, 174)
'Save and Close Workbook
wb.Close SaveChanges:=True
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings: “重置宏优化设置 Application.EnableEvents =真 Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating =真 Application.DisplayAlerts =真
末次
http://www.xlorate.com/vba-examples.html#Loop%20Through%20Folder – Davesexcel