简体   繁体   English

如何在Excel VBA中更改行值和列值

[英]How to change the row values and column values in excel vba

Hi I am very new to Excel VBA 嗨,我是Excel VBA的新手

I want to change row and column values My data is similar to this 我想更改行和列的值我的数据与此类似 在此处输入图片说明

My age is   My height is    My weight is 
between 10-20   150 45
between 21-30   170 56
between 10-20   155 60
between 10-20   160 53
between 21-30   173 68

I want to make the data like this 我想要这样的数据

在此处输入图片说明

Age Hght    Wght
Category A  150 45
Category B  170 56
Category A  155 60
Category A  160 53
Category B  173 68

ie I changed the first row 即我改变了第一行

Then I changed all "between 10-20" to "Category A" and "between 21-30" to "Category B" 然后我将所有“ 10-20之间”更改为“类别A”,将“ 21-30之间”更改为“类别B”

I prefer not to use column numbers in code, instead I prefer to use column names. 我更喜欢在代码中不使用列号,而更喜欢使用列名。

ie like this, If Age="between 10-20" then Age="Category A" 即像这样,如果Age =“ between 10-20” then Age =“ Category A”

How can we do it? 我们该怎么做?


First of all, make sure you put the data in the table. 首先,请确保将数据放在表中。 (not simply type in the data in the excel's cells) (不仅仅是在Excel的单元格中输入数据)
You should go to INSERT ribbon there insert the table. 您应该转到INSERT功能区,在其中插入表格。
表名称:年龄表

Next, follow the advice from @Bryon, create another table called "DataTable" like following figure. 接下来,按照@Bryon的建议,创建另一个名为“ DataTable”的表,如下图所示。 You may create this table at another worksheet (eg MyData) 您可以在另一个工作表(例如MyData)上创建此表
表名:数据表

Lastly, you create another table called "MyResult". 最后,您创建另一个表“ MyResult”。 At the 1st row of the table, put the formula in the cell as following based on the columns : 在表格的第一行,根据以下各列,将公式放在单元格中,如下所示:
=VLOOKUP(AgeTable[@[My age is]],DataTable,2,TRUE) = VLOOKUP(AgeTable [@ [我的年龄是]],DataTable,2,TRUE)
column 2 第2栏
=VLOOKUP(AgeTable[@[My Height is]],AgeTable[@[My Height is]],1,TRUE) = VLOOKUP(AgeTable [@ [我的身高是]],AgeTable [@ [我的身高是]],1,TRUE)
column 3 第三栏
=VLOOKUP(AgeTable[@[My Weight is]],AgeTable[@[My Weight is]],1,TRUE) = VLOOKUP(AgeTable [@ [我的体重是]],AgeTable [@ [我的体重是]],1,TRUE)

Following is the output of the table: 以下是该表的输出:
表名称:MyResult
Whenever values had been change at column 2 and column 3 of table 1, it will change the column 2 and column 3 of table 3 每当在表1的第2列和第3列中更改值时,它将更改表3的第2列和第3列

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

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