VBA - Run Time Error 9: Copy and paste

China☆狼群 提交于 2020-01-04 02:40:09

问题


I recently learned about vba.

I've make a form with some fields, this fields function are

  1. sheetname
  2. range copy
  3. range paste
Private Sub CommandButton1_Click()
    Dim barisawal As String
    Dim sampaiawal As String
    Dim tujuanawal As String
    Dim tujuanakhir As String


    barisawal = bariske.Text
    barisakhir = TextBox2.Text
    tujuanawal = TextBox3.Text
    tujuanakhir = TextBox4.Text
    sheetawals = sheetawal.Text
    sheetakhirs = sheettujuan.Text

    Worksheets("sheetawals").Range("barisawal:barisakhir").Copy
    Worksheets("Sheetakhirs").Range("tujuanawal:tujuanakhir").Paste

End Sub

When I click my submit button CommandButton1, I get a runtime error #9 "Subscript out of range".

Here what I want to do: the user will fill the form with worksheet name, range and worksheet destination for paste.


回答1:


A subscript out of range error happens on this line:

Worksheets("sheetawals").Range("barisawal:barisakhir").Copy

The "sheetawals" is understood by VBA as a literal string, so it's trying to find a sheet named sheetawals in the active workbook. When it doesn't find it, it blows up with runtime error 9 / subscript out of range.

As @tospig correctly hinted, you mean to use the variables you've defined, not string literals - by removing the double quotes you'll fix your problem if the specified sheet names exist in the active workbook. Same for Range - you need to concatenate the strings using the string concatenation operator &, like this:

Worksheets(sheetawals).Range(barisawal & ":" & barisakhir).Copy

Same for the next line of code.

Now, you'll need to add some error-handling to prevent a runtime error when the user-provided values aren't legal. That's done with an On Error statement. Make that the first executable line of code in your procedure:

On Error GoTo ErrHandler

And then just before End Sub, add something like this:

    Exit Sub

ErrHandler:
    MsgBox "Invalid selection! Please try again."



回答2:


I haven't tested this code but this is how you should format the strings and Range

Private Sub CommandButton1_Click()

Dim barisawal As String
Dim sampaiawal As String
Dim tujuanawal As String
Dim tujuanakhir As String
Dim strRangeCopy as string
Dim strRangePaste as string

barisawal = bariske.Text
barisakhir = TextBox2.Text
tujuanawal = TextBox3.Text
tujuanakhir = TextBox4.Text
sheetawals = sheetawal.Text
sheetakhirs = sheettujuan.Text

strRangeCopy = barisawal & ":" & barisakhir
strRangePaste = tujuanawal & ":" & tujuanakhir

Worksheets(sheetawals).Range(strRangeCopy).Copy
Worksheets(Sheetakhirs).Range(strRangePaste).PasteSpecial

End Sub

To use the worksheet(...).Range(...) you need to have a string representing the range you will be using. This is typically formatted like Range("A1"), or Range("A1:A4")



来源:https://stackoverflow.com/questions/28080405/vba-run-time-error-9-copy-and-paste

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!