I am using code that I got from 101 Ready-to_use Macros by Michael Alexander and John Walkenbach.
In this code they specify the range values in the code. I want the users to be able to select the values. I'm getting errors after I've appeared to successfully have the user enter the value of the range. To help me debug the problem I've just written a short macro to test what i'm trying to do. But I can't seem to pass the information to the message box so I can see that it is working (although it appears to, be becuase when the user selects the range the address shows up in the entry box.) I cannot figure out how to take the user supplied RANGE type and turn it into a STRING value to show in my message or to use it as a Range. Now as a newbie, I'm now thinking this little test is more important than the original problem to help be learn to debug.
(I've tried some different variations including making the AS RANGE as AS Variant and trying to make a StringVariable assigned the value of the RANGE (eg UserRange = Rng1 Where UserRange is type String and Rng1 is type Range):
Here is my code
Sub SelectRange()
Dim Rng1 As Range
Set Rng1 = Application.InputBox("select cell", Type:=8)
MsgBox ("You selected " & Rng1 & "as the range")
End Sub
Here was the original code:
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
You don't need to conver the Range
to a string, you need to use its address property, which is already a string
Sub SelectRange()
Dim Rng1 As Range
Set Rng1 = Application.InputBox("select cell", Type:=8)
MsgBox ("You selected " & Rng1.Address & "as the range")
End Sub
You need to use the Range.Address property, like so:
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
I also like to make sure there won't be an error if the user presses cancel, and I like to use the current selection as the default.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.