简体   繁体   English

VBA绘制动态范围

[英]VBA to plot dynamic range

I have an excel 2010 macro enabled workbook. 我有一台启用了Excel 2010宏的工作簿。 The workbook has several sheets. 该工作簿有几张纸。 The first sheet has a macro assigned button that imports a csv file. 第一页有一个分配了宏的按钮,用于导入一个csv文件。 The csv file loads into an existing sheet named “Data”. csv文件将加载到名为“数据”的现有工作表中。 The imported csv file loads into columns with row 1 acting as a header for the data below. 导入的csv文件加载到第1行作为以下数据的标题的列中。 For example, A1 is named “Time” and cell A2 down is the time data. 例如,A1命名为“时间”,而单元格A2 down是时间数据。 Cell B1 is named “Temperature” and cell B2 down is the temperature data related to each time reading. 单元格B1被命名为“温度”,而单元格B2 down是与每次读取相关的温度数据。 The workbook has a third existing sheet in the workbook named “Temperature Plot”. 该工作簿在工作簿中已有第三张工作表,称为“温度曲线图”。 I would like to automatically chart Temperature versus Time on this sheet by referencing the values from the “Data” sheet. 我想通过参考“数据”表中的值自动在此表上绘制温度与时间的图表。 (Time is x-axis and Temp is y-axis) (时间是x轴,温度是y轴)

The problem is that the imported csv file that loads into the “Data” sheet will have a different number of rows each time. 问题在于,加载到“数据”表中的导入的csv文件每次都有不同的行数。 Sometimes the imported csv file may have 25 rows of data and other times 100,000 rows of data. 有时,导入的csv文件可能具有25行数据,而有时可能有100,000行数据。 Lets say I import a csv with 1000 rows of data, the workbook also plots the blank cells since my chart is pre-set to capture the entire column. 可以说我导入了具有1000行数据的csv,由于预设了图表以捕获整个列,因此工作簿还绘制了空白单元格。 As a result, the chart data is squeezed to the left. 结果,图表数据被压缩到左侧。 If I pre-set the number of rows to be plotted to 1000 then the next imported csv file with more than 1000 rows will not plot beyond 1000 rows. 如果我将要绘制的行数预先设置为1000,则下一个导入的具有超过1000行的csv文件不会绘制超过1000行。

I've tried rendering cell with #NA formula to avoid plotting empty cells but this solution creates a massive file. 我尝试使用#NA公式渲染单元格以避免绘制空单元格,但是此解决方案会创建一个大文件。

Is there a way to create a macro that generates a line graph of the data: 有没有一种方法可以创建一个宏来生成数据的折线图:

y-axis = (B2 to the last B cell with a number) versus x-axis = (A2 to the last A cell with a number) y轴=(B2到最后一个B单元格带数字)vs x轴=(A2到最后一个A单元格带数字)

So that each time the workbook is opened and a new csv file (regardless of how many rows) is imported the graph plots the cells with numbers and does not plot blank cells? 这样,每次打开工作簿并导入新的csv文件(无论有多少行)时,该图都会用数字绘制单元格,而不会绘制空白单元格?

Any assistance would be greatly appreciated. 任何帮助将不胜感激。 Thank you in advance! 先感谢您!

By using a dynamic range, you can make a named range that expands and contracts based in the amount of data that you have. 通过使用动态范围,您可以创建一个命名范围,该范围根据您拥有的数据量进行扩展和收缩。 You would make two named ranges, one for x and one for y, and then plot the ranges. 您将创建两个命名范围,一个用于x,一个用于y,然后绘制范围。 Not in from of PC right now, but this would be done (dynamic range) with the offset command. 现在不在PC机中,但是可以通过offset命令完成(动态范围)。

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

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