Michael Alexander と John Walkenbach による 101 Ready-to_use Macros から入手したコードを使用しています。
このコードでは、コード内の範囲値を指定しています。ユーザーが値を選択できるようにしたい。ユーザーが範囲の値を正常に入力したように見えた後、エラーが発生します。問題のデバッグを支援するために、私がやろうとしていることをテストするための短いマクロを書きました。しかし、メッセージボックスに情報を渡すことができないように見えるので、それが機能していることがわかります (ただし、ユーザーが範囲を選択すると、アドレスが入力ボックスに表示されるためです)。ユーザーが指定した RANGE タイプを取り、それを STRING 値に変換してメッセージに表示する方法、または範囲として使用する方法。初心者として、この小さなテストは、デバッグを学ぶのに役立つ元の問題よりも重要であると考えています。
(AS RANGE を AS Variant として作成し、StringVariable に RANGE の値を割り当てようとするなど、いくつかの異なるバリエーションを試しました (たとえば、UserRange = Rng1 UserRange は String 型で、Rng1 は Range 型です):
これが私のコードです
Sub SelectRange()
Dim Rng1 As Range
Set Rng1 = Application.InputBox("select cell", Type:=8)
MsgBox ("You selected " & Rng1 & "as the range")
End Sub
元のコードは次のとおりです。
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