2
我有以下代碼。Excel - 打開工作簿名稱
非常簡單,它要求用戶選擇多個Excel工作簿,然後將數據從這些工作簿複製並粘貼到當前工作簿。
1. 我想添加功能,而不是用戶選擇Excel工作簿。 Excel工作簿將被選中,因爲他們的名字被列在當前的Excel表格中。
例如 - 選擇名稱在A1:A5中列出的指定文件夾中的Excel工作簿。
- 我想在將數據複製到當前工作簿之前對數據執行自動處理。
例如,如果工作簿名稱= 100.xlsx然後通過15
乘以選擇參閱我當前的代碼
Sub SUM_BalanceSheet()
Application.ScreenUpdating = False
'FileNames is array of file names, file is for loop, wb is for the open file within loop
'PasteSheet is the sheet where we'll paste all this information
'lastCol will find the last column of PasteSheet, where we want to paste our values
Dim FileNames
Dim file
Dim wb As Workbook
Dim PasteSheet As Worksheet
Dim lastCol As Long
Set PasteSheet = ActiveSheet
lastCol = PasteSheet.Cells(1, Columns.Count).End(xlToLeft).Column
'Build the array of FileNames to pull data from
FileNames = Application.GetOpenFilename(filefilter:="Excel Files (*.xlsx), *.xlsx", MultiSelect:=True)
'If user clicks cancel, exit sub rather than throw an error
If Not IsArray(FileNames) Then Exit Sub
'Loop through selected files, put file name in row 1, paste P18:P22 as values
'below each file's filename. Paste in successive columns
For Each file In FileNames
Set wb = Workbooks.Open(file, UpdateLinks:=0)
PasteSheet.Cells(1, lastCol + 1) = wb.Name
wb.Sheets("Page 1").Range("L14:L98").Copy
PasteSheet.Cells(2, lastCol + 1).PasteSpecial Paste:=xlPasteValues
wb.Close SaveChanges:=False
lastCol = lastCol + 1
Next
'If it was a blank sheet then data will start pasting in column B, and we don't
'want a blank column A, so delete it if it's blank
If Cells(1, 1) = "" Then Cells(1, 1).EntireColumn.Delete shift:=xlLeft
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
謝謝尤金,我該如何倍增範圍? – RalphDylan
@RalphDylan有許多方法,你可以迭代目標範圍內的每個單元格,並指定新的值'rng.Value = rng.Value * Factor',或者你可以在表單中的某個臨時單元格中放置一個因子(Set rngFactor = SomeSheet.Cells(1,1).Value'),複製它,然後將其作爲乘法值插入到目標範圍中:'rngTarget.PasteSpecial Paste:= xlPasteValues,Operation:= xlMultiply,SkipBlanks:= False,Transpose := FALSE' – Eugene