简体   繁体   English

在VBA中选择单元格范围

[英]select range of cells in VBA

within 1 row of my spreadsheet, there are the dates from 1/1/2017 to 31/12/2017 (row 2).在我的电子表格的 1 行内,有从 1/1/2017 到 31/12/2017(第 2 行)的日期。 Row 3 contains a value for each day in 2017.第 3 行包含 2017 年每一天的值。

Cell A1 and B1 contain dates according to which the row 2 shall be selected - so if A1 is 1/1/2017 and B1 is 31/3/2017, only these months shall be displayed in the chart that si drawn from row 2 and row 3 below (row 2 is x and row 3 is y value).单元格 A1 和 B1 包含应根据其选择第 2 行的日期 - 因此,如果 A1 是 1/1/2017 并且 B1 是 31/3/2017,则从第 2 行和下面的第 3 行(第 2 行是 x,第 3 行是 y 值)。

I thought about either matching or if statements.我考虑过匹配或 if 语句。 Does anybody have an idea how I could achieve that in VBA有没有人知道我如何在 VBA 中实现这一目标

If I'm understanding you correctly, A1 is the start day and B1 is the end day of the range you'd like to select with row 2 being all the days of 2017 starting at A2 and proceeding until the end of the year.如果我理解正确的话,A1 是开始日,B1 是您要选择的范围的结束日,第 2 行是 2017 年的所有日子,从 A2 开始一直持续到年底。 Row 3 are the values associated with each day in row 2. Then to get the range that you want you can use the following:第 3 行是与第 2 行中的每一天相关联的值。然后要获得您想要的范围,您可以使用以下内容:

Dim startIdx As Integer, endIdx As Integer
Set valRng = ActiveSheet.Range("A2:NB2")

startIdx = Application.Match(CLng(Cells(1, 1).Value), valRng, 0)
endIdx = Application.Match(CLng(Cells(1, 2).Value), valRng, 0)

Set rngSel = ActiveSheet.Range(Cells(2, 1).Offset(0, startIdx-1), Cells(2, 1).Offset(0, endIdx-1))
MsgBox ("rngSel=" & rngSel.Cells.Address)

The active ingredient that makes the value conversion work in the match statements is that the 'CLng' call gets you the day portion of the date as per this page .使匹配语句中的价值转换起作用的有效成分是“CLng”调用为您提供日期的日期部分,如本页所述

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

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