Excel VBA:下标超出范围的代码修复

我一直在行中收到下标超出范围错误

Sheets("Dump").Select

如何调整我的代码以消除错误?有没有办法调整它以删除 .Select

Sub UploadData()
'open the source workbook and select the source
Dim wb As Workbook

Workbooks.Open Filename:=Sheets("Instructions").Range("$B$4").value
Set wb = ActiveWorkbook
Sheets("Invoice Totals").Select

'copy the source range
Sheets("Invoice Totals").Range("A:R").Select
Selection.Copy

'select current workbook and paste the values
ThisWorkbook.Activate
Sheets("Dump").Select
Sheets("Dump").Range("A2").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False

' copy the source range
Sheets("Lease & RPM Charges").Range("A:AH").Select
Selection.Copy

'select current workbook and paste the values
ThisWorkbook.Activate
Sheets("Dump").Select
Sheets("Dump").Range("T2").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False

'copy the source range
Sheets("MMS_Service_And_Repairs").Range("A:R").Select
Selection.Copy

'select current workbook and paste the values
ThisWorkbook.Activate
Sheets("Dump").Select
Sheets("Dump").Range("BC2").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False

'close the source workbook
wb.Close

End Sub
GMalc

编辑(修复问题)试试这个...(在模拟数据上测试)。

Sub UploadData()

Dim wb As Workbook
Dim lRow As Long, lRow2 As Long, lRow3 As Long 'Set lastrow for each source worksheet
Dim rng As Range, rng2 As Range, rng3 As Range 'Set range for each source worksheet

'open the source workbook using the filename in cell B4(I'm making an assumption that the
'source workbook is located in the same folder as the Thisworkbook
Set wb = Workbooks.Open(Filename:=Sheets("Instructions").Range("$B$4").Value)

With wb
    With .Sheets("Invoice Totals") 'Copy the range on this ws and paste to "Dump" in dest ws
        lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        Set rng = .Range("A1:R" & lRow)
            rng.Copy Destination:=ThisWorkbook.Sheets("Dump").Range("A2")
    End With

    With .Sheets("Lease & RPM Charges") 'Copy the range on this ws and paste to "Dump" in dest ws
        lRow2 = .Cells(.Rows.Count, 1).End(xlUp).Row
        Set rng2 = .Range("A1:AH" & lRow2)
            rng2.Copy Destination:=ThisWorkbook.Sheets("Dump").Range("T2")
    End With

    With .Sheets("Invoice Totals") 'Copy the range on this ws and paste to "Dump" in dest ws
        lRow3 = .Cells(.Rows.Count, 1).End(xlUp).Row
        Set rng3 = .Range("A1:R" & lRow3)
            rng3.Copy Destination:=ThisWorkbook.Sheets("Dump").Range("BC2")
    End With

    With ThisWorkbook.Sheets("Dump").UsedRange
       .Value = .Value  'Sets all the data in the usedrange to values only
    End With
End With

wb.Close 'close the source workbook

End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

下标超出范围VBA Excel数组

来自分类Dev

VBA/Excel:“下标超出范围”错误

来自分类Dev

Excel VBA无法修复运行时错误“ 9”:下标超出范围

来自分类Dev

Excel VB下标超出范围修复宏

来自分类Dev

此Excel VBA脚本中的下标超出范围错误

来自分类Dev

Excel 2013 VBA:下标超出范围(错误9)

来自分类Dev

Excel VBA 下标在 00:00 拆分时超出范围

来自分类Dev

Excel VBA-运行时错误'9',下标超出范围

来自分类Dev

VBA-下标超出范围错误导致Excel崩溃

来自分类Dev

具有下标超出范围错误的Excel VBA剪切和粘贴功能

来自分类Dev

下标超出范围 - 使用 Excel VBA 查找下一个

来自分类Dev

VBA Excel,形状数组。如何修复错误“指定集合的索引超出范围”?

来自分类Dev

VBA下标超出范围,代码9

来自分类Dev

下标超出范围,VBA

来自分类Dev

VBA:下标超出范围

来自分类Dev

Excel VBA - .xlam (AddIn) 抛出下标超出范围错误 9 但一切都像 xlsm 一样?

来自分类Dev

运行时错误:Excel 宏 2016 中的“9”下标超出范围

来自分类Dev

代码中的下标超出范围错误

来自分类Dev

VBA-下标超出范围

来自分类Dev

VBA-下标超出范围

来自分类Dev

下标超出范围的VBA变体数据

来自分类Dev

尝试将Excel图表复制到Power Point演示文稿时下标超出范围错误

来自分类Dev

从 Excel 获取数据 - 索引超出范围

来自分类Dev

如何在简单的代码中修复“下标超出范围”错误

来自分类Dev

vba错误编号9下标超出范围

来自分类Dev

VBA-公共阵列错误-下标超出范围

来自分类Dev

数组中的VBA随机元素,下标超出范围错误

来自分类Dev

VBA下标超出范围和错误9

来自分类Dev

VBA中ReDim的下标超出范围(错误9)

Related 相关文章

热门标签

归档