简体   繁体   English

运行时错误“9”:下标超出范围

[英]Run-time error '9': Subscript out of range

I am trying to copy one column from a workbook to another workbook.我正在尝试将一列从工作簿复制到另一个工作簿。 This is my code and the error I am getting.这是我的代码和我得到的错误。

(Run-time error '9': Subscript out of range) (运行时错误“9”:下标超出范围)

Sub CopyColumn()
Dim sourceColumn As Range, targetColumn As Range

Set sourceColumn = Workbooks("source").Worksheets("a").Columns("H")
Set targetColumn = Workbooks("copyto").Worksheet("HCM_FBL_Person_Template_Sample_").Columns("A")

sourceColumn.Copy Destination:=targetColumn
End Sub

You have to open the files first, using exact file names (including their absolute path), and exact sheet names including any spaces您必须首先打开文件,使用准确的文件名(包括它们的绝对路径)和准确的工作表名称,包括任何空格


Option Explicit

Sub CopyColumn()
    Const FILE1     As String = "C:\TestFile1.xlsx"
    Const FILE2     As String = "C:\TestFile2.xlsx"
    Const SHEET1    As String = "Sheet2"
    Const SHEET2    As String = "Sheet1"
    Const COL1      As String = "A"
    Const COL2      As String = "D"

    Dim wb1 As Workbook, wb2 As Workbook, wb As Workbook, wbInfo As String
    Dim ws1 As Worksheet, ws2 As Worksheet, ws As Worksheet
    Dim sourceColumn As Range, targetColumn As Range

    If Dir(FILE1) > vbNullString And Dir(FILE2) > vbNullString Then
        For Each wb In Workbooks
            wbInfo = "\" & wb.Name
            If InStr(1, FILE1, wbInfo, vbBinaryCompare) > 0 Then Set wb1 = wb
            If InStr(1, FILE2, wbInfo, vbBinaryCompare) > 0 Then Set wb2 = wb
        Next
        If wb1 Is Nothing Then Set wb1 = Workbooks.Open(FILE1)
        If wb2 Is Nothing Then Set wb2 = Workbooks.Open(FILE2)
        If Not wb1 Is Nothing And Not wb2 Is Nothing Then
            For Each ws In wb1.Worksheets
                If ws.Name = SHEET1 Then Set ws1 = ws
            Next
            For Each ws In wb2.Worksheets
                If ws.Name = SHEET2 Then Set ws2 = ws
            Next
            If Not ws1 Is Nothing And Not ws2 Is Nothing Then
                Set sourceColumn = wb1.Worksheets(SHEET1).UsedRange.Columns(COL1)
                Set targetColumn = wb2.Worksheets(SHEET2).UsedRange.Columns(COL2)
                sourceColumn.Copy Destination:=targetColumn
            End If
        End If
    End If
End Sub

This also checks if the files are already open or not, and if sheet names exists in each file这还会检查文件是否已经打开,以及每个文件中是否存在工作表名称

Maybe this will solve your problem-也许这会解决你的问题——

Sub sbCopyRangeToAnotherSheet()

    Sheets("Sheet1").Range("A1:Z1").Copy

     Range("A1:Z1").Select
     Sheets("Sheet2").Activate
     ActiveSheet.Paste

     Application.CutCopyMode = False

     End Sub

ALternatively if you dont want to specify fixed range you can declare a Range object as use End(xlLeft) to select all range from A1-allleftColumns-或者,如果您不想指定固定范围,您可以将 Range 对象声明为使用 End(xlLeft) 从 A1-allleftColumns- 中选择所有范围-

I hope it will help you.我希望它会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM