[英]Update cell based on two values for finding the row and column and corresponding worksheet
Any ideas how I can carry out the following using VBA:关于如何使用 VBA 执行以下操作的任何想法:
'Input a Value INTO Cell = [P4]'
'in Worksheet Titled = [P3] '
'WHERE ROW = [M4] AND Column = [N4]'
Basically when I click a button I want the formula that is in [Cell:P4] to be populated into another worksheet based on that worksheet having the title of [Cell:P3] and only in the Row and column where both the row has a number as stated in [Cell:M4] and the column has a number as stated in [Cell:N4].基本上,当我单击一个按钮时,我希望 [Cell:P4] 中的公式基于标题为 [Cell:P3] 的工作表填充到另一个工作表中,并且仅在两行都有一个的行和列中[Cell:M4] 中所述的数字,并且该列具有 [Cell:N4] 中所述的数字。
So first you need to know how to hook up your macro to a button.所以首先你需要知道如何将你的宏连接到一个按钮。 See this official documentation or this one from excelcampus.com.
请参阅此官方文档或来自 excelcampus.com 的此文档。 Basically, create a rectangle or any form you want your button to have, then right-click the form and click on "assign macro...".
基本上,创建一个矩形或您希望按钮具有的任何形式,然后右键单击该形式并单击“分配宏...”。 There are other more complicated ways, but this should be enough.
还有其他更复杂的方法,但这应该足够了。
So now about the macro itself I will give you a few hints.所以现在关于宏本身,我会给你一些提示。 You want to copy a formula into another cell.
您想要将公式复制到另一个单元格中。 The basic approach in VBA:
VBA中的基本做法:
Sub copyFormula()
Cells(1, 3).Formula = Cells(1, 2).Formula 'this will copy the formula in B1 to C1
End Sub
But we want to make this code a little safer.但我们想让这段代码更安全一点。 Lets write code that identifies the workbook and the worksheets that are involved.
让我们编写代码来标识所涉及的工作簿和工作表。
Option Explicit
Sub copyFormula()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim ws_source As Worksheet
Dim ws_target As Worksheet
Set ws_source = wb.Worksheets("Sheet1") 'replace Sheet1 with name of your sheet
Set ws_target = wb.Worksheets("Sheet2") 'replace Sheet2 with name of your sheet
ws_target.Cells(1, 3).Formula = ws_source.Cells(1, 2).Formula
End Sub
At this point you will need to worry about references in your formula (I am talking about the excel formula now, not the VBA code).此时您需要担心公式中的引用(我现在说的是 excel 公式,而不是 VBA 代码)。 Hint: Try using $ signs to lock cell references (not an immediate concern for this method) and also reference the sheet of the cell you are pointing to like this: "=Sheet2!A1"
提示:尝试使用 $ 符号来锁定单元格引用(此方法不是直接关注的问题)并引用您指向的单元格的工作表,如下所示:“=Sheet2!A1”
Okay, now lets modify the code a little more.好的,现在让我们再修改一下代码。 You want to address the sheet that has a variable name, namely the name that is written in Cell P3 (I assume of the source worksheet).
您想要处理具有变量名称的工作表,即在单元格 P3 中写入的名称(我假设为源工作表)。 We can do this.
我们做得到。 All we have to do is read the value of P3 and then replace our static "Sheet2" in the target worksheet definition with said variable.
我们所要做的就是读取 P3 的值,然后用所述变量替换目标工作表定义中的 static “Sheet2”。
Dim targetName As String
targetName = ws_source.Cells(3, "P").Value
Set ws_target = wb.Worksheets(targetName)
Try writing an invalid name into cell P3 and you will see why this is dangerous business as it is.尝试在单元格 P3 中写入一个无效的名称,您就会明白为什么这是一件危险的事情。 I will leave it to you to take precautions that the entered value in P3 will not break your code:)
我会留给您采取预防措施,确保 P3 中输入的值不会破坏您的代码:)
I do not really understand what you mean with the last part talking about rows and columns.我真的不明白你最后一部分谈论行和列的意思。 Show us what you have tried, then we can help improve your code.
向我们展示您的尝试,然后我们可以帮助改进您的代码。
Was this what you had in mind?这是你的想法吗? The function must be installed in the code sheet of the tab on which you have the command button.
function 必须安装在您有命令按钮的选项卡的代码表中。
Private Sub CommandButton1_Click()
' 027
On Error Resume Next
With Worksheets(Cells(3, "P").Value)
.Cells(CLng(Cells(4, "M").Value), CLng(Cells(4, "N").Value)) _
.Formula = Cells(4, "P").Formula
End With
If Err Then
MsgBox "There is an error in the specs." & vbCr & _
"Check the tab's name.", vbInformation, _
"Execution suspended"
Cells(3, "P").Select
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.