我有一张2张工作簿。 Sheet1命名为Form,Sheet2命名为Data。我试图在Sheet1上创建一个在Sheet2中创建新行的表单。我对VBA很新,我真的只是想通过Google自学。我的问题是,你怎么能引用一张没有活动的表格并让它按预期编纂信息而不用直观地切换标签?
非常感谢任何帮助。
非常尊重,
Adam Walker
答案 0 :(得分:2)
Option Explicit
Sub add_row()
Dim wb As Workbook, ws2 As Worksheet
Set wb = ThisWorkbook
Set ws2 = wb.Sheets("Sheet2")
ws2.Rows(1).Insert shift:=xlDown
End Sub
以上将在表2的顶部添加一个新行。 如果要在不同的工作表上执行多个操作,还可以使用with语句。
Option Explicit
Sub add_data()
Dim wb As Workbook, ws2 As Worksheet
Set wb = ThisWorkbook
Set ws2 = wb.Sheets("Sheet2")
With ws2
.Rows(1).Insert shift:=xlDown
.Cells(1, 1).Value2 = "Rows shifted down by 1!"
End With
End Sub
我希望这对你有帮助。
答案 1 :(得分:1)
一般来说,宏录制器会教你如何做事。但是,它确实编写了非常钝的代码,并且始终使用Active*
(Cell,Range,WorkSheet等)使用.select
,您已经发现它并不是非常有效或漂亮。
试试这个:
ActiveWorkbook.worksheets("Form").range("A1") = "This goes on the Form sheet"
ActiveWorkbook.worksheet("Data").range("A1") = "This goes on the Data sheet"
当然,这取决于ActiveWorkbook
,但你明白了。
答案 2 :(得分:1)
首先,因为你是vba的新手,你学会以正确的方式宣布所有人都能理解你在说什么,当你说形式变得模糊时,是用户形式还是公式可以你明白了吗?
关于对另一张未处于活动状态的工作表的引用,只需声明如下:
Workbooks("example.xlsm").Worksheets("Data").Range("A1").EntireRow.Insert
此声明将在第1行插入一个新行,通过该示例,您可以获得如何声明要使用的工作簿,工作表和范围
答案 3 :(得分:0)
过度杀伤示例:这是带有示例表单的Form
表单。已为Submit
按钮分配了宏SubmitForm
,这是在VBA中创建的过程(要遵循的代码)。
在查找特定单元格中的值时,命名范围在VBA中非常有用。通过将NamedRanges分配给表单的每个字段,我们只需使用Range("NamedRangeName").Value
First
字段分配了NamedRange FormFirstName
Last
字段分配了NamedRange FormLastName
Email
字段分配了NamedRange FormEmail
以下是我示例中的Data
表格。我们有一个标题行,然后是空白行。单击Submit
工作表上的Form
按钮,将在此处创建新记录(行)。
提交前的数据表点击
提交后点击数据表
这是与此示例一起提供的SubmitForm
过程代码。当用户通过单击按钮提交表单时,它会执行以下操作:
Form
NamedRanges
工作表字段中检索值
Data
表格
Public Sub SubmitForm()
Application.ScreenUpdating = False
Application.Calculation = XlCalculation.xlCalculationManual
Dim userFirstName As String
Dim userLastName As String
Dim userEmail As String
' get a reference to the Form worksheet
Dim formWorksheet As Worksheet
Set formWorksheet = ActiveWorkbook.Worksheets("Form")
' using the named ranges, retrieve the form field values
userFirstName = formWorksheet.Range("FormFirstName").Value
userLastName = formWorksheet.Range("FormLastName").Value
userEmail = formWorksheet.Range("FormEmail").Value
Dim dataWorksheet As Worksheet
Dim headerRow As Integer
Dim nextUserRow As Integer
Dim userIdColumn As Integer
Dim firstNameColumn As Integer
Dim lastNameColumn As Integer
Dim emailColumn As Integer
Dim createdDateColumn As Integer
' get a reference to the Data worksheet
Set dataWorksheet = ActiveWorkbook.Worksheets("Data")
headerRow = 1
' get the number of used rows (rows that contain data) on this worksheet,
' then add one to it. This will be the next empty row where our new
' record will live
nextUserRow = dataWorksheet.UsedRange.Rows.Count + 1
' set the columns for each field
userIdColumn = 1
firstNameColumn = 2
lastNameColumn = 3
emailColumn = 4
createdDateColumn = 5
' persist the data retrieved from the Form sheet to the Data sheet row
dataWorksheet.Cells(nextUserRow, userIdColumn).Value = (nextUserRow - headerRow)
dataWorksheet.Cells(nextUserRow, firstNameColumn).Value = userFirstName
dataWorksheet.Cells(nextUserRow, lastNameColumn).Value = userLastName
dataWorksheet.Cells(nextUserRow, emailColumn).Value = userEmail
dataWorksheet.Cells(nextUserRow, createdDateColumn).Value = Now
Application.Calculation = XlCalculation.xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Public Sub SubmitForm()
Application.ScreenUpdating = False
Application.Calculation = XlCalculation.xlCalculationManual
Dim userFirstName As String
Dim userLastName As String
Dim userEmail As String
' get a reference to the Form worksheet
Dim formWorksheet As Worksheet
Set formWorksheet = ActiveWorkbook.Worksheets("Form")
' using the named ranges, retrieve the form field values
userFirstName = formWorksheet.Range("FormFirstName").Value
userLastName = formWorksheet.Range("FormLastName").Value
userEmail = formWorksheet.Range("FormEmail").Value
Dim dataWorksheet As Worksheet
Dim headerRow As Integer
Dim nextUserRow As Integer
Dim userIdColumn As Integer
Dim firstNameColumn As Integer
Dim lastNameColumn As Integer
Dim emailColumn As Integer
Dim createdDateColumn As Integer
' get a reference to the Data worksheet
Set dataWorksheet = ActiveWorkbook.Worksheets("Data")
headerRow = 1
' get the number of used rows (rows that contain data) on this worksheet,
' then add one to it. This will be the next empty row where our new
' record will live
nextUserRow = dataWorksheet.UsedRange.Rows.Count + 1
' set the columns for each field
userIdColumn = 1
firstNameColumn = 2
lastNameColumn = 3
emailColumn = 4
createdDateColumn = 5
' persist the data retrieved from the Form sheet to the Data sheet row
dataWorksheet.Cells(nextUserRow, userIdColumn).Value = (nextUserRow - headerRow)
dataWorksheet.Cells(nextUserRow, firstNameColumn).Value = userFirstName
dataWorksheet.Cells(nextUserRow, lastNameColumn).Value = userLastName
dataWorksheet.Cells(nextUserRow, emailColumn).Value = userEmail
dataWorksheet.Cells(nextUserRow, createdDateColumn).Value = Now
Application.Calculation = XlCalculation.xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub