[英]How to open Excel file saved at specific location,Create a new tab as of last tab in a sheet using VBA
I am not from software background and trying to write a macro to avoid repeated work that causes errors.我不是来自软件背景并试图编写宏以避免导致错误的重复工作。 Please feel free to suggest things that will make it better.I tried to pull pieces from different program from this site and make it work.
请随意提出可以使它变得更好的东西。我试图从这个站点的不同程序中提取片段并使其工作。
Thank you in advance.先感谢您。
Here is what I am trying to do.这就是我想要做的。 a) I am trying to open Excel file(lets say file 2) saved at specific location from current file (Say file 1).
a)我正在尝试打开保存在当前文件(比如文件 1)特定位置的 Excel 文件(比如文件 2)。 Works
作品
b) Create a new tab in file 2 same as last tab in that file, Works b) 在文件 2 中创建一个与该文件中的最后一个选项卡相同的新选项卡, Works
c) Re-name the created tab using 2 different cell from file 1.(Auto Rename would be good based on file 1 cells but I could only manage pop up that ask me to enter but doesn't rename the file). c) 使用文件 1 中的 2 个不同单元格重命名创建的选项卡。(基于文件 1 单元格自动重命名会很好,但我只能管理要求我输入但不重命名文件的弹出窗口)。 Doesn't Work/partially work
不工作/部分工作
d) Pop up to input Name and 2 more input in different cells(I can only get Your name how can I get 3 input in 3 different cells) Doesn't Work/partially work d) 弹出以在不同的单元格中输入名称和另外 2 个输入(我只能得到你的名字,我怎样才能在 3 个不同的单元格中获得 3 个输入)不起作用/部分工作
e) Copy 2 cells from file 1 and copy in file 2 newly created tab. e) 从文件 1 中复制 2 个单元格并复制到文件 2 新创建的选项卡中。 Doesn't work
不起作用
Here is the code I tried to write.这是我尝试编写的代码。
Sub Filling_List()
Dim sPath As String, sFile As String, wb As Workbook, i As Integer
'Application.ScreenUpdating = False
sPath = "C:\Users\aricsonp\Desktop\Filling list macro\"
sFile = sPath & "ArF Filling List.xlsm"
Set wb = Workbooks.Open(sFile)
ActiveSheet.Copy After:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Name = InputBox("New Name:")
If sName = "" Then Exit Sub
ActiveSheet.cell(3, "E") = InputBox("Your Name:")
' With ActiveSheet.Sheets("ArF Filling List (7)")
'.Range("B03").Value = uploader.Sheets("Que & Tsc Cal").Range("B02").Value
' .Range("B05").Value = uploader.Sheets("Que & Tsc Cal").Range("B01").Value
' End With
'Application.ScreenUpdating = True
End Sub
Here are some pointers.这里有一些提示。
1) Put Option Explicit at top so syntax and declarations are checked. 1) 将Option Explicit放在顶部,以便检查语法和声明。 This would force you to declare
sName
and uploader
as well as set their values.这将强制您声明
sName
和uploader
并设置它们的值。 i
is also declared but not assigned. i
也已声明但未分配。
2) Your code, as is, did rename the sheet . 2)您的代码按原样重命名了sheet 。 You assigned directly to the newly added sheet from the inputbox rather than storing in a variable.
您直接从输入框中分配给新添加的工作表,而不是存储在变量中。
Worksheets(Worksheets.Count).Name = InputBox("New Name:")
Assuming the variable sName
was actually to hold this and that you want to grab this value from 2 cells in the workbook containing the code (ie ThisWorkbook ) you are running:假设变量
sName
实际上是用来保存这个的,并且你想从包含你正在运行的代码(即ThisWorkbook )的工作簿中的 2 个单元格中获取这个值:
sName = ThisWorkbook.Worksheets("Sheet1").Range("A1") & ThisWorkbook.Worksheets("Sheet1").Range("B1")
You would probably want to declare and assign ThisWorkbook
and worksheets you reference within as variables.您可能希望将
ThisWorkbook
和您在其中引用的工作表声明和分配为变量。
eg例如
Dim wb1 as Workbook
Dim ws1 as Worksheet
Set wb1 = ThisWorkbook
Set ws1 = ThisWorkbook.Worksheets("Sheet1") 'change as appropriate
3) Avoid mixing Sheets and Worksheets collections. 3) 避免混合Sheets和Worksheets集合。 I prefer Worksheets collections unless you have Chart sheets present.
除非你有图表表,否则我更喜欢工作表集合。
4) In most cases you will want to compare against vbNullstring rather than an empty string literal (""). 4) 在大多数情况下,您希望与vbNullstring进行比较,而不是与空字符串文字 ("") 进行比较。 It is faster to assign, uses less memory etc.
分配速度更快,使用更少的内存等。
If sName = vbNullString Then Exit Sub
5) Assigning more values to cells; 5)为单元格分配更多的值; add more inputboxes AND use
Cells
not Cell eg添加更多输入框并使用
Cells
而不是单元格,例如
ActiveSheet.Cells(4, "E") = InputBox("Your Age:")
6) Adding more cells' values into the newly opened workbook, newly added sheet; 6) 在新打开的工作簿、新添加的工作表中添加更多单元格的值; use your
wb
and sName
variables to ensure correct targeting:使用
wb
和sName
变量来确保正确定位:
With wb.Worksheets(sName)
7) You may want to declare each variable at the top on its own line ie avoid multiple declarations on one line. 7) 您可能希望在自己的行的顶部声明每个变量,即避免在一行上进行多个声明。 Makes it easier to debug and spot any implicit variants.
使调试和发现任何隐式变体变得更容易。
So, you might have something like the following:因此,您可能会遇到以下情况:
Option Explicit
Public Sub Filling_List()
Dim sPath As String
Dim sFile As String
Dim wb As Workbook
' Dim i As Integer ''not used
Dim sName As String 'add sName declaration
'Add declaration for uploader variable and set its value
Dim wb1 as Workbook
Dim ws1 as Worksheet
Set wb1 = ThisWorkbook
Set ws1 = ThisWorkbook.Worksheets("Sheet1")
Application.ScreenUpdating = False
sPath = "C:\Users\aricsonp\Desktop\Filling list macro\"
sFile = sPath & "ArF Filling List.xlsm"
Set wb = Workbooks.Open(sFile)
ActiveSheet.Copy After:=Worksheets(Worksheets.Count)
'Worksheets(Worksheets.Count).Name = InputBox("New Name:")
sName = ws1.Range("A1") & ws1.Range("B1") 'assign value from two cells
ActiveSheet.Name = sName
If sName = vbNullString Then Exit Sub 'compare against vbNullstring not empty string literal
With wb.Worksheets(sName)
.Cells(3, "E") = InputBox("Your Name:")
.Cells(4, "E") = InputBox("Your Age:")
.Cells(5, "E") = InputBox("Your Occupation:")
.Range("B03") = uploader.Worksheets("Que & Tsc Cal").Range("B02").Value2
.Range("B05") = uploader.Worksheets("Que & Tsc Cal").Range("B01").Value2
End With
Application.ScreenUpdating = True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.