2013-09-02 2 views
1

내 VBA 프로그램에서 범위를 참조하는 데 문제가 있습니다.VBA Excel 2010 - 범위를 사용할 때 런타임 1004 오류

With Worksheets("Overall 6 mo") 
    .Columns("A:G").ColumnWidth = 13.57 
    .Range("A1:Z100").Rows.RowHeight = 15 

    .Columns("F:G").NumberFormat = "0.00%" 

    .Range("B3:G3") = Worksheets("TEMPLATE").Range("A3:F3").Value 
    .Range("F4:G100") = Worksheets("TEMPLATE").Range("E4:F100").Formula 

    .Range("A1").NumberFormat = "@" 
    .Range("A1") = .Name 
End With 

이 던질 것 "런타임 1004 응용 프로그램 정의 또는 개체 정의 오류"그럼 라인 3. 을 거쳐, 내가

.Range("A1:Z100").Rows.RowHeight = 15 
을 변경 : 다음 코드 조각은 내 원래의 코드를 보여줍니다

.Rows.RowHeight = 15 

에 요점은 내가 너무 변화가 내 프로그램을 다치게하지 않았다 (15)의 높이를 사용할 필요가 세포를 만드는 것이 었습니다. 그리고 이제는 허용되지만 다음 줄에서 같은 오류가 발생합니다. 여기서 다시 범위를 참조합니다. 그래서 왜 내가 .range를 사용하지 못하게 하는지를 알아 내려고 노력하고 있단 말인가? 아니면 적어도 어떻게 고칠 수 있습니까?

업데이트 : 내 통합 문서의 어디에서나 .Range 메서드를 사용할 수 없다는 것을 깨닫게되었습니다 (위의 인스턴스뿐 아니라). 무엇이 나를 사용할 수 없게 할 것입니다. 어디에서든지?

UPDATE2 : 더 이상 두 번째 줄에서 .Columns 메서드를 사용할 수 없게됩니다. 나는 아무것도하지 않았지만 그것을 몇 차례 밟았다. 이게 뭐가 잘못 됐니?

UPDATE3 : Excel을 다시 시작할 때 워크 시트 "Overall 6 mo"코드를 한 번 실행 한 다음 그 후에 매번 오류를 던지기 시작합니다. 나머지 시트에 대한 코드를 포함 시켰습니다.

Option Explicit 

Private Sub Worksheet_Activate() 

    Application.ScreenUpdating = False 

    Dim shIndex As Integer 
    Dim rowIndex As Integer 
    Dim myLastRow As Integer 
    Dim shLastRow As Integer 
    Dim col As Integer 

    myLastRow = Worksheets("Overall 6 mo").Cells(65536, 1).End(xlUp).Row 

    ' Format Worksheet 
    Sheets("Overall 6 mo").Cells.Clear 
    With Worksheets("Overall 6 mo") 
     .Columns.ColumnWidth = 13.57 
     .Rows.RowHeight = 15 

     .Columns("F:G").NumberFormat = "0.00%" 

     .Range("B3:G3") = Worksheets("TEMPLATE").Range("A3:F3").Value 
     .Range("F4:G100") = Worksheets("TEMPLATE").Range("E4:F100").Formula 

     .Range("A1").NumberFormat = "@" 
     .Range("A1") = .Name 
    End With 

    ' Clear current sheet data 
    myLastRow = Worksheets("Overall 6 mo").Cells(65536, 2).End(xlUp).Row 
    Worksheets("Overall 6 mo").Range(Cells(4, 1), Cells(myLastRow, 7)).Clear 

    ' Compile data from last six months and add to and display on "Overall 6 mo" sheet 
    For shIndex = Worksheets.Count - 5 To Worksheets.Count 
     Worksheets(shIndex).Activate 

     myLastRow = Worksheets("Overall 6 mo").Cells(65536, 2).End(xlUp).Row 
     shLastRow = Worksheets(shIndex).Cells(65536, 1).End(xlUp).Row 

     Worksheets("Overall 6 mo").Cells(myLastRow + 1, 1).Value _ 
      = MonthName(Month(CDate(Worksheets(shIndex).Name)), False) 

     Worksheets(shIndex).Range("A4:D" & shLastRow) _ 
      .Copy (Worksheets("Overall 6 mo").Cells(myLastRow + 1, 2)) 

    Next shIndex 

    ' Call UpdateChart to clear and re-add Quality and Cost charts to wks 
    Call UpdateCharts(Worksheets("Overall 6 mo").Index) 

    Worksheets("Overall 6 mo").Activate 
    Application.ScreenUpdating = True 

