我编辑了这篇文章,表明我已经对此做了一些研究。下面的一些代码是在“记录宏”会话期间捕获的。我只是无法弄清楚如何将它记录的工作簿名称更改为变量。非常感谢任何帮助!
我正在尝试在两个工作表之间复制和粘贴一些数据。一个是模板,并且将始终具有相同的名称,另一个取决于我打开哪个。我现在的代码一直在抛出一个错误。我目前的代码如下!任何帮助将不胜感激!我想在变量中手动打开随机工作簿存储它的名称。然后让宏打开模板文件,在templateFile中创建一个新选项卡(称为neededInfo),切换回手动打开的文件,选择范围(“A1:B4”),然后将其复制到templateFile中的新工作表。如果有更好的方法,请告诉我!我对此非常陌生!
Sub Macro3()
Dim templateFile As Workbook ' template macro workbook; will always have same filename
Dim workingFile As String ' current working CSCC Quote download
Dim workingSheet As Worksheet ' only sheet in CSCC Quote
Dim tempSheet As Worksheet ' created sheet in CSCC Quote to hold temp data
Dim fileName As String
Dim wrkBook As Workbook
Dim nRow As Long
Dim neededInfo As Long
Dim neededRange As Range
Application.ScreenUpdating = False
' Set template file name
Set templateFile = "20161115 SMARTnet Template.xlsx"
Set templateFile = ActiveWorkbook
templateFile.Active
Set tempSheet = Worksheets.Add(after:=ActiveSheet)
tempSheet.Name = "neededInfo"
workingFile = ActiveSheet.Name
Range("A1:B4").Select
Selection.Copy
templateSheet.Activate
Range("A5").Select
ActiveSheet.Paste
workingFile.Activate
End Sub
我的解决方案:
Sub Macro3()
Dim tPlate As Workbook ' template macro
Dim wrkngFile As Workbook ' current working CSCC Quote download
Dim wrkngSheet As Worksheet ' only sheet in CSCC Quote
Dim tSheet As Worksheet ' created sheet in template file to hold temp data
Dim fileName As String
Dim tPath As String, tFile As String ' template path location and template file
Dim nRow As Long
Dim neededInfo As Long
Dim neededRange As Range
Application.ScreenUpdating = False
tPath = "C:\Users\tcoplien\Desktop\SMARTnet\"
tFile = tPath & "20161115 SMARTnet Template.xlsx"
Set wrkngFile = ActiveWorkbook
Range("A1:B4").Select
Selection.Copy
' Open template file and save name as variable
Set tPlate = Workbooks.Open(tFile)
Set tSheet = Worksheets.Add(After:=ActiveSheet)
tSheet.Name = "neededInfo"
tSheet.Range("A5").PasteSpecial xlPasteValues
wrkngFile.Activate
Range("A1").Select
End Sub
答案 0 :(得分:2)
您的代码相当接近,但存在一些问题。首先,不要创建一个以其类型为名称的变量。将Dim workBook As Workbook
更改为Dim bk As Workbook
。
接下来,您有声明Set templateFile = "20161115 SMARTnet Template.xlsx"
。这实际上是在尝试将工作簿设置为字符串。而是将其更改为Set templateFile = Workbooks("20161115 SMARTnet Template")
。或者更好的是将其更改为:
Dim tempStr as String
tempStr = "20161115 SMARTnet Template"
Set templatefile = Workbooks(tempStr)
我不完全确定你要用接下来的几行代码完成什么。您似乎在向templatefile
工作簿添加新标签。只需使用此Set tempSheet = templatefile.Worksheets.Add(after:=ActiveSheet)
,然后将此新工作表重命名为“neededInfo”,而不是激活工作簿。
接下来,将workingFile
变量设置为活动工作表,在本例中为neededInfo
工作表。我假设这不是你的意图。在添加新工作表之前,您需要定义workingSheet
变量。
新代码可能如下所示:
Sub Macro3()
Dim templatefile As Workbook
Dim workingSheet As Worksheet ' only sheet in CSCC Quote
Dim tempSheet As Worksheet ' created sheet in CSCC Quote to hold temp data
Dim tempStr As String
Application.ScreenUpdating = False
' Set template file name
tempStr = "20161115 SMARTnet Template"
Set templatefile = Workbooks(tempStr)
Set workingSheet = ActiveSheet
Set tempSheet = Worksheets.Add(after:=ActiveSheet)
tempSheet.Name = "neededInfo"
workingSheet.Range("A1:B4").Copy
tempSheet.Range("A5").PasteSpecial xlPasteValues
Application.ScreenUpdating = True
End Sub
我认为这是你想要完成的事情。如果没有,这应该是一个良好的开端。
祝你好运!