vba代码还很陌生,需要一些帮助...
我有一个VBA代码,可以复制模板工作表并将其重命名,我将新名称另存为公共字符串:
“ My_Tamplate”是我要复制的工作表
“ PublicStringName”是我用来将其重命名为
的公共字符串变量
我还在表单的其他地方使用了“ PublicStringName”,这就是为什么我需要它作为字符串。
Sheets("My_Tamplate").Copy After:=Worksheets(Sheets.Count)
ActiveSheet.name = PublicStringName
接下来,当我需要将使用表单收集的数据写到新创建的工作表上时。
接下来,我打开一个新表单,从用户收集数据到几个变量。
现在,我想将数据写入新创建的工作表(现在名为“ PublicStringName”)中
如果我将WS声明为工作表,它将不接受“ PublicStringName”,因为它是一个字符串(我认为)。
我不知道它将是什么工作表编号,因此无法通过(sheet1)来调用它。
非常感谢 爱丹
更新-1月19日
我不知道如何上传示例excel,所以:
excel有2个工作表,分别是:数据(sheet2)和Project_Template(Sheet1)
在sheet2 C3中,我有= MAX(B:B)
我有1个表单(UserForm1),它有一个多页对象
在第1页中,我有一个文本框txtProjectName和一个按钮cmdCreateProject
在第2页中,我有5个文本框,分别命名为:txtData1到txtData5和一个更新按钮btnUpdate
我尝试过PeterT解决方案(在代码中)
在此处附加有问题的代码:
Public ProjectName
Private Sub btnUpdate_Click()
Dim WS As Worksheet
Dim Addme As Range
Set WS = ThisWorksheet.Sheets(ProjectName)
Set Addme = WS.Cells(Rows.Count, 3).End(xlUp)
With WS
Addme.Offset(0, 1).Value = Me.txtData1
Addme.Offset(0, 2).Value = Me.txtData2
Addme.Offset(0, 3).Value = Me.txtData3
Addme.Offset(0, 4).Value = Me.txtData4
Addme.Offset(0, 5).Value = Me.txtData5
End With
MsgBox "Contact for Project:" & " " & ProjectName & ", " & "was successfully
added"
End Sub
Private Sub cmdCreateProject_Click()
Dim path As String
Dim mydir As String
Dim DataSh As Worksheet
Set DataSh = Sheet2
ProjectName = ""
'error handler
On Error GoTo errHandler:
ProjectName = Me.txtProjectName.Value
If Me.txtProjectName.Value = "" Then
MsgBox "Please enter a Project Name", vbOKOnly, "Project Name Error"
Exit Sub
End If
mydir = ThisWorkbook.path & "\" & ProjectName
If Dir(mydir, vbDirectory) = "" Then
MkDir mydir
'Copy tamplate sheet to for new Project
Sheets("Project_Template").Copy After:=Worksheets(Sheets.Count)
ActiveSheet.Name = ProjectName
Else
MsgBox "Directory already exsists"
Me.txtProjectName.Value = ""
Me.txtProjectName.SetFocus
ProjectName = ""
Exit Sub
End If
Set Addme = DataSh.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)
DataSh.Activate
DataSh.Select
With DataSh
'add the unique reference ID then all other values
Addme.Offset(0, -1) = DataSh.Range("C3").Value + 1
Addme.Value = Me.txtProjectName
End With
Me.MultiPage1.Pages(1).Enabled = True
Me.MultiPage1.Pages(1).Visible = True
Me.MultiPage1.Pages(0).Enabled = Fals
Me.MultiPage1.Pages(0).Visible = Fals
Exit Sub
errHandler:
'if error occurs then show me exactly where the error occurs
MsgBox "Error " & Err.Number & _
" (" & Err.Description & ")in procedure PcmdClear_Click of Form ProjectDB"
End Sub
现在,当我尝试更新新创建的工作表(以项目命名)时,出现错误:运行时错误'424':
必需的对象
在行上:
Set WS = ThisWorksheet.Sheets(ProjectName)
我认为Gary的学生答案缺少以下步骤:创建新工作表后,我开始收集数据
希望我说得更清楚
答案 0 :(得分:0)
这里是使用名称的一种方法:
Sub FreshSheet()
Dim PublicStringNAme As String
PublicStringNAme = "Ellan"
Sheets("My_Tamplate").Copy After:=Worksheets(Sheets.Count)
ActiveSheet.Name = PublicStringNAme
'
'
'
'
Sheets(PublicStringNAme).Range("A1") = 1
End Sub
如果要使变量“更全局”,则:
Public PublicStringName As String
Sub FreshSheet()
PublicStringName = "Ellan"
Sheets("My_Tamplate").Copy After:=Worksheets(Sheets.Count)
ActiveSheet.Name = PublicStringName
'
'
'
'
Sheets(PublicStringName).Range("A1") = 1
End Sub
请参阅: