简体   繁体   English

从特定的工作表和单元格复制公式,并将其向下拖动直到特定列的最后一行

[英]Copy formula from a specific sheet and cell and drag it down until specific column last row

So far I have this code below; 到目前为止,我的代码如下; counting last cell is working fine but is copy/pasting the wrong data to wrong sheet. 计算最后一个单元格工作正常,但是将错误的数据复制/粘贴到错误的工作表中。 Should copy data and use the formula from Sheet "Parsing" cell B2, and its using the main sheet where is the VBA. 应该复制数据并使用工作表“解析”单元格B2中的公式,并使用VBA所在的主工作表。 Looks lile what is missing is to execute the copy/select to "parsing" sheet, but didnt manage to do it. 看起来,缺少的是执行复制/选择以“解析”工作表,但是没有做到这一点。

Sub drag_formula_original()

Dim myLastRow As Long
With Worksheets("Parsing")



myLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

.Range("B2").Copy Destination:=.Range("B2:B" & myLastRow)
Application.CutCopyMode = False

End With
End Sub

Its solved. 解决了。 Thanks a lot. 非常感谢。

Range("B2").Copy 

The above will grab by default from the Activesheet 默认情况下,以上内容将从Activesheet中获取

you have to tell it what sheet you would like it to pick that range/value from. 您必须告诉它您要从哪个表中选择该范围/值。

sheets("Parsing").Range("B2").Copy 

Edit: Just noticed your With 编辑:刚注意到您的

To actually use a with you need to use a "." 要与一起实际使用,您需要使用“。” eg your copy line would look like below 例如,您的复制行如下所示

.Range("B2").Copy 

One other thing to note this: 需要注意的另一件事:

Range("B2:B" & myLastRow).Select
ActiveSheet.Paste

Is rather inefficient, below would be better. 是相当低效的,下面会更好。 Selecting in general is best to keep away from it is rather slow 一般而言,最好选择远离它,这相当慢

 Range("B2:B" & myLastRow).Paste

Or with your with 或与您一起

.Range("B2:B" & myLastRow).Paste

I just copied and pasted your code and ran it. 我只是复制并粘贴您的代码并运行它。 I changed nothing in your code except for adding "Option Explicit" before your sub. 除了在您的子代码之前添加“ Option Explicit”外,我没有更改您的代码中的任何内容。 (Just a personal habit) (只是个人习惯)

Option Explicit
Sub drag_formula_original()

Dim myLastRow As Long
With Worksheets("Parsing")

myLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

Range("B2").Copy
Range("B2:B" & myLastRow).Select
ActiveSheet.Paste
Application.CutCopyMode = False

End With
End Sub

I did, however, use a very simple formula in cell B2. 但是,我确实在单元格B2中使用了一个非常简单的公式。 What I did was have column A go from 1 to 10 and column C go from 11 to 20. Cell B2 was =A2+C2. 我所做的是将A列从1移至10,将C列从11移至20。单元格B2为= A2 + C2。 After running the code I checked each cell in column B and they each had the correct formula in them, and not a hard-coded value. 运行代码后,我检查了B列中的每个单元格,每个单元格中都有正确的公式,而不是硬编码的值。

A trick I do when I want to do something like this but can't figure out how is I record a macro of me dragging the cell formula down a little ways and then stop the recording and look at the code it made. 当我想执行类似操作但无法弄清楚如何记录我的宏时,我会做一个技巧,将宏单元格公式向下拖动一点,然后停止记录并查看其代码。 From that you should be able to adjust it to do what you want. 由此,您应该能够调整它以执行所需的操作。

When I did that I got this code: 当我这样做时,我得到了以下代码:

Sub Macro1()
'
' Macro1 Macro
'

'
    Range("B2").Select
    Selection.AutoFill Destination:=Range("B2:B15"), Type:=xlFillDefault
    Range("B2:B15").Select
End Sub

暂无
暂无

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

相关问题 从具有公式的最后一行向下拖动公式,直到“ A”结束 - Drag down formula from last row with formula to next, until “A” ends 在不使用dim的情况下将公式向下复制到一列直到最后一行 - Copy formula down a column until the last row without using dim 根据另一个工作表的最后一行向下拖动公式 - Drag down formula based on the last row of a another sheet 如何将一列从特定行一直复制到带有值的最后一行? - How to copy a column from a specific row all the way down to the last row with values? 使用单元格引用从特定行号复制到最后一行 - copy till last row from specific row number with cell reference 将工作表1中某些行中的特定单元格复制(取决于列中的非零值)到工作表2的最后一个空行 - Copy specific cells from sertain rows from Sheet 1 depending on non-zero value in column to the last empty row of Sheet 2 将特定单元格数据复制到其他工作表的列中 - Copy a specific cell data into column of other sheet VBA向下填充公式,直到空列中的最后一行 - VBA to fill formula down until last row in empty column 计数直到填充特定列的最后一行 - Countif until the last row in specific column is populated 从第二行复制一列数据,直到其中有数据的最后一行,然后粘贴为另一张表 VBA 上一列的最后一行 - Copy a column of data from the second row until the last row with data in it and paste as the last row in a column on another sheet VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM