简体   繁体   English

具有定义范围的合并单元(运行时错误1004)

[英]MergeCells with defined range (Run-time error 1004)

I'm trying to merge cells based on two cell values that change according to user input but am having probems with the following code: 我正在尝试根据两个单元格值合并单元格,这两个单元格值会根据用户输入而变化,但是具有以下代码的探针:

    SafB = 5 + 2 * Sheets("LinksTable").Range("X2").Value - 2 * Sheets("LinksTable").Range("W2").Value
    SafE = 5 + 2 * Sheets("LinksTable").Range("X2").Value - 1

    Set SafRB = Sheets("Report").Range("B1").Offset(SafB - 1, 0)
    Set SafRE = Sheets("Report").Range("C1").Offset(SafE - 1, 0)

    Sheets("Report").Range("SafRB:SafRE").MergeCells = True

For example, cells W2 and X2 will have values that change depending on how many metrics are selected for a given lever, in this case "Safety". 例如,单元格W2和X2的值将根据为给定杠杆选择的度量标准而改变,在这种情况下为“安全”。 So if the user selects 2 metrics for the Safety lever, W2 = 2 (number of metrics for that lever) and X2 = 2 (cumulative number of metrics in the report; safety being the first lever, the cumulative equals the total in this case). 因此,如果用户为“安全”杠杆选择2个度量标准,则W2 = 2(该杠杆的度量标准数量)和X2 = 2(报告中的累积度量标准数量;安全性是第一个杠杆,在这种情况下,累积等于总和)。

Given the above code, SafB=5 and SafE=8. 给定上面的代码,SafB = 5和SafE = 8。 Therefore the cells I need to merge are B5:C8. 因此,我需要合并的单元格是B5:C8。

However, I get the following error message whenever I run the above code: 但是,每当我运行上面的代码时,我都会收到以下错误消息:

"Run-time error '1004': Application-defined or object-defined error". “运行时错误'1004':应用程序定义的错误或对象定义的错误”。

Any help would be greatly appreciated! 任何帮助将不胜感激!

You're sending a string literal, "SafRB:SafRE" as a range argument. 您正在发送字符串文字“ SafRB:SafRE”作为范围参数。 This error means there is no such range defined on your sheet or workbook. 此错误意味着您的工作表或工作簿上没有定义此范围。

I think this should work. 我认为这应该有效。 use your range variables as arguments to the Range method, like so: 使用范围变量作为Range方法的参数,如下所示:

Sheets("Report").Range(SafRB, SafRE).Merge

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

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