나는 이것을 약간만 테스트 했으므로 그다지 강력하지 않을 수도 있습니다.
참고이 하위 모듈은 시트 또는이 워크 북 모듈에 새로 추가하거나 기존 모듈에 배치 할 필요가 없습니다.
매크로이기 때문에 워크 시트에서 UDF로 호출 할 수 없습니다. 또한 인수가 있으므로 직접 호출 할 수 없습니다.
코드를 사용하려면 코드를 호출하기 위해 다른 하위를 만들거나 직접 실행 창에서 직접 호출해야합니다.
Sub RunCode()
Main "Name1", "Name2" ' you could run this line in the immediate/debug window
End Sub
하위 RunCode
는 통합 문서에 매크로 메뉴에서 사용할 수 있습니다. 당신이 함수에 약간 더 사용자 정의 인터페이스를 원한다면
Sub Main(ByVal Name1 As String, ByVal Name2 As String)
Dim Cell As Long
Dim Range1 As Range: Set Range1 = ThisWorkbook.Names(Name1).RefersToRange
Dim Range2 As Range: Set Range2 = ThisWorkbook.Names(Name2).RefersToRange
' check to make sure Name1 and Name2 are the same size
If Range1.Cells.Count = Range2.Cells.Count Then
If Range1.Rows.Count = Range2.Rows.Count Then
If Range1.Columns.Count = Range2.Columns.Count Then
' populate the cells with the formula
For Cell = 1 To Range1.Cells.Count
Range2.Cells(Cell).Formula = "=" & Range1.Worksheet.Name & "!" & Range1.Cells(Cell).Address
Next Cell
End If
End If
End If
End Sub
, 다음 코드는 도움이 될 것입니다. RunCode2
매크로를 실행하면 Main
Public Function nameExists(ByVal Name As String) As Boolean
Dim Result As Boolean: Result = fasle
Dim Item As Variant
For Each Item In ThisWorkbook.Names
If Item.Name = Name Then
Result = True
Exit For
End If
Next Item
nameExists = Result
End Function
Sub RunCode2()
Dim Response As Variant
Dim Name1, Name2 As String
Response = Application.InputBox(Prompt:="Name 1", Type:=2)
If VarType(Response) = vbBoolean Then
Debug.Print "RunCode2 - User Canceled Name 1 Selection"
Exit Sub
Else
If nameExists(Response) = False Then
MsgBox "Name [" & Response & "] Not Found", vbOKOnly
Exit Sub
Else
Name1 = Response
End If
End If
Response = Application.InputBox(Prompt:="Name 2", Type:=2)
If VarType(Response) = vbBoolean Then
Debug.Print "RunCode2 - User Canceled Name 2 Selection"
Exit Sub
Else
If nameExists(Response) = False Then
MsgBox "Name [" & Response & "] Not Found", vbOKOnly
Exit Sub
Else
Name2 = Response
End If
End If
Main Name1, Name2
End Sub
당신은 단지 두 번째에서 첫 번째 시트에 셀 참조를 추가, 수식 속성을 사용할 수 있습니다에 전달하는 두 개의 이름을 입력하라는 메시지를 표시합니다. 해당 범위의 각 셀을 반복해야 할 수도 있습니다. – NickSlash
그럼 왜'= Sheet2! A1'을 사용하여 ***** *****가 아니십니까? – user2140261
왜'myrange.formula = myrange.formula'를 사용하지 않습니까? http://msdn.microsoft.com/en-us/library/office/ff838835.aspx – glh