![](/img/trans.png)
[英]VBA excel trying to create a macro that imports data from file then if the data is equal to a specific value put one cell into a sheet on the new file
[英]How using the Excel macro VBA to create a new file where numbers as values from the sheet in the initial file?
我有 3 張紙 A、B 和 C。 A 具有指向 B 的超鏈接,A 具有單元格,其中公式從工作表 C 中獲取值。 我想創建一個新文件,其中只有 2 張 A 和 B。因此,我認為宏代碼應該包含告訴將 A(從初始文件)復制粘貼到 A(在新文件中)的代碼。 不幸的是,我是 VBA 的新手,無法弄清楚要使用哪個代碼。 你能幫忙嗎?
我使用了下面的代碼,但是由於公式取決於 C 我得到了錯誤。
Sub newfile()
Sheets(Array("A", "B")).Select
Sheets(Array("A", "B")).Copy
ActiveWorkbook.SaveAs Filename:="C:XX.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub
您需要將工作表 B 的計算值轉換為實際值。 我認為用新名稱保存當前工作簿,用當前名稱重新保存,打開新工作簿,轉換值然后刪除工作表 C 很容易。 我也包含了一個錯誤處理程序:
Sub e()
Dim wb As Workbook ' declare a variable for the workbook
On Error GoTo ErrHandle ' add an error handling step
Application.DisplayAlerts = False ' turn off save-over alerts
ThisWorkbook.SaveAs "NewBook.xlsm" ' save current book with new name
ThisWorkbook.SaveAs "CurrentWorkBookName.xlsm" ' resave current book to preserve name
Set wb = Application.Workbooks.Open("NewBook.xlsm") ' open the new book
wb.Sheets("B").UsedRange.Value = wb.Sheets("B").UsedRange.Value ' convert B calc values to actual values
wb.Sheets("C").Delete ' delete sheet C
wb.Close True ' close the new book and save the changes
ErrHandle:
If Err.Number <> 0 Then MsgBox Err.Description 'display error if there was one
Application.DisplayAlerts = True ' turn on alerts again
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.