通过VBA子范围

我保持getitng这个错误“types错配”

我希望能够通过传递一个范围和一个值,我想显示在我的spreed工作表中的单元格。

任何人都可以指出我要去哪里错了吗?

Sub Layout() Call Create_Box("A1:A2", 10) End Sub Sub Create_Box(R As Range, V As String) Dim box As Object Set box = Range(R) With box .Merge .Value = Box_value .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .Border.Style = continous .Border.Color = black .Border.Weight = xlThick End With End Sub` 

你传入的参数不是一个Range对象 ; 只是表示范围对象的本地地址属性的string。

 Sub Layout() with activesheet '<-set this explicitly to something like With Sheets("Sheet1") Call Create_Box(.range("A1:A2"), 10) end with End Sub Sub Create_Box(R As Range, V As String) With R .Merge .Value = Box_value .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .Border.Style = xlContinuous .Border.Color = 0 .Border.Weight = xlThick End With End Sub 

将其作为范围对象传递。

 Sub Layout() Dim r1 As Range Dim ws As Excel.Worksheet Set ws = Application.ActiveSheet Set r1 = ws.Range("A1:A2") Call Create_Box(r1, "10") End Sub Sub Create_Box(R As Range, V As String) Dim box As Object Set box = Range(R.Address) With box .Merge .Value = Box_value .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .Border.Style = continous .Border.Color = black .Border.Weight = xlThick End With 

结束小组

这应该做到这一点:

 Sub Create_Box(R As String, V As String)