需要对象但无法找到丢失的位置

时间:2014-08-20 20:20:59

标签: excel-vba compiler-errors vba excel

我试图让这个vba代码运行,但我得到了一个"对象需要"错误。当我调试时,没有突出显示。由于这个原因,我没有看到对象丢失的位置。我正在运行Excel 2010.提前感谢您的帮助。

Private Sub CommandButton1_Click()

    Dim i As Long
    Dim sh As Object
    Dim Yr, Yr_P
    Dim Qinput As String
    Dim Sheet6, Sheet1, Sheet2, Sheet3 As Object

'get new year
    Qinput = InputBox(Prompt:="Enter the year you want to create data for")
      If Qinput = vbNullString Then
      Exit Sub
      Else
      End If

'identify year
    Yr = Right(Left(Qinput, 4), 2)
    Yr_P = Yr - 1

'change new on tabnames
    Sheet6.Name = "FY" & Yr & "GM Forecast (AMSG) Total"
    Sheet1.Name = "FY" & Yr & "GM Forecast(US)"
    Sheet2.Name = "FY" & Yr & "GM Forecast(MCU)"
    Sheet3.Name = "FY" & Yr & "GM Forecast (PDSN)"

'update headers
    With Sheet6
        Cells(1, 6) = "FY" & Yr_P
        Cells(1, 15) = "FY" & Yr
        Cells(1, 24) = "FY" & Yr
        Cells(1, 33) = "FY" & Yr
        Cells(1, 42) = "FY" & Yr

        Cells(2, 3) = "FY" & Yr_P & "Q4"
        Cells(2, 12) = "FY" & Yr & "Q1"
        Cells(2, 21) = "FY" & Yr & "Q2"
        Cells(2, 30) = "FY" & Yr & "Q3"
        Cells(2, 39) = "FY" & Yr & "Q4"
        Cells(2, 48) = "FY" & Yr & "(to date)"
        Cells(2, 49) = "FY" & Yr & "FCST"
        Cells(2, 51) = "Normalized FY" & Yr & "FCST"

        Cells(3, 3) = "Jul," & Yr_P
        Cells(3, 4) = "Aug," & Yr_P
        Cells(3, 5) = "Sep," & Yr
        Cells(3, 12) = "Oct," & Yr
        Cells(3, 13) = "Nov," & Yr
        Cells(3, 14) = "Dec,) & Yr"
        Cells(3, 21) = "Jan," & Yr
        Cells(3, 22) = "Feb," & Yr
        Cells(3, 23) = "Mar," & Yr
        Cells(3, 30) = "Apr," & Yr
        Cells(3, 31) = "May," & Yr
        Cells(3, 32) = "Jun," & Yr
        Cells(3, 39) = "Jul," & Yr
        Cells(3, 40) = "Aug," & Yr
        Cells(3, 41) = "Sep," & Yr

    End With

    With Sheet1
        Cells(1, 6) = "FY" & Yr_P
        Cells(1, 15) = "FY" & Yr
        Cells(1, 24) = "FY" & Yr
        Cells(1, 33) = "FY" & Yr
        Cells(1, 42) = "FY" & Yr

        Cells(2, 3) = "FY" & Yr_P & "Q4"
        Cells(2, 12) = "FY" & Yr & "Q1"
        Cells(2, 21) = "FY" & Yr & "Q2"
        Cells(2, 30) = "FY" & Yr & "Q3"
        Cells(2, 39) = "FY" & Yr & "Q4"
        Cells(2, 48) = "FY" & Yr & "(to date)"
        Cells(2, 49) = "FY" & Yr & "FCST"
        Cells(2, 51) = "Normalized FY" & Yr & "FCST"

        Cells(3, 3) = "Jul," & Yr_P
        Cells(3, 4) = "Aug," & Yr_P
        Cells(3, 5) = "Sep," & Yr
        Cells(3, 12) = "Oct," & Yr
        Cells(3, 13) = "Nov," & Yr
        Cells(3, 14) = "Dec,) & Yr"
        Cells(3, 21) = "Jan," & Yr
        Cells(3, 22) = "Feb," & Yr
        Cells(3, 23) = "Mar," & Yr
        Cells(3, 30) = "Apr," & Yr
        Cells(3, 31) = "May," & Yr
        Cells(3, 32) = "Jun," & Yr
        Cells(3, 39) = "Jul," & Yr
        Cells(3, 40) = "Aug," & Yr
        Cells(3, 41) = "Sep," & Yr

