现在我终于设法将所有代码放在一起,现在一切看起来都正确并且没有给出任何语法错误。但是,代码完成后,程序根本不执行任何操作。
我在VBA中有一个申请,要求提供开始日期(月份)和结束日期,然后在每个电子表格中输出每月的月历。
例如:如果用户选择九月(8)作为开始日期而十二月作为结束日期(11),则应输出每个电子表格中九月到十二月之间的所有月份。
以下是此
的代码 Private Sub UserForm_Initialize()
start_date.AddItem ("January"), 0
start_date.AddItem ("February"), 1
start_date.AddItem ("March"), 2
start_date.AddItem ("April"), 3
start_date.AddItem ("May"), 4
start_date.AddItem ("June"), 5
start_date.AddItem ("July"), 6
start_date.AddItem ("August"), 7
start_date.AddItem ("September"), 8
start_date.AddItem ("October"), 9
start_date.AddItem ("November"), 10
start_date.AddItem ("December"), 11
end_date.AddItem ("January"), 0
end_date.AddItem ("February"), 1
end_date.AddItem ("March"), 2
end_date.AddItem ("April"), 3
end_date.AddItem ("May"), 4
end_date.AddItem ("June"), 5
end_date.AddItem ("July"), 6
end_date.AddItem ("August"), 7
end_date.AddItem ("September"), 8
end_date.AddItem ("October"), 9
end_date.AddItem ("November"), 10
end_date.AddItem ("December"), 11
End Sub
Private Sub newProjectNext1_Click()
Dim strArrayOne(11) As String
Dim wsArrayOne(11) As Worksheet
strArrayOne(0) = "January"
strArrayOne(1) = "February"
strArrayOne(2) = "March"
strArrayOne(3) = "April"
strArrayOne(4) = "May"
strArrayOne(5) = "June"
strArrayOne(6) = "July"
strArrayOne(7) = "August"
strArrayOne(8) = "September"
strArrayOne(9) = "October"
strArrayOne(10) = "November"
strArrayOne(11) = "December"
Dim ArrayTwo(11) As String
ArrayTwo(0) = "January 2015"
ArrayTwo(1) = "February 2015"
ArrayTwo(2) = "March 2015"
ArrayTwo(3) = "April 2015"
ArrayTwo(4) = "May 2015"
ArrayTwo(5) = "June 2015"
ArrayTwo(6) = "July 2015"
ArrayTwo(7) = "August 2015"
ArrayTwo(8) = "September 2015"
ArrayTwo(9) = "October 2015"
ArrayTwo(10) = "November 2015"
ArrayTwo(11) = "December 2015"
Do Until start_date.ListIndex <= end_date.ListIndex
Set wsArrayOne(start_date.ListIndex) = Sheets.Add
Sheets.Add.Name = strArrayOne(start_date.ListIndex)
Application.ScreenUpdating = False
Range("a1:g14").Clear
MyInput = ArrayTwo(start_date.ListIndex)
If MyInput = "" Then Exit Sub
StartDay = DateValue(MyInput)
If Day(StartDay) <> 1 Then
StartDay = DateValue(Month(StartDay) & "/1/" & _
Year(StartDay))
End If
Range("a1").NumberFormat = ArrayTwo(start_date.ListIndex)
With Range("a1:g1")
.HorizontalAlignment = xlCenterAcrossSelection
.VerticalAlignment = xlCenter
.Font.Size = 18
.Font.Bold = True
.RowHeight = 35
End With
With Range("a2:g2")
.ColumnWidth = 11
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Orientation = xlHorizontal
.Font.Size = 12
.Font.Bold = True
.RowHeight = 20
End With
Range("a2") = "Sunday"
Range("b2") = "Monday"
Range("c2") = "Tuesday"
Range("d2") = "Wednesday"
Range("e2") = "Thursday"
Range("f2") = "Friday"
Range("g2") = "Saturday"
With Range("a3:g8")
.HorizontalAlignment = xlRight
.VerticalAlignment = xlTop
.Font.Size = 18
.Font.Bold = True
.RowHeight = 21
End With
Range("a1").Value = Application.Text(MyInput, "mmmm yyyy")
DayofWeek = Weekday(StartDay)
CurYear = Year(StartDay)
CurMonth = Month(StartDay)
FinalDay = DateSerial(CurYear, CurMonth + 1, 1)
Select Case DayofWeek
Case 1
Range("a3").Value = 1
Case 2
Range("b3").Value = 1
Case 3
Range("c3").Value = 1
Case 4
Range("d3").Value = 1
Case 5
Range("e3").Value = 1
Case 6
Range("f3").Value = 1
Case 7
Range("g3").Value = 1
End Select
For Each cell In Range("a3:g8")
RowCell = cell.Row
ColCell = cell.Column
If cell.Column = 1 And cell.Row = 3 Then
ElseIf cell.Column <> 1 Then
If cell.Offset(0, -1).Value >= 1 Then
cell.Value = cell.Offset(0, -1).Value + 1
If cell.Value > (FinalDay - StartDay) Then
cell.Value = ""
Exit For
End If
End If
ElseIf cell.Row > 3 And cell.Column = 1 Then
cell.Value = cell.Offset(-1, 6).Value + 1
If cell.Value > (FinalDay - StartDay) Then
cell.Value = ""
Exit For
End If
End If
Next
For x = 0 To 5
Range("A4").Offset(x * 2, 0).EntireRow.Insert
With Range("A4:G4").Offset(x * 2, 0)
.RowHeight = 65
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.WrapText = True
.Font.Size = 10
.Font.Bold = False
.Locked = False
End With
With Range("A3").Offset(x * 2, 0).Resize(2, _
7).Borders(xlLeft)
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
With Range("A3").Offset(x * 2, 0).Resize(2, _
7).Borders(xlRight)
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
Range("A3").Offset(x * 2, 0).Resize(2, 7).BorderAround _
Weight:=xlThick, ColorIndex:=xlAutomatic
Next
If Range("A13").Value = "" Then Range("A13").Offset(0, 0) _
.Resize(2, 8).EntireRow.Delete
ActiveWindow.DisplayGridlines = False
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, _
Scenarios:=True
ActiveWindow.WindowState = xlMaximized
ActiveWindow.ScrollRow = 1
Application.ScreenUpdating = True
Exit Sub
start_date.ListIndex = start_date.ListIndex + 1
Loop
End Sub
我尝试检查start_date.ListIndex
和end_date.ListIndex
的数字是否从组合框输出正确的选定值,并且效果很好。
它不会输出任何内容,甚至是错误。
答案 0 :(得分:0)
需要考虑的几件事情:
1 - 此行正在停止执行。如果开始日期小于结束日期,则此条件为真,并且永远不会进入循环。
Do Until start_date.ListIndex <= end_date.ListIndex
我认为你应该使用Do ..而不是
Do While start_date.ListIndex <= end_date.ListIndex
2 - 这一行给出了错误,我想你想要设置Value,而不是NumberFormat
Range("a1").NumberFormat = ArrayTwo(start_date.ListIndex)
3 - 此行在一次迭代后退出循环,将其注释掉
Exit Sub
4 - 您每次使用这些行添加两张纸,因为您不使用该纸张作为参考(您应该),您可以注释掉第一行
Set wsArrayOne(start_date.ListIndex) = Sheets.Add
Sheets.Add.Name = strArrayOne(start_date.ListIndex)
更好的方法是声明一个工作表变量,然后在整个过程中使用它作为您的参考(如果您不将单元格操作调用绑定到工作表,它们将发生在当时活动工作表的任何内容中,在这种情况下可以,但如果你一次使用多张纸,会让你遇到麻烦。这样会更好
Dim ws as worksheet
Set ws=Sheets.Add
ws.name=strArrayOne(start_date.ListIndex)
'then bind your cell manuipulations to the worksheet variable like this
ws.Range("a1:g14").Clear
要考虑的其他事项是当用户做出意外情况时,比如5月到2月的选择......循环不会运行,但是你可以给用户一条消息,说开始日期必须小于结束日期。另外,养成为代码添加错误处理的习惯,如果编码不仅仅是学校项目,那么错误处理是必不可少的,最好尽早提取!祝你好运!