在vba中将多个范围粘贴到另一个工作表

时间:2017-09-27 16:15:46

标签: excel vba excel-vba

我希望代码在'rngcel'下面粘贴'cashb',但每次都是 我运行代码'cashb'的值出现在'rngCel'。value之上。 Rngcell的范围来自A2:A34,我希望'Cashb'出现在A35的正下方。我试过把'A35'放进去 范围,但它不起作用。

这是我想在rngcel.value下面显示的代码。

Sheets(" Price").Range("A35").Resize(Cashb.Rows.Count).Value = Cashb.Value

我还想在“cashb”范围右侧返回5列的列 我感谢您收到的任何帮助。

这是我的代码。谢谢。

    Sub liveP()

   Application.ScreenUpdating = False

    Dim rngTicker As Range
    Dim rngCel As Range
    Dim Cashb As Range
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets.Add(After:= _
             ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
             ws.Name = "Live"
    Set rngTicker = Worksheets("prices").Range("H16:H200") 
    Set Cashb = Sheets("prices").Range("cashbalances")


    For Each rngCel In rngTicker

        If rngCel.Font.ColorIndex = 33 Then
         Worksheets("Live").Cells(Rows.Count, 1).End(xlUp).offset(1).Resize(1, 2).Value = Array(rngCel.offset(, "-7").Value, rngCel.Value) ' this is range cell value'
         WorkSheets("Live").Range("A35").Resize(Cashb.Rows.Count).Value = Cashb.Value.offset ' this is the value I'd like to appear under rngcel value

            'New data that im posting on the Live sheet'
              Sheets("Live").Range("C2:H33").Formula = "=($B2 +$C5)"
              Sheets("Live").Range("A1") = "Header1"
              Sheets("Live").Range("B1") = "Header2"
              Sheets("Live").Range("C1") = "Header3"
              Sheets("Live").Range("D1") = "Header4"
              Sheets("Live").Range("E1") = "Header5"
              Sheets("Live").Range("F1") = "Header6"

        End If


    Next


  Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:0)

试试这个

Sub liveP()

Application.ScreenUpdating = False

Dim rngTicker As Range
Dim rngCel As Variant 'used in for each this should be variant
Dim Cashb As Range
Dim ws As Worksheet
Dim LastRow As Long 'dimensioned variable for the last row
Dim CashbRows As Long 'dimensioned variable for Cashb rows
Set ws = ThisWorkbook.Sheets.Add(After:= _
         ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
         ws.Name = "Live"
Set rngTicker = Worksheets("prices").Range("H16:H200")
Set Cashb = Sheets("prices").Range("cashbalances")
'Assuming "cashbalances" is a named range in the worksheet

CashbRows = Cashb.Rows.Count

For Each rngCel In rngTicker

    If rngCel.Font.ColorIndex = 33 Then
     With Worksheets("Live")
        LastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1 'set lastrow variable
        .Cells(LastRow, 1) = rngCel.Offset(0, -7).Value 'putting value 7 columns before into live worksheet column A
        .Cells(LastRow, 2) = rngCel.Value 'putting value into live worksheet column B
        .Range(.Cells(35, 1), .Cells(35 + CashbRows, 1)) = Cashb.Offset(, 5).Value 'im not really sure if this line is going to work at all

        'New data that im posting on the Live sheet'
          .Range("C2:H33").Formula = "=($B2 +$C5)"
          .Range("A1") = "Header1"
          .Range("B1") = "Header2"
          .Range("C1") = "Header3"
          .Range("D1") = "Header4"
          .Range("E1") = "Header5"
          .Range("F1") = "Header6"
     End With

    End If

Next

Application.ScreenUpdating = True

End Sub

修改:

rngCel现在是变种而不是范围

使用LastRow变量远离偏移量

将数据放入“LIVE”时删除了数组,因为为什么不

CashbRows现在只会在循环前计算一次。节省时间

With Worksheets("Live")语句是一项节省时间的措施。

您将A35称为范围,而不是,然后调整到范围可能?很难知道什么时候我不知道什么是“现金余额”。如果“现金余额只有1行或者可能只有1行,那么您需要添加If Then Else控件来处理它。

每次循环都会覆盖A35 ......所以不确定你想在那里做什么。

我希望我能够很好地理解你的问题,让你朝着正确的方向前进。

修改

停止将“cashbalances”视为命名范围。我相信VBA挂在范围的原始行号上,类似于Variant数组从1开始时的指定,如下所示。它看起来不像你正在修改“现金余额”,所以在循环之前但在CashbRows之后创建一个变量数组。

实施例

Dim CB() as variant, j as long
with sheets("PUT THE SHEET NAME OR INDEX HERE")
CB = .range(.cells(1,6), .cells(CashbRows,6)).value 'address to whatever .offset(,5) is
'i assumed cashb was in column A 

而不是.Range(.Cells(35, 1), .Cells(35 + CashbRows, 1)) = Cashb.Offset(, 5).Value使用:

For j = 1 to CashbRows
    .cells(34 + j, 1) = CB(j)
Next j