2013-10-22 1 views
1

받은 보고서를 다시 형식화하기위한 양식을 만들었으며 일부분을 자동화하는 데 문제가 있습니다. 모든 것은 정의 된 범위에서 셀 (첫 번째 열, 두 번째 행)의 길이로 설정하려는 마지막 가변 코드 길이를 정의하고 설정하기 전까지 작동합니다. 런타임 오류 424, "필요한 개체"가 나타납니다. 어떤 도움을 주셔서 감사합니다 !! - 그것은 독립 실행 형 객체 아니다VBA 개체 필요 오류

Private Sub CommandButton1_Click() 


Application.ScreenUpdating = False 

Dim rg As Range 
Dim rgg As Range 
Dim Addr1 As String 
Dim Addr2 As String 

'Get the address, or reference, from the RefEdit control. 
Addr1 = RefEdit1.Value 
Addr2 = RefEdit2.Value 

'Set the SelRange Range object to the range specified in the 
'RefEdit control. 
Set rg = Range(Addr1) 
Set rgg = Range(Addr2) 

ActiveWorkbook.Names.Add Name:="codes", RefersTo:=rgg 

'Infill 
'Copies the value from the row above into blank cells. 
Dim cel As Range, col As Range 
Set rg = Range(Addr1).Columns(1).Resize(, 2) 
On Error Resume Next 
For Each col In rg.Columns 
    Set rgg = Nothing 
    Set rgg = col.SpecialCells(xlCellTypeBlanks) 
    If Not rgg Is Nothing Then 
     rgg.FormulaR1C1 = "=R[-1]C"  'Blank cells set equal to value from row above 
     rgg.Formula = rgg.Value 'Optional: Replace the formulas with the values returned by the formulas 
    End If 
Next 
Set rgg = rg.Offset(1, 0).Resize(rg.Rows.Count - 1, rg.Columns.Count) 
For Each cel In rgg.Cells 
    If cel = "" Then cel.Value = cel.Offset(-1, 0).Value 
Next 
On Error GoTo 0 

'ColCDeleter 
Dim i As Long, n As Long 
Set rg = Intersect(ActiveSheet.UsedRange, Range(Addr1).Columns(3)) 
n = rg.Rows.Count 
For i = n To 1 Step -1 
If rg.Cells(i, 1) = "" Then rg.Cells(i, 1).EntireRow.Delete 
Next 

'insert corresponding values 
Dim codelength As Integer 
codelength = Len(codes.Cells(2, 1).Value) 
rg.Columns(2).EntireColumn.Insert 
rg.Columns(2).EntireColumn.Insert 
rg.Columns(2).EntireColumn.Insert 
rg.Columns(2).EntireColumn.Insert 
If codelength = 6 Then 
rg.Columns(2).FormulaR1C1 = "=VLOOKUP((MID(RC1,9,9)),codes,2,FALSE)" 
rg.Columns(3).FormulaR1C1 = "=VLOOKUP((MID(RC1,9,9)),codes,3,FALSE)" 
Else 
rg.Columns(2).FormulaR1C1 = "=VLOOKUP((MID(RC1,8,9)),codes,2,FALSE)" 
rg.Columns(3).FormulaR1C1 = "=VLOOKUP((MID(RC1,8,9)),codes,3,FALSE)" 
End If 
rg.Cells(1, 2).Value = "Plan" 
rg.Cells(1, 3).Value = "Status" 


'Unload the userform. 
Unload Me 



End Sub 
+5

rng. 때와 도움말'렌 (범위 ("코드"). 세포 (2, 1) .Value를 교체 rng Range 개체를 사용하는 것)' –

+0

정확히 내가 필요한 것 ... 감사합니다! – kcraig23

답변

1

가 먼저

Dim rng as Range 
Set rng = Range("A1:A10") 

ActiveWorkbook.Names.Add Name:="codes", RefersTo:=rng 

다음이 그냥 이름이됩니다 다음 구문을 사용하여 범위를 이름 : 여기

는 코드입니다. 그래서 당신이 얻고있는 오류는 정확히 무엇이 일어나고 있는지를 알려줍니다 -> Object required.

명명 된 범위를 참조하려면 큰 따옴표로 묶고 Range 개체의 매개 변수로 지정해야합니다. 따라서 Range("codes")rng 범위를 참조하는 Range 개체를 만듭니다.

대안은 이름을 생략하는 것은 단순히 Range("codes").