首页 > 解决方案 > 在工作表之间链接代码和数组

问题描述

我在表 3 中有一个数组“v”,我需要使用循环将它的最后一列复制到表一。只有当我在表 3 上时它才有效。无论我在哪张表上,我都希望它能够工作. 在这个循环中我有

    For i = 1 To Cells(12, 8).Value  'this cell is in sheet 2


    'i need this Cells(7, i + 8).Value  output to be in sheet 1 
Cells(7, i + 8).Value = v(i, UBound(v, 1))

    Next i

   ' for the array 
    With Worksheets("three")
    Const firstCol As Long = 7, firstRow As Long = 12

        lastCol = Sheet3.Cells(firstRow, Columns.Count).End(xlToLeft).Column
        lastRow = Sheet3.Cells(Rows.Count, lastCol).End(xlUp).Row


         v= Range(Cells(firstRow, firstCol), Cells(lastRow, lastCol))
            End With

标签: excelvba

解决方案


胸罩X

这应该完成它:

Dim Src as Worksheet
Dim Dst as Worksheet 

Set Dst = Worksheets("One") 'I assume this isn't the real name?
Set Src = Worksheets("Three") 'I assume this isn't the real name?

'You'll progablly want to dim/set this sheet also but I don't know what to call it
 For i = 1 To Worksheets("Sheet2").Cells(12, 8).Value  'this cell is in sheet 2


    'i need this Cells(7, i + 8).Value  output to be in sheet 1 
   Dst.Cells(7, i + 8).Value = v(i, UBound(v, 1))

    Next i

   ' for the array 
    With Src
        Const firstCol As Long = 7, firstRow As Long = 12

        lastCol = .Cells(firstRow, Columns.Count).End(xlToLeft).Column
        lastRow = .Cells(Rows.Count, lastCol).End(xlUp).Row

'*** Don't know which sheet formula below is looking at but you get the gist
         v= Range(Cells(firstRow, firstCol), Cells(lastRow, lastCol))
    End With 'Src

高温高压


推荐阅读