[英]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.