我的vba代码遇到了很多麻烦。我创建了一个将在文件打开时执行的子程序。但是,我不断收到运行时错误,说我有应用程序定义或对象定义的问题。当“With Cells(13,dateFinder.Column)”块没有伴随下面的“With Cells(17,dateFinder.Column)”块时,此代码有效。
以下是代码:
Sub equityRaiseOpen()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Worksheets("PRESENTATION")
Set ws2 = Worksheets(7)
ws2.Activate
Dim dateRange As Range
ws2.Unprotect
Dim dte As Date
'this block finds current quarter ending date
If Application.WorksheetFunction.RoundUp(Month(Now()) / 3, 0) = 1 Then
dte = DateValue("3/31/" & Year(Now()))
ElseIf Application.WorksheetFunction.RoundUp(Month(Now()) / 3, 0) = 2 Then
dte = DateValue("6/30/" & Year(Now()))
ElseIf Application.WorksheetFunction.RoundUp(Month(Now()) / 3, 0) = 3 Then
dte = DateValue("9/30/" & Year(Now()))
ElseIf Application.WorksheetFunction.RoundUp(Month(Now()) / 3, 0) = 4 Then
dte = DateValue("12/31/" & Year(Now()))
End If
Set dateRange = Range("FFO___AFFO_SUMMARY")
Dim iterator As Range
Dim colNum As Integer
'this block finds the column number for the current quarter ending date
For Each iterator In dateRange
If iterator = dte Then
colNum = iterator.Column
End If
Next
ws2.Range("B1") = dte
Dim dateFinder As Range
Set dateFinder = ws2.Range("B1")
Dim i As Integer
'Call putDates
i = 9
'ws2.Unprotect
Dim j As Integer
Set dateFinder = ws2.Range("B1")
For j = 1 To i
Cells(12, dateFinder.Column) = 0.4
'create data validiation for debt source
Debug.Print Cells(13, dateFinder.Column).Address
With Cells(13, dateFinder.Column)
.Interior.Color = RGB(255, 255, 255)
.Locked = False
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
With .Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=DebtList"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
.Value = "Revolver"
End With
With Cells(17, dateFinder.Column)
.Interior.Color = RGB(255, 255, 255)
.Locked = False
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
With .Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="ATM,Common, Preferred"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
.Value = "ATM"
End With
Set dateFinder = dateFinder.Offset(0, 2)
Next j
'ws2.Protect
End Sub