简体   繁体   English

根据两个值更新单元格以查找行和列以及相应的工作表

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

相关问题 如何根据工作表最后一列中的单元格值隐藏行? - How to hide row based on cell value in last column of worksheet? Excel从A列循环并查找与相应值的匹配并将它们移动到新工作表 - Excel looping from column A and finding matches with corresponding values and moving them to a new worksheet Excel 宏根据匹配两个值(行/列)查找单元格并替换单元格中的值 - Excel Macro to find a cell based on matching two values (Row/Column) and replace value in cell 然后从一个单元格中查找字符串中的文本,然后将值从对应的行复制到另一个单元格中 - Finding text in a string then from a cell, then copying values over from the corresponding row to another cell 查找某个值是否位于对应列的某个单元格范围内,到与给定行中给定单元格具有相同值的行 - Finding if a value is located in a range of cells in a corresponding column, to a row with the same value as a given cell in a given row 根据垂直行/列值格式化单元格 - Formatting a cell based on perpendicular row/column values 查找在两个不同列中包含两个单元格值的行 - Finding the row that contains two cell values in two different columns excel vba-在列中查找单元格的值并记录相应行的另一列中的文本 - excel vba - finding a value of a cell in a column and recording what text is in the another column of the corresponding row Excel:根据相应行打印列值列表? - Excel: print list of column values based on corresponding row? 根据同一行中另一个单元格的值计算相应单元格中的单元格值 - Count cell values in corresponding cell based on the value of another cell in the same row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM