0
我的代码如下。我正在尝试根据Date
列中的唯一值创建新工作表。如果我没有正确格式化日期,Excel自动化错误 - 格式化日期
由于/
,我得到一个无效的表名错误。但是,当试图格式化日期以避免出现此错误时,出现自动化错误,并且宏终止于我在此发布的最后一行。
请帮忙。 :)所有的
Sub Analyze()
Dim DateColumn As Range
Dim theDate As Range
Dim theNextDate As Range
Dim theWorksheet As Worksheet
Dim thenewWorksheet As Worksheet
Const DateColumnCell As String = "Date"
Set theWorksheet = Sheets("Main")
Set DateColumn = theWorksheet.UsedRange.Find(DateColumnCell, , xlValues, xlWhole)
'Make sure you found something
If Not DateColumn Is Nothing Then
'Go through each cell in the column
For Each theDate In Intersect(DateColumn.EntireColumn, theWorksheet.UsedRange).Cells
'skip the header and empty cells
If Not IsEmpty(theDate.Value) And theDate.Address <> DateColumn.Address Then
'see if a sheet already exists
On Error Resume Next
Set thenewWorksheet = theWorksheet.Parent.Sheets(DateColumn.Value)
On Error GoTo 0
'if it doesn't exist, make it
If thenewWorksheet Is Nothing Then
Set thenewWorksheet = theWorksheet.Parent.Worksheets.Add
thenewWorksheet.Name = Format(theDate.Value, "Long Date")
运行此命令时'theDate'的值是多少? –
你永远不会检查'工作表(格式(theDate.Value,“长日期”))'是否存在。这可能是问题吗? –
我很难看到问题出在哪里,但我注意到你正在使用'DateColumn.Value'进行测试,它会返回一个未格式化的日期,例如2015年1月1日,但是你使用长格式命名表单,例如,“2015年1月1日星期一”。这些比较看起来像他们一直是错误的。现在是适应F8并在本地窗口中检查变量的好时机。 –