简体   繁体   English

如何使用公式或VBA根据来自另一列的另一个单元格值来设置单元格中的值?

[英]How to set value in a cell based on another cell value from another column using formula or VBA?

I have two sheets: 我有两张纸: 在此处输入图片说明

Sheet 1 : consist of three columns ( Status , Cost , Version ) 表格1:由三列组成( 状态成本版本

Sheet 2 : Used as an Action Page with two columns ( Formula , Version Number ). 工作表2:用作两页(“ 公式” ,“ 版本号” )的“操作页面”。

Question: 题:

I want to set a version number in column two in sheet 2 and let the formula or the Button using VBA code to do the following: 我想在工作表2的第二栏中设置版本号,并使用VBA代码让公式或Button执行以下操作:

  1. The User set value = 5 in column 2 [Version Number] sheet 2 用户设置值= 5在第2列[版本号]第2页中
  2. The System takes the value from [Version Number] cell 系统从[版本号]单元格获取值
  3. The System search for all rows containing the [Version Number] value = 5 , in sheet 1 column [Version] 系统在工作表1列[版本]中搜索包含[版本号]值= 5的所有行。
  4. The System Replace each cell in column [ Status ] having [ Version ] value= 5 , with Status Value = "Delivered". 系统将[ 状态 ]列中具有[ 版本 ]值= 5的每个单元格替换为状态值= “已交付”。

Any help please either throw formula or using the VBA Code. 任何帮助请抛出公式或使用VBA代码。

Regards 问候

If that's all you want to achieve, and if you can use a formula in Sheet1 , in the [Status] field, then probably all you want to do is to use the following formula: 如果仅此而已,并且可以在Sheet1[Status]字段中使用公式,则可能要做的就是使用以下公式:

=IF(C2 = 5, "Delivered", "Pending")

This is basically looking at your Version column and checks if your version number is 5 , then change the text to Delivered , else use Pending (or whatever you need). 基本上,这是在查看您的“ 版本”列,并检查您的版本号是否为5 ,然后将文本更改为“ 已交付” ,否则使用“ 待定” (或您需要的任何值)。

I would like to thank you all for your advice , after searching for the key code to know how to return the number of rows filled in a specific table , I found the solution and now I am able to do my logic. 我想感谢大家的建议,在搜索了关键代码以知道如何返回特定表中填充的行数之后,我找到了解决方案,现在我可以执行我的逻辑了。 Below is my solution for the above question. 以下是我针对上述问题的解决方案。

Steps: 脚步:

  1. Create Variable to hold the values 创建变量以保存值
  2. The below variable will hold the inserted value need to be updated 以下变量将保存需要更新的插入值
  3. The below variable will hold the Item Version Column number in the table 下面的变量将在表中保存项目版本列号
  4. The below variable will hold the value of each row under the column [Get_Version_Item_Column] to be used when comparing the values of each row 下面的变量将保存[Get_Version_Item_Column]列下要比较的每一行的值
  5. The below variable will hold the Item Progress Column number in the table 下面的变量将在表中保存项目进度列号
  6. The below variable will hold the value of each row under the column [Get_Item_Progress_Column] to be used when comparing the values or each row 下面的变量将保存[Get_Item_Progress_Column]列下要比较的每一行的值或每一行的值
  7. The below variable used to get the total number of used rows in the table 下面的变量用于获取表中已用行的总数
  8. The below variable used as a counter to go throw a loop for all the rows 下面的变量用作计数器来为所有行抛出循环

Create a Function that will do the below in Order: 创建一个将按顺序执行以下操作的函数:

  1. Get Column Number from table 1 Sheet1 where header name = [Fixed in Version] 从表1 Sheet1中获取列号,其中表头名称= [固定于版本]

  2. Get Column Number from table 1 Sheet1 where header name = [Item Progress] 从表1 Sheet1中获取列号,其中标题名称= [Item Progress]

  3. Select Sheet 2 to get the value inserted in the Cell B1 选择工作表2以获取插入到单元格B1中的值

  4. Get Inserted value from the cell located inside the Sheet2 从Sheet2内部的单元格获取插入值

  5. Set Counter value = 1 to start the loop from the first row after the header in table 1 Sheet 1 设置Counter value = 1以从表1表1中的标题之后的第一行开始循环

  6. Open while Loop and set a condition if counter <= Total number of rows do the below 在while循环中打开,并在计数器<=总行数执行以下操作时设置条件

  7. Start moving row by row in the table using the Cells Method : Cells(counter, Get_Version_Item_Column) ' in the cells method we set the [Row]=> counter variable which starts with value = 1 , [Column]=> Get_Version_Item_Column ' 使用Cells方法开始在表中逐行移动:Cells(counter,Get_Version_Item_Column)'在cells方法中,我们设置[Row] =>计数器变量,该变量以值= 1开头,[Column] => Get_Version_Item_Column'

  8. Get the value of the second column we want to put into the condition with the same way shown in Step 7 用步骤7中所示的相同方式获取要放入条件中的第二列的值

  9. Start setting the condition usinf IF Else 如果其他条件,则开始设置条件usinf

  10. If Get_Fixed_In_Version_Value = Get_Version_Inserted_Value And Get_Item_Progress_Value = "Approved" Then 'IF CORRECT NUMBER ' DO something Example [Replace the value in this row , this column , this cell with value = "" ] 如果Get_Fixed_In_Version_Value = Get_Version_Inserted_Value和Get_Item_Progress_Value =“已批准”,则'IF正确数字'执行某些操作示例[替换此行中的值,此列,此单元格的值为=“”]

  11. Else ' Do something Else 别做别的

  12. Add 1 to the counter 将1加到计数器

  13. Close the While Loop 关闭While循环

Code Solution: 代码解决方案:

Public Get_Version_Inserted_Value As String
Public Get_Version_Item_Column As Integer
Public Get_Fixed_In_Version_Value As String
Public Get_Item_Progress_Column As Integer
Public Get_Item_Progress_Value As String
Public QATotal_Items_Row As Integer
Public counter As Integer

Function Change_Version_Item_Progress() As String

Get_Version_Inserted_Value = ThisWorkbook.Worksheets("Sheet2").Range("B1").Value2

Get_Version_Item_Column = Application.Match("Fixed in Version", Sheets("Sheet1").Rows(1), 0)

Get_Item_Progress_Column = Application.Match("Item Progress", Sheets("Sheet1").Rows(1), 0)

Sheets(Sheet1).Select
QATotal_Items_Row = WorksheetFunction.CountA(Range("B:B")) - 1 
counter = 1
 While counter <= QATotal_Items_Row

    Get_Fixed_In_Version_Value = ThisWorkbook.Worksheets("Sheet1").Range("Table1").Cells(counter, Get_Version_Item_Column).Value2

    Get_Item_Progress_Value = ThisWorkbook.Worksheets("Sheet1").Range("Table1").Cells(counter, Get_Item_Progress_Column).Value2

    If Get_Fixed_In_Version_Value = Get_Version_Inserted_Value And Get_Item_Progress_Value = "Approved" Then 

        Get_Item_Progress_Value = ThisWorkbook.Worksheets("Sheet1").Range("Table1").Cells(counter, Get_Item_Progress_Column).Value = "Delivered"

    Else 
        Get_Item_Progress_Value = ThisWorkbook.Worksheets("Sheet1").Range("Table1").Cells(counter, Get_Item_Progress_Column).Value = Get_Item_Progress_Value

    End If
        counter = counter + 1 
    Wend

End Function

Hope it will help. 希望它会有所帮助。

Regards 问候

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM