我如何编写一个VBA宏将用户选择的文件从封闭的工作簿导入到现有的电子表格中,然后覆盖原始文件? 我正在研究一些宏的工作,并且很难弄清楚如何正确编写代码以导入/导出/offrite excel电子表格文件。我想在顺序上发生的事情是...

问题描述 投票:0回答:0
4。)用户清除了参考表和用于更改它的计算表,我也已经完成并工作了。 我有以下宏来进口(步骤1)和出口(步骤3)到目前为止写的:

Import

Sub Import() Dim sourceWB As Workbook Dim sourceWS As Worksheet, destinationWS As Worksheet Dim dialog As fileDialog Dim fileName As String, wsName As String 'Create dialog for file selection. 'Allow user to select workbook containing original workbook file. Set dialog = Application.fileDialog(msoFileDialogFilePicker) With dialog .Title = "Select workbook to copy from" .Filters.Add "Excel Files", "*.xls; *.xlsx; *.xlsm", 1 .AllowMultiSelect = False If .Show <> -1 Then Exit Sub fileName = .SelectedItems(1) End With 'Open the workbook. Set sourceWB = Workbooks.Open(fileName) 'Prompt user to input the name of the worksheet. 'Match user input string to file names to select correct worksheet. wsName = Application.InputBox("Enter the name of worksheet to be imported: ", "Select Worksheet", Type:=2) On Error Resume Next Set sourceWS = sourceWB.Sheets(wsName) On Error GoTo 0 If sourceWS Is Nothing Then MsgBox "Worksheet not found. Check file names.", vbExclamation sourceWB.Close False Exit Sub End If 'Data sheet MUST be imported into the Reference Table sheet. Set destinationWS = ThisWorkbook.Sheets("Reference Table") 'Transfer worksheet into empty reference table, maintain original formatting. sourceWS.UsedRange.copy destinationWS.PasteSpecial Paste:=xlPasteAllUsingSourceTheme sourceWB.Close False End Sub

export

Sub Export() Dim destinationWB As Workbook Dim currentWS As Worksheet, destinationWS As Worksheet Dim dialog As fileDialog Dim fileName As String, wsName As String 'Reference Table sheet MUST be source for export. Set currentWS = ThisWorkbook.Sheets("Reference Table") 'Create dialog for file selection. 'Allow user to select workbook containing original file (will now be replaced with updated version). Set dialog = Application.fileDialog(msoFileDialogFilePicker) With dialog .Title = "Select workbook to overwrite to" .Filters.Add "Excel Files", "*.xls; *.xlsx; *.xlsm", 1 .AllowMultiSelect = False If .Show <> -1 Then Exit Sub fileName = .SelectedItems(1) End With 'Open the workbook. Set destinationWB = Workbooks.Open(fileName) 'Prompt user to input the name of the worksheet. 'Match user input string to file names to select correct worksheet. wsName = Application.InputBox("Enter the name of worksheet to overwrite:", "Select Worksheet", Type:=2) On Error Resume Next Set destinationWS = destinationWB.Sheets(wsName) On Error GoTo 0 If destinationWS Is Nothing Then MsgBox "Worksheet not found. Check file names.", vbExclamation destinationWB.Close False Exit Sub End If 'Overwrite original worksheet with updated version from score converter. currentWS.UsedRange.copy destinationWS.PasteSpecial Paste:=xlPasteAllUsingSourceTheme 'Save new version of file and close workbook. destinationWB.Save destinationWB.Close End Sub

,但是,我无法让宏执行。两者都会给出一个编译错误:“找不到命名参数”。我不知道这是否有帮助,但是当出现错误框时,每个子底部附近的糊状语句将部分突出显示。 我是新手VBA和编写宏,所以我一直偶然地将所有内容拼凑在一起,并且无法保证即使解决了编译错误后,代码也可以正常工作。帮助弄清楚导致错误的原因并确保整体宏(和/或任何更好的方法!)都将不胜感激。谢谢!

instead:

sourceWS.UsedRange.copy destinationWS.PasteSpecial Paste:=xlPasteAllUsingSourceTheme 使用: sourcews.usedrange.copy destinationws.cells.pastepecial糊状:=xlpasteallusingsourcetheme

	

excel vba spreadsheet worksheet
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.