【发布时间】:2020-03-31 23:03:41
【问题描述】:
我希望在活动表上执行代码后,我在下一张表上的值增加 1。
情况如下: 公共子 ResizeCiv2()
Dim targetSheet As Worksheet
Dim targetRange As Range
Dim targetShape As Shape
' Define the sheet that has the pictures
Set targetSheet = ThisWorkbook.ActiveSheet
' Define the range the images is going to fit
Set targetRange = targetSheet.Range("C3:K24")
' Loop through each Shape in Sheet
For Each targetShape In targetSheet.Shapes
' Check "picture" word in name
If targetShape.Name Like "*Picture*" Then
' Call the resize function
SizeToRange targetShape, targetRange
End If
Next targetShape
Call CivBox
Call Divider
ActiveSheet.Range("M15").Value = Range("D52")
With Sheets("Cables 1").Range("C50:C51")
.Value = .Value + 1
End With
End Sub
现在调试器会突出显示 .Value = .Value + 1 说,这是类型不匹配。
这是我的带有一张图片的活动表。 根据上面的代码,我偶尔会添加另一张图片。如果是这样,我需要在下表中将值递增 1...
如果我不能使用With 语句或Sheet("Cables 1").Range("C50").Value = Range("C50").Value + 1 来执行此操作,是否有任何选项可以让它运行?
可能与此查询有关: How can I simplify or loop excel vba code for data exchange in different sheets?
更新:
我在这里找到了一些解决方案:
Increment Cell Values in a Range by 1 with VBA Excel
并尝试使用我的代码...
Public Sub ResizeCiv2()
Dim targetSheet As Worksheet
Dim targetRange As Range
Dim targetShape As Shape
Dim rng As Range
Set rng = Sheets("Cables 1").Range("C50:C51")
' Define the sheet that has the pictures
Set targetSheet = ThisWorkbook.ActiveSheet
' Define the range the images is going to fit
Set targetRange = targetSheet.Range("C3:K24")
' Loop through each Shape in Sheet
For Each targetShape In targetSheet.Shapes
' Check "picture" word in name
If targetShape.Name Like "*Picture*" Then
' Call the resize function
SizeToRange targetShape, targetRange
End If
Next targetShape
Call CivBox
Call Divider
ActiveSheet.Range("M15").Value = Range("D52")
Dim myCell As Range
With rng
myCell = myCell + 1
End With
End Sub
现在,调试器说: 对象变量或未设置变量
当我用Value = .Value + 1 取代 myCell 行时,我收到错误:
类型不匹配
【问题讨论】:
-
With Sheets("Cables 1").Range("C50:C51")是两个单元格,不能使用数组,只能修改单个单元格的值。 -
@Warcupine,实际上你可以在没有数组的情况下修改多个单元格范围的值(见我的回答)
-
@HTH 那不是范围对象内的数组吗?
-
@Warcupine,这是一个
Range参考。而.Range("C50:C51").Value将是一个数组 -
@hth 但是不需要 pastespecial 需要访问那个数组来做它的事情。