热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

参考范围不同的表-Referencearangefromadifferentsheet

Imtryingtocreateaprogramthatoptimizeshistogrambinwidthbeforethatthough,Ineedhelp

I'm trying to create a program that optimizes histogram bin width...before that though, I need help with a seemingly simple task - setting a Range mentioned in a cell into a VBA array.

我正在尝试创建一个程序来优化直方图栏的宽度……在此之前,我需要帮助完成一个看似简单的任务——将单元格中提到的范围设置为VBA数组。

I'd like the data for the histogram to be on any sheet, in this case 'Data'!B4:M12. This is mentioned in D4 of the sheet I want the histogram to appear on. I keep getting an error when I run my code though, even after changing it multiple times. There's clearly a syntax error that I don't know how to handle.

我希望直方图的数据在任何表格上,在这种情况下是“数据”!B4:M12。这是在表格D4中提到的我想要直方图出现在上面。当我运行我的代码时,即使在多次修改之后,我仍然会得到一个错误。很明显,有一个语法错误我不知道如何处理。

Any assistance would be much appreciated!

如有任何帮助,我们将不胜感激!

Sub Histogram_Shimazaki_Shinomoto()

Dim data_range As String, min_bins As Integer, max_bins As Integer
Dim Data()
Dim x_min As Double, x_max As Double

data_range = Cells(4, 4)    ' data range
min_bins = Cells(5, 4)      ' min # of bins
max_bins = Cells(6, 4)      ' max # of bins

Set Data = Range(data_range)

x_min = WorksheetFunction.Min(Data)

MsgBox x_min

End Sub

1 个解决方案

#1


0  

You are trying to set an array of variant equal to a Range -- but that isn't possible since you can't assign to an array. You could assign a range to a simple Variant (or to a Range variable). You could change

您正在尝试设置一个变量数组,它等于一个范围——但是这是不可能的,因为您不能为一个数组赋值。您可以将范围分配给一个简单的变量(或一个范围变量)。你可以改变

Dim Data()

to

Dim Data as Variant

Note the absence of parenthesis. Also, as a stylistic point I think that it is good to be explicit about the type, even though Variant is the default.

注意没有括号。另外,从风格上来说,我认为明确类型是好的,尽管变体是默认的。

This might be enough for your code to work, although if all you want is the minimum value in the range, you could change

这可能足以让您的代码工作,尽管如果您想要的是范围内的最小值,您可以更改。

Set Data = Range(data_range)

to

Data = Range(data_range).Value

If the sheet "Data" isn't the active sheet and data_range contains "B4:M12" then you would need to use

如果表“Data”不是活动表,并且data_range包含“B4:M12”,那么您需要使用它

Data = Sheets("Data").Range(data_range).Value

since Range returns a range on the active sheet unless explicitly qualified by a reference to the sheet.

因为范围返回活动表上的范围,除非显式地由对表的引用限定。


推荐阅读
author-avatar
苏佡挖图组糖最棒
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有