[英]How to pass a Range type value to a String Type variable
我正在使用從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
您不需要將Range
轉換為字符串,您需要使用其address屬性,該屬性已經是字符串
Sub SelectRange()
Dim Rng1 As Range
Set Rng1 = Application.InputBox("select cell", Type:=8)
MsgBox ("You selected " & Rng1.Address & "as the range")
End Sub
您需要使用Range.Address屬性,如下所示:
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
我還想確保如果用戶按“取消”,不會有錯誤,並且我希望將當前選擇用作默認值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.