简体   繁体   English

Excel VBA - 如何将单元格引用更改为倒数第二列和最后一列

[英]Excel VBA - How to change a cell reference to the second last and last column

My first column consists of the currency names and the column data are monthly exchange rates (with the first row as month names).我的第一列由货币名称组成,列数据是每月汇率(第一行是月份名称)。

Every month, I add a new column with the latest exchange rates.每个月,我都会添加一个包含最新汇率的新列。 I need to amend the cell references of last month's rate and this month's rate to the second last and last column of the sheet.我需要将上个月汇率和本月汇率的单元格引用修改为表格的倒数第二列和最后一列。 Is there any code for this?有任何代码吗?

Thank you谢谢

lLastColumn = Cells(1, Columns.Count).End(xlToLeft).Column

This will give the last column assuming the header is in first row.假设 header 在第一行,这将给出最后一列。 Else, change first argument accordingly否则,相应地更改第一个参数

Columns.Count give the total number of columns in the worksheet (which is 16384 in the new Excel formats) Columns.Count给出工作表中的总列数(在新的 Excel 格式中为 16384)

Cells(1, Columns.Count) will give the range of cell in last column in the first row Cells(1, Columns.Count)将给出第一行最后一列的单元格范围

Cells(1, Columns.Count).End(xlToLeft) will give the range of last non-blank column in the first row. Cells(1, Columns.Count).End(xlToLeft)将给出第一行中最后一个非空白列的范围。 It simulated Ctrl+Left key它模拟了 Ctrl+Left 键

Finally, Cells(1, Columns.Count).End(xlToLeft).Column will give the column number of last column in the header最后, Cells(1, Columns.Count).End(xlToLeft).Column将给出 header 中最后一列的列号

You can then refer other data in last column using Cells(i, lLastColumn)然后,您可以使用 Cells(i, lLastColumn) 引用最后一列中的其他数据

You don't need code for this.你不需要代码。 With a very small change to your setup you can use this function.只需对您的设置进行很小的更改,您就可以使用此 function。

=INDEX(Sheet1!$A$1:$ZZ$1000, MATCH($A3,Sheet1!$A:$A,0),MATCH(MAX(Sheet1!$1:$1),Sheet1!$1:$1,0))

The required change is to enter dates in the first row of your sheet.所需的更改是在工作表的第一行输入日期。 (I refer to it as Sheet1 but it could be any name.) To make a caption like "January", enter 1/1/2021 and format the cell as custom mmmm . (我将其称为Sheet1 ,但它可以是任何名称。)要制作像“一月”这样的标题,请输入 1/1/2021 并将单元格格式化为自定义mmmm Use the format mmm yy to display "Jan 21" etc. Having such a date in the first column - that would be column B - use this formula to write subsequent column headers: =EDATE(B1,1) and copy to the right.使用mmm yy格式显示“Jan 21”等。在第一列中有这样的日期 - 即 B 列 - 使用此公式编写后续列标题: =EDATE(B1,1)并复制到右侧。

In place of Sheet1:$A$1:$ZZ$1000 I would use a dynamic named range.代替Sheet1:$A$1:$ZZ$1000我将使用动态命名范围。 I would use part of that range or an independently declared second named range instead of Sheet1:$1:$1) but that is largely a matter of taste.我会使用该范围的一部分或独立声明的第二个命名范围,而不是Sheet1:$1:$1)但这在很大程度上是一个品味问题。

Now, MAX(Sheet1:$1:$1) returns the largest (latest) date in row 1. MATCH(MAX(Sheet1:$1,$1):Sheet1,$1:$1,0) returns the column of that date and MATCH(MAX(Sheet1:$1,$1):Sheet1,$1:$1,0)-1 would be the column before that, for the month before.现在, MAX(Sheet1:$1:$1)返回第 1 行中最大(最新)的日期。 MATCH(MAX(Sheet1:$1,$1):Sheet1,$1:$1,0)返回该日期的列, MATCH(MAX(Sheet1:$1,$1):Sheet1,$1:$1,0)-1将是前一个月的列。

MATCH($A2,Sheet1:$A,$A,0) returns the row number of a match for A2 in column A of Sheet1. MATCH($A2,Sheet1:$A,$A,0)返回 Sheet1 的 A 列中 A2 匹配项的行号。 Of course, that's where you have the currencies and one of these must be in A2 of the sheet on which you have the formula, which can't be on the same sheet while INDEX specifies all of that sheet.当然,这就是您拥有货币的地方,其中一种货币必须在您拥有公式的工作表的 A2 中,而 INDEX 指定所有该工作表时不能在同一张工作表上。 So, the formula will return the rate from the latest column.因此,该公式将返回最新列的比率。 As you add another column the formula automatically takes the latest.当您添加另一列时,公式会自动采用最新的。

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

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