최종 하위

답변

0

당신이 할 수있는 행의 높이에 따라 변화 :

.Range("A1:Z100").RowHeight = 15 

그리고 당신은 범위 복사 방법을 사용할 수 있습니다

Worksheets("TEMPLATE").Range("A3:F3").Copy .Range("B3") 
Worksheets("TEMPLATE").Range("E4:F100").Copy .Range("F4") 

UPDATE :

Option Explicit 

Private Sub Worksheet_Activate()  
    Dim oSh As Worksheet 
    Dim shIndex As Long 
    Dim rowIndex As Long 
    Dim myLastRow As Long 
    Dim shLastRow As Long 

    Application.ScreenUpdating = False 
    Set oSh = ThisWorkbook.Worksheets("Overall 6 mo") 

    ' Format Worksheet 
    With oSh 
     .Cells.Clear 
     .Columns.ColumnWidth = 13.57 
     .Rows.RowHeight = 15 
     .Columns("F:G").NumberFormat = "0.00%" 
     .Range("A1").NumberFormat = "@" 
     .Range("A1") = .Name 

     .Range("B3:G3") = Worksheets("TEMPLATE").Range("A3:F3").Value 
     .Range("F4:G100") = Worksheets("TEMPLATE").Range("E4:F100").Formula 
    End With 

    ' Clear current sheet data 
    oSh.Range(oSh.Cells(4, 1), oSh.Cells(GetLastRow(oSh, 2), 7)).Clear 

    ' Compile data from last six months and add to and display on "Overall 6 mo" sheet 
    For shIndex = Worksheets.Count - 5 To Worksheets.Count 
     'Worksheets(shIndex).Activate 

     myLastRow = GetLastRow(oSh, 2) 
     shLastRow = GetLastRow(Worksheets(shIndex), 1) 

     oSh.Cells(myLastRow + 1, 1).Value = MonthName(Month(CDate(Worksheets(shIndex).Name)), False) 
     Worksheets(shIndex).Range("A4:D" & shLastRow).Copy oSh.Cells(myLastRow + 1, 2) 

    Next shIndex 

    ' Call UpdateChart to clear and re-add Quality and Cost charts to wks 
    Call UpdateCharts(oSh.Index) 

    oSh.Activate 
    Set oSh = Nothing 
    Application.ScreenUpdating = True 
End Sub 

Private Function GetLastRow(oSheet As Worksheet, lngColumn As Long) As Long 
    GetLastRow = oSheet.Cells(oSheet.UsedRange.SpecialCells(xlLastCell).Row + 1, lngColumn).End(xlUp).Row 
End Function 

인덱스 1 (또는 Worksheets.Count - 5보다 작은)과 같은 통합 문서에서 "템플리트"입니까? 나는이 서브를 For 루프에서 매번 실행할 필요가없는 것처럼 Worksheets(shIndex).Activate을 주석 처리했다.

+0

사용합니다. 여전히 오류가 발생합니다 – mmiles19

+0

"전체 6 개월"워크 시트가있는 통합 문서가 암호로 보호되어 있습니까/읽기 전용입니까? 그럴 경우 오류가 1004 발생합니다. Excel을 다시 시작하십시오. – PatricK

+0

아니요, 통합 문서가 보호되지 않고 Excel을 다시 시작하려고 시도했지만 계속 진행할 것입니다. – mmiles19

0

RowHeight행에 전체 행에 적용되며 행의 일부는 해당되지 않습니다.

그래서 내가 그 시도

.Range("A1:A100").EntireRow.RowHeight = 15 

또는

.Range("1:100").RowHeight = 15 
관련 문제