生成临时工作表,其中第一行是从现有工作表复制的 [英] Generating a temporary worksheet with the first row copied from existing worksheet
问题描述
您好,我想知道如何创建临时工作表(用于显示临时订单),该工作表将在使用"取消"按钮关闭用户窗体后删除。如何将第一行WorkSheet2复制到名为
" Temp"的临时工作表上,或者每次生成时都显示第一行?
Hello, I would like to know how I can create a temporary worksheet (for displaying temporary orders) that will delete after closing the userform with a Cancel button. How can I copy the first row of WorkSheet2 onto the temporary worksheet named "Temp", or have the first row displayed every time it is generated?
谢谢!
推荐答案
尝试以下代码。将所有代码复制到Userform代码模块中。
Try the following code. Copy all of the code into the Userform code module.
请注意, Dim语句必须位于Userform代码模块的顶部。
Note that the Dim statement must be at the top of the Userform code module.
代码测试工作表Temp的存在,如果不存在则创建它。如果它已经存在则清除它。
The code tests for existence of the worksheet Temp and if does not exist then it creates it. If it already exists then it clears it.
"WorkSheet2"的第1行然后复制到工作表Temp
Row 1 of "WorkSheet2" is then copied to the worksheet Temp
取消按钮的代码删除Temp工作表。在示例中,"取消"按钮的名称为btnCancel。
The code for the Cancel button delete the Temp worksheet. In the example, the name of the Cancel button is btnCancel.
将以下所有代码复制到Userform模块。
Copy all the following code to the Userform Module.
Dim wsTemp As Worksheet '在Userform代码模块顶部
Dim wsTemp As Worksheet 'At top of Userform code module
Private Sub UserForm_Initialize()
'测试Temp工作表是否已存在
On Error Resume Next
设置wsTemp =工作表(" Temp")
On Error GoTo 0
如果wsTemp什么都没有那么 '我没有什么就不存在
'Temp不存在所以添加表单为
'跟随行编辑"工作表2"到您的工作表名称
设置wsTemp = Sheets.Add(After:= Worksheets(Sheets.Count))
wsTemp.Name =" Temp" '重新命名添加的工作表
否则
'工作表临时表已经存在,因此工作表清晰明了
wsTemp.Cells.ClearContents
结束如果是
'将第一行WorkSheet2复制到添加的工作表Temp。
工作表("WorkSheet2")。行(1)。复制目的地:= wsTemp.Cells(1,1)
End Sub
Private Sub UserForm_Initialize()
'Test if the worksheet Temp already exists
On Error Resume Next
Set wsTemp = Worksheets("Temp")
On Error GoTo 0
If wsTemp Is Nothing Then 'Id Nothing then does not already exist
'Temp does not exist so add the sheet
'Following line edit "WorkSheet2" to your worksheet name
Set wsTemp = Sheets.Add(After:=Worksheets(Sheets.Count))
wsTemp.Name = "Temp" 'Rename the added sheet
Else
'Worksheet Temp already exists so clear the worksheet
wsTemp.Cells.ClearContents
End If
'Copy 1st row of WorkSheet2 to the added sheet Temp.
Worksheets("WorkSheet2").Rows(1).Copy Destination:=wsTemp.Cells(1, 1)
End Sub
Private Sub btnCancel_Click()
'这个子被名为btnCancel的按钮调用
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
卸载我
结束子
Private Sub btnCancel_Click()
'this sub is called by button named btnCancel
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
Unload Me
End Sub
这篇关于生成临时工作表,其中第一行是从现有工作表复制的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!