使用合并单元格选择范围

时间:2014-03-18 20:58:56

标签: excel vba excel-vba

因此,有一个工作簿运行由公司中的另一个组及其GARBAGE创建的宏,但是他们锁定了它并且不允许我修改VBA。所以我试图将该工作簿的输出提取到我的工作簿中。范围的标题位于合并行中,前11行由按钮占用以运行宏。

这是我目前的代码

Sub Pull_Data()
Dim returnValue As Variant
Dim wb As Workbook
Dim ws As Worksheet
Dim wb2 As Workbook
Dim ws2 As Worksheet
Set wb2 = Application.Workbooks("name.xlsm")
Set ws2 = wb2.Worksheets("INPUT")
Dim rng As Range
r = Range("H65536").End(xlUp).Row
Set ws = wb.Worksheets("Report")
ws.Range("A11:A13").Select
Set rng = ws.Range(Cells(11, 1), Cells(r, 8))
rng.Select
rng.Copy
    wb2.Activate
ws.Range(Cells(1, 1), Cells(r, 8)).PasteSpecial xlPasteAll

End Sub

我的问题是,当我尝试选择整个输出时,它有时会选择我想要的东西(A11到H,然后是最后一行有值),但有时它会选择A1:H11并且令人沮丧地想出问题。如果有更好的方式我想知道。

编辑:

更新了代码

Sub Pull_Data()
Dim returnValue As Variant
Dim wb As Workbook
Dim ws As Worksheet
Dim wb2 As Workbook
Dim ws2 As Worksheet
Set wb2 = Application.Workbooks("name.xlsm")
Set ws2 = wb2.Worksheets("INPUT")
Dim rng As Range
r = Application.Max(Range("H65536").End(xlUp).Row, 11)
Set ws = wb.Worksheets("Report")
ws.Range("A11:A13").Select
Set rng = ws.Range(Cells(11, 1), Cells(r, 8))
rng.Select
rng.Copy
    wb2.Activate
ws.Range(Cells(1, 1), Cells(r, 8)).PasteSpecial xlPasteAll

End Sub

2 个答案:

答案 0 :(得分:0)

不是将数据从garbage.xlsm提取到工作簿中,而是将整个工作表复制到工作簿中。

然后在您的版本的工作表中,删除合并等。

答案 1 :(得分:0)

r = Application.Max(Range("H65536").End(xlUp).Row, 11) 'wich sheet is the range assiciated with ?
Set ws = wb.Worksheets("Report")
ws.Range("A11:A13").Select 'how is this line usefull at all ?
Set rng = ws.Range(Cells(11, 1), Cells(r, 8)) ' change to  ws.Range(ws.Cells(11, 1), ws.Cells(r, 8))
rng.Select 'delete this line
rng.Copy '
wb2.Activate
ws.Range(Cells(1, 1), Cells(r, 8)).PasteSpecial xlPasteAll 'change to ws.Range(ws.Cells(1, 1), ws.Cells(r, 8)).PasteSpecial xlPasteAll