'copy all Q4 data from columns AM-AO and paste into C-E the delete/clear the old data
       Sheet1.Cells("AM4:AO13").Value = Sheet1.Cells("C4:E13").Value
       Sheet1.Cells("AM4:AO13").ClearContents
       Sheet1.Cells("AM18:AO32").Value = Sheet1("C18:E32").Value
       Sheet1.Cells("AM18:AO32").ClearContents
       Sheet1.Cells("AM44:AO47").Value = Sheet1.Cells("C44:E47").Value
       Sheet1.Cells("AM44:AO47").ClearContents
       Sheet1.Cells("AS50").Copy Destination:=Sheet1.Cells("H50")
       Sheet1.Cells("AS50,Q50,Z50,AI50,AX50").ClearContents



    End With

    End Sub

3 个答案:

答案 0 :(得分:1)

有关How to use Sheets by Index Number

的信息

使用Worksheets(6)代替sheet6

您已声明Dim Sheet6, Sheet1, Sheet2, Sheet3 As Object但应用程序不知道它们是否表示工作簿中的Sheets或其他内容

删除声明并将所有对工作表的引用更改为Worksheets(Index-No)

例如With Sheet1将成为With Worksheets(1)

P.S确保您的工作簿中有6张

完整解决方案:

 Private Sub CommandButton1_Click()
 Dim i As Long
    Dim sh As Object
    Dim Yr, Yr_P
    Dim Qinput As String   
'get new year
    Qinput = InputBox(Prompt:="Enter the year you want to create data for")
      If Qinput = vbNullString Then
      Exit Sub
      Else
      End If

'identify year
    Yr = Right(Left(Qinput, 4), 2)
    Yr_P = Yr - 1

'change new on tabnames
    Worksheets(6).Name = "FY" & Yr & "GM Forecast (AMSG) Total"
    Worksheets(1).Name = "FY" & Yr & "GM Forecast(US)"
    Worksheets(2).Name = "FY" & Yr & "GM Forecast(MCU)"
    Worksheets(3).Name = "FY" & Yr & "GM Forecast (PDSN)"

'update headers
    With Worksheets(6)
        Cells(1, 6) = "FY" & Yr_P
        Cells(1, 15) = "FY" & Yr
        Cells(1, 24) = "FY" & Yr
        Cells(1, 33) = "FY" & Yr
        Cells(1, 42) = "FY" & Yr

        Cells(2, 3) = "FY" & Yr_P & "Q4"
        Cells(2, 12) = "FY" & Yr & "Q1"
        Cells(2, 21) = "FY" & Yr & "Q2"
        Cells(2, 30) = "FY" & Yr & "Q3"
        Cells(2, 39) = "FY" & Yr & "Q4"
        Cells(2, 48) = "FY" & Yr & "(to date)"
        Cells(2, 49) = "FY" & Yr & "FCST"
        Cells(2, 51) = "Normalized FY" & Yr & "FCST"

        Cells(3, 3) = "Jul," & Yr_P
        Cells(3, 4) = "Aug," & Yr_P
        Cells(3, 5) = "Sep," & Yr
        Cells(3, 12) = "Oct," & Yr
        Cells(3, 13) = "Nov," & Yr
        Cells(3, 14) = "Dec,) & Yr"
        Cells(3, 21) = "Jan," & Yr
        Cells(3, 22) = "Feb," & Yr
        Cells(3, 23) = "Mar," & Yr
        Cells(3, 30) = "Apr," & Yr
        Cells(3, 31) = "May," & Yr
        Cells(3, 32) = "Jun," & Yr
        Cells(3, 39) = "Jul," & Yr
        Cells(3, 40) = "Aug," & Yr
        Cells(3, 41) = "Sep," & Yr

    End With

    With Worksheets(1)
        Cells(1, 6) = "FY" & Yr_P
        Cells(1, 15) = "FY" & Yr
        Cells(1, 24) = "FY" & Yr
        Cells(1, 33) = "FY" & Yr
        Cells(1, 42) = "FY" & Yr

        Cells(2, 3) = "FY" & Yr_P & "Q4"
        Cells(2, 12) = "FY" & Yr & "Q1"
        Cells(2, 21) = "FY" & Yr & "Q2"
        Cells(2, 30) = "FY" & Yr & "Q3"
        Cells(2, 39) = "FY" & Yr & "Q4"
        Cells(2, 48) = "FY" & Yr & "(to date)"
        Cells(2, 49) = "FY" & Yr & "FCST"
        Cells(2, 51) = "Normalized FY" & Yr & "FCST"

        Cells(3, 3) = "Jul," & Yr_P
        Cells(3, 4) = "Aug," & Yr_P
        Cells(3, 5) = "Sep," & Yr
        Cells(3, 12) = "Oct," & Yr
        Cells(3, 13) = "Nov," & Yr
        Cells(3, 14) = "Dec,) & Yr"
        Cells(3, 21) = "Jan," & Yr
        Cells(3, 22) = "Feb," & Yr
        Cells(3, 23) = "Mar," & Yr
        Cells(3, 30) = "Apr," & Yr
        Cells(3, 31) = "May," & Yr
        Cells(3, 32) = "Jun," & Yr
        Cells(3, 39) = "Jul," & Yr
        Cells(3, 40) = "Aug," & Yr
        Cells(3, 41) = "Sep," & Yr

'copy all Q4 data from columns AM-AO and paste into C-E the delete/clear the old data
       Worksheets(1).Range("C4:E13").Value = Worksheets(1).Range("AM4:AO13")
       Worksheets(1).Range("AM4:AO13").ClearContents
       Worksheets(1).Range("C18:E32").Value = Worksheets(1).Range("AM18:AO32").Value
       Worksheets(1).Range("AM18:AO32").ClearContents
       Worksheets(1).Range("C44:E47").Value = Worksheets(1).Range("AM44:AO47").Value
       Worksheets(1).Range("AM44:AO47").ClearContents
       Worksheets(1).Range("AS50").Copy Destination:=Worksheets(1).Range("H50")
       Worksheets(1).Range("AS50,Q50,Z50,AI50,AX50").ClearContents  

    End With
End Sub

答案 1 :(得分:0)

Worksheets("Sheet6").Name = "FY" & Yr & "GM Forecast (AMSG) Total"
Worksheets("Sheet1").Name = "FY" & Yr & "GM Forecast(US)"
Worksheets("Sheet2").Name = "FY" & Yr & "GM Forecast(MCU)"
Worksheets("Sheet3").Name= "FY" & Yr & "GM Forecast (PDSN)"

解决了一些问题。底部代码块仍然存在问题。

编辑:思维上限提醒我,我做了Dim Sheet6,Sheet1,Sheet2,Sheet3 As worksheet

编辑2:这不是正确切换工作表,更改......

编辑3:

我测试了它。在问题中评论的12月错字仍然存在并仍然提供了一个不稳定的输出,但以下代码成功复制到副本部分。

Private Sub CommandButton1_Click()

Dim i As Long
Dim sh As Object
Dim Yr As Integer
Dim Yr_P As Integer
Dim Qinput As String
Dim Sheet6, Sheet1, Sheet2, Sheet3  As Worksheet






'get new year
 Qinput = InputBox(Prompt:="Enter the year you want to create data for")
  If Qinput = vbNullString Then
  Exit Sub
  Else
  End If

'identify year
Yr = Right(Left(Qinput, 4), 2)
Yr_P = Yr - 1



'change new on tabnames
Worksheets("Sheet6").Name = "FY" & Yr & "GM Forecast (AMSG) Total"
Worksheets("Sheet1").Name = "FY" & Yr & "GM Forecast(US)"
Worksheets("Sheet2").Name = "FY" & Yr & "GM Forecast(MCU)"
Worksheets("Sheet3").Name = "FY" & Yr & "GM Forecast (PDSN)"

'update headers
 ActiveWorkbook.Sheets("FY" & Yr & "GM Forecast (AMSG) Total").Activate
    Cells(1, 6) = "FY" & Yr_P
    Cells(1, 15) = "FY" & Yr
    Cells(1, 24) = "FY" & Yr
    Cells(1, 33) = "FY" & Yr
    Cells(1, 42) = "FY" & Yr

    Cells(2, 3) = "FY" & Yr_P & "Q4"
    Cells(2, 12) = "FY" & Yr & "Q1"
    Cells(2, 21) = "FY" & Yr & "Q2"
    Cells(2, 30) = "FY" & Yr & "Q3"
    Cells(2, 39) = "FY" & Yr & "Q4"
    Cells(2, 48) = "FY" & Yr & "(to date)"
    Cells(2, 49) = "FY" & Yr & "FCST"
    Cells(2, 51) = "Normalized FY" & Yr & "FCST"

    Cells(3, 3) = "Jul," & Yr_P
    Cells(3, 4) = "Aug," & Yr_P
    Cells(3, 5) = "Sep," & Yr
    Cells(3, 12) = "Oct," & Yr
    Cells(3, 13) = "Nov," & Yr
    Cells(3, 14) = "Dec,) & Yr"
    Cells(3, 21) = "Jan," & Yr
    Cells(3, 22) = "Feb," & Yr
    Cells(3, 23) = "Mar," & Yr
    Cells(3, 30) = "Apr," & Yr
    Cells(3, 31) = "May," & Yr
    Cells(3, 32) = "Jun," & Yr
    Cells(3, 39) = "Jul," & Yr
    Cells(3, 40) = "Aug," & Yr
    Cells(3, 41) = "Sep," & Yr



 ActiveWorkbook.Sheets("FY" & Yr & "GM Forecast(US)").Activate
    Cells(1, 6) = "FY" & Yr_P
    Cells(1, 15) = "FY" & Yr
    Cells(1, 24) = "FY" & Yr
    Cells(1, 33) = "FY" & Yr
    Cells(1, 42) = "FY" & Yr

    Cells(2, 3) = "FY" & Yr_P & "Q4"
    Cells(2, 12) = "FY" & Yr & "Q1"
    Cells(2, 21) = "FY" & Yr & "Q2"
    Cells(2, 30) = "FY" & Yr & "Q3"
    Cells(2, 39) = "FY" & Yr & "Q4"
    Cells(2, 48) = "FY" & Yr & "(to date)"
    Cells(2, 49) = "FY" & Yr & "FCST"
    Cells(2, 51) = "Normalized FY" & Yr & "FCST"

    Cells(3, 3) = "Jul," & Yr_P
    Cells(3, 4) = "Aug," & Yr_P
    Cells(3, 5) = "Sep," & Yr
    Cells(3, 12) = "Oct," & Yr
    Cells(3, 13) = "Nov," & Yr
    Cells(3, 14) = "Dec,) & Yr"
    Cells(3, 21) = "Jan," & Yr
    Cells(3, 22) = "Feb," & Yr
    Cells(3, 23) = "Mar," & Yr
    Cells(3, 30) = "Apr," & Yr
    Cells(3, 31) = "May," & Yr
    Cells(3, 32) = "Jun," & Yr
    Cells(3, 39) = "Jul," & Yr
    Cells(3, 40) = "Aug," & Yr
    Cells(3, 41) = "Sep," & Yr

'copy all Q4 data from columns AM-AO and paste into C-E the delete/clear the old data
   Sheet1.Cells("AM4:AO13").Value = Sheet1.Cells("C4:E13").Value
   Sheet1.Cells("AM4:AO13").ClearContents
   Sheet1.Cells("AM18:AO32").Value = Sheet1("C18:E32").Value
   Sheet1.Cells("AM18:AO32").ClearContents
   Sheet1.Cells("AM44:AO47").Value = Sheet1.Cells("C44:E47").Value
   Sheet1.Cells("AM44:AO47").ClearContents
   Sheet1.Cells("AS50").Copy Destination:=Sheet1.Cells("H50")
   Sheet1.Cells("AS50,Q50,Z50,AI50,AX50").ClearContents





End Sub

答案 2 :(得分:0)

有几种方法可以引用工作表对象:

  • 通过传递其索引 - 即表示工作表标签中的工作表顺序的数字 - 或名称到工作表方法

    worksheets(index)
    worksheets("Name")
    
  • 按其代号

    codeName
    

确保不要混淆

相关问题