【问题标题】:VBA Excel autoincrement value on the following sheet, when changing data on active sheet更改活动工作表上的数据时,下工作表上的 VBA Excel 自动增量值
【发布时间】: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 需要访问那个数组来做它的事情。

标签: excel vba


【解决方案1】:

更明显的解决方案是循环遍历单元格

但您可以使用PasteSpecial 选项向单元格添加值:

With Sheets("Cables 1")
    .Range("A1").Copy '<-- change A1 to whatever cell address you'd use to store increment value
    .Range("C50:C51").PasteSpecial Operation:=xlPasteSpecialOperationAdd
    Application.CutCopyMode = False
End With

我将所需增量的值存储在单元格“A1”中,但您可以在单元格地址和增量中更改它

【讨论】:

  • 我不需要 pastespecial 值。我需要这些列的值递增。
  • 不,不幸的是,不是,因为我认为这没有意义,因为您指的是范围(“A1”)。不过谢谢你的帮助:)
  • 如果您尝试过,您会发现它完全符合您的需要。并且该范围 A1 可以设置在您想要的任何位置:您可能想要使用一些 A1000 或任何行索引,以确保它永远不会触及您的实际数据
  • 下次我会试试的。现在我已经找到了解决方案。还是谢谢你
【解决方案2】:

我在这里找到了我的问题的答案:

How do I Increase cell value in Excel using VBA? Error: Type mismatch

所以我的代码应该是这样的:

Public Sub ResizeCiv2()

Dim targetSheet As Worksheet
Dim targetRange As Range
Dim targetShape As Shape

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")
Sheets("Cables 1").Cells(50, 3).Value = Sheets("Cables 1").Cells(50, 3).Value + 1
Sheets("Cables 1").Cells(51, 3).Value = Sheets("Cables 1").Cells(51, 3).Value + 1

End sub

我在工作表中指定特定单元格而不是“范围”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 2017-07-01
    • 2018-06-16
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    相关资源
    最近更新 更多