[英]Copy cells from one workbook into multiple workbooks excel
I have around 300 workbooks in the same folder, and i want to copy cell B19-B49 from 1 workbook into the rest of the 300 workbooks in the same folder. 我在同一文件夹中大约有300个工作簿,我想将单元格B19-B49从1个工作簿复制到同一文件夹中其余300个工作簿中。 Is that possible in some clever way so I dont have to copy-paste through 300 different files?
是否可以通过某种巧妙的方式实现,所以我不必通过300个不同的文件进行复制粘贴?
Best regards 最好的祝福
Use a simple PowerShell script to copy the value from the source file to the files in the destination directory. 使用简单的PowerShell脚本将值从源文件复制到目标目录中的文件。 Just replace the 5 variables at the top with your values:
只需将顶部的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;
'Hi Below the code for your requirement...please note you can change the myextension as per your requirement.. “嗨,在您的要求代码下方...请注意,您可以根据自己的要求更改myextension。
Sub Button2_Click() 子Button2_Click()
'PURPOSE: To loop through all workbooks in a user selected folder and perform a similar task 目的:遍历用户所选文件夹中的所有工作簿并执行类似任务
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 If myPath = "" Then GoTo ResetSettings NextCode:myPath = 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: 'Reset Macro Optimization Settings Application.EnableEvents = True Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True Application.DisplayAlerts = True ResetSettings:'重置宏优化设置Application.EnableEvents = True Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True Application.DisplayAlerts = True
End Sub 结束子
是的,您可以在源工作簿中编写vba模块,该模块循环遍历目标文件夹中的文件,然后使用Workbooks.Open方法打开它,添加所需的单元格并保存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.