简体   繁体   English

如何打开保存在特定位置的 Excel 文件,使用 VBA 从工作表中的最后一个选项卡创建一个新选项卡

[英]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.这将强制您声明sNameuploader并设置它们的值。 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) 避免混合SheetsWorksheets集合。 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:使用wbsName变量来确保正确定位:

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.

相关问题 在Excel中打开VBA的多选项卡表单到特定的选项卡 - Open a VBA Multi Tab Form in Excel to a specific tab Excel VBA用来自最后一张表的数据创建一个新表 - Excel VBA Create a new sheet with data from the last sheet 如何使用 Excel 宏 VBA 创建一个新文件,其中数字作为初始文件中工作表的值? - How using the Excel macro VBA to create a new file where numbers as values from the sheet in the initial file? 在Excel中创建新标签页 - Create New Tab in Excel 将 Excel 工作表另存为制表符分隔文本文件的 VBA 代码 - VBA code to save Excel sheet as tab-delimited text file 如何使用excel VBA将excel选项卡的格式复制并粘贴到除所选工作表以外的其他选项卡上 - How do I copy and paste formatting of excel tab to other tabs except a selected sheet using excel VBA 如何在 Excel vba 中使用 ADO 在 .xlsx 文件中创建新工作表/表格 - How to create a new sheet/table in an .xlsx file using ADO in excel vba 需要VBA代码将Excel工作表列转换为新Excel工作表中的选项卡 - Need a VBA code to convert Excel sheet columns into tab in new Excel sheet 打开特定的excel文件/表格以获取特定的内容单元格位置 - Open specific excel file/sheet to get a specific content cell location Excel VBA Store / Dim工作表标签颜色 - Excel VBA Store/Dim sheet tab color
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM