使用一个大脚本来处理当前活动工作簿中的数据,然后将数据复制到新工作表中。我介绍了一个按钮,用于将同一工作表从当前工作簿复制到另一个工作簿。
此处D6包含目标文件的实际路径。当我尝试使用Windows +运行选项打开链接时,它是正确的并且正在打开
J12包含新工作表的名称。
“总计”代表目标工作簿中的第一张工作表,之后应该插入新工作簿。
char *a = ((ws < 1) ? "Calm" :
(ws >= 1) && ws < 4) ? "Light Air" :
(ws >= 4 && ws <= 27) ? "Breeze" :
(ws >= 28 && ws <= 47) ? "Gale":
(ws >= 48 && ws <= 63) ? "Storm" : "Hurricane");
printf("Wind level: %s\n", a);
sourceWbk.Sheets(shName).Copy After:= destWbk.Sheets(“ Total”)-此代码有时会给我“运行时错误'9':下标超出范围错误,而有时却显示成功msgbox。情况非常令人困惑。有人可以帮忙吗?
谢谢。
答案 0 :(得分:1)
最后,正常的宏录制有助于解决该问题。我只是记录了一个宏,并用动态值替换了静态值。甚至添加了一个额外的选项,供用户在特定工作表之后将文件移动到目标文件中。
Private Sub CopyToDest_Click()
Dim destWbk As Workbook
Dim wbkName As String
Dim shName As String, _
dstShName As String
Dim filePath As Variant, _
dstFileName As Variant
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'check neccessary cells have values
If Sheet29.Range("R12").Value = "" Then
MsgBox "Destination worksheet name has to be selected in Cell R12"
Exit Sub
End If
'Set source and dest sheets
wbkName = ThisWorkbook.Name ' Storing name of macro workbook
filePath = Sheet29.Range("D6").Value 'D6 holds the file location of destination sheet
dstFileName = Sheet29.Range("D7").Value 'D7 holds the file name extracted from D6
Set destWbk = Workbooks.Open(filePath) 'Open destination sheet
shName = Sheet29.Range("J12").Value 'Contains sheetname in macro workbook which has to be moved
dstShName = Sheet29.Range("R12").Value ' Contains sheetname of destination workbook after which the sheet has to be placed.
'Copy sheet from macro file to destination sheet
Windows(wbkName).Activate
Sheets(shName).Select
Sheets(shName).Move After:=Workbooks(dstFileName).Sheets(dstShName)
destWbk.Save
destWbk.Close
Windows(wbkName).Activate
ActiveWorkbook.Save
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox "Sheet Copied Successfully", vbInformation, "Success"
End Sub
答案 1 :(得分:0)
该错误表示该名称的工作表不存在。 “总计”或shName中的任何内容均无效。请记住,这里的名称是Excel中Tab的名称,而不是VBA中工作表对象的名称。工作表有两个名称。