2013-08-13 1 views
0

Michael Alexander 및 John Walkenbach의 101 Ready-touse 매크로에서 얻은 코드를 사용하고 있습니다.범위 유형 값을 문자열 유형 변수로 전달하는 방법

이 코드에서는 범위 값을 코드에 지정합니다. 나는 사용자가 값을 선택할 수 있기를 원한다. 사용자가 성공적으로 범위 값을 입력하게 한 다음 오류가 발생합니다. 문제를 디버깅하는 데 도움이되도록 짧은 매크로를 작성하여 수행하려는 작업을 테스트했습니다. 하지만 메시지 상자에 정보를 전달하는 것처럼 보이지 않아서 작동한다는 것을 알 수 있습니다 (사용자가 입력란에 주소가 표시되는 범위가 선택 되었기 때문에 나타납니다). 알아낼 수 없습니다. 사용자가 입력 한 RANGE 유형을 가져 와서 내 메시지에 표시하거나 범위로 사용하려면 STRING 값으로 변환하는 방법. 이제 초보자로서, 나는이 작은 테스트가 디버깅을 배우는 데 도움이되는 원래의 문제보다 더 중요하다고 생각합니다.

는 (나는() UserRange는 String 형 예 UserRange = Rng1 인 경우와 Rng1 입력 범위 인 범위의 값을 할당 변형 AS로 AS 범위를 제조하고 StringVariable을 만들려고 노력 등 일부 다른 변화를 시도했다 :

다음은

Sub SelectRange() Dim Rng1 As Range Set Rng1 = Application.InputBox("select cell", Type:=8) MsgBox ("You selected " & Rng1 & "as the range") End Sub 

여기

가 원래의 코드였다 내 코드입니다 0
Sub Macro101() 

' I've already changed Step#4 to read the worksheet name instead of C20 
' I'm now trying to change Step#3 to let the user select the range but 
' I'm having problems using the input from the user becuase I've made that 
' Variable as Range (Not shown here). 


'Step 1: Declare your variables 
    Dim pp As PowerPoint.Application 
    Dim PPPres As PowerPoint.Presentation 
    Dim PPSlide As PowerPoint.Slide 
    Dim xlwksht As Excel.Worksheet 
    Dim MyRange As String 
    Dim MyTitle As String 
    Dim Slidecount As Long 


'Step 2: Open PowerPoint, add a new presentation and make visible 
    Set pp = New PowerPoint.Application 
    Set PPPres = pp.Presentations.Add 
    pp.Visible = True 


'Step 3: Set the ranges for your data and title 
    MyRange = "A1:J29" 


'Step 4: Start the loop through each worksheet 
    For Each xlwksht In ActiveWorkbook.Worksheets 
    xlwksht.Select 
    Application.Wait (Now + TimeValue("0:00:1")) 
    MyTitle = xlwksht.Range("C20").Value 


'Step 5: Copy the range as picture 
    xlwksht.Range(MyRange).CopyPicture _ 
    Appearance:=xlScreen, Format:=xlPicture 


'Step 6: Count slides and add new slide as next available slide number 
    Slidecount = PPPres.Slides.Count 
    Set PPSlide = PPPres.Slides.Add(Slidecount + 1, ppLayoutTitleOnly) 
    PPSlide.Select 


'Step 7: Paste the picture and adjust its position 
    PPSlide.Shapes.Paste.Select 
    pp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True 
    pp.ActiveWindow.Selection.ShapeRange.Top = 100 


'Step 8: Add the title to the slide then move to next worksheet 
    PPSlide.Shapes.Title.TextFrame.TextRange.Text = MyTitle 
    Next xlwksht 


'Step 9: Memory Cleanup 
    pp.Activate 
    Set PPSlide = Nothing 
    Set PPPres = Nothing 
    Set pp = Nothing 

End Sub 

답변

0

당신은 너무처럼 Range.Address 속성을 사용할 필요가 캐릭터에게 이미 해당 주소 속성을 사용할 필요가 당신은 문자열로 Range을 Conver 유럽 필요가 없습니다 :

Sub SelectRange() 

    Dim Rng1 As Range 

    On Error Resume Next 'If the user presses cancel, it would cause an error 
    Set Rng1 = Application.InputBox("Select Cell", "Range Selection", Selection.Address, Type:=8) 
    On Error GoTo 0   'Remove the On Error Resume Next condition 
    If Rng1 Is Nothing Then Exit Sub 'Pressed cancel 

    MsgBox ("You selected " & Rng1.Address & " as the range") 

End Sub 

가 나는 또한 사용자가 취소를 누르면 오류가되지 않습니다 있는지 확인하십시오, 그리고 나는 기본적으로 현재 선택을 사용하는 것을 좋아합니다.

+0

감사합니다. Tigeravatar. 당신은 오류 검사가 필요하다는 것에 대해 절대적으로 맞습니다. 나는 테스트를하는 동안 몇 번 나 자신을 취소했다. 나는 이것을 변경하려고하는 주 모듈에 포함시킬 것이다. – user2498217

0

, 당신은

Sub SelectRange() 
    Dim Rng1 As Range 
Set Rng1 = Application.InputBox("select cell", Type:=8) 
MsgBox ("You selected " & Rng1.Address & "as the range") 
End Sub 
+0

우수! 그것은 그 문제를 해결했습니다. 자신이하는 일을 알고있는 사람과 솔루션이 얼마나 쉽고 얼마나 쉬운 지 알지 못하는 것이 언제나 얼마나 놀라운 일인가. 고맙습니다. 이제는 계속 공부할 수 있습니다. – user2498217