gpt4 book ai didi

excel - 在 Range 变量中连接 For 循环计数器

转载 作者:行者123 更新时间:2023-12-04 22:21:35 26 4
gpt4 key购买 nike

我需要帮助将 myV1 = p1.Value 与 for 循环中的变量“j”连接起来
像 myV1="p"& j & ".value"。
它给出类型不匹配错误
请帮忙

Sub MyPrg()

Dim myVol As Range
Dim p1 As Range
Dim p2 As Range
Dim p3 As Range
Dim myV1 As Long
Dim i, j As Integer

Set myVol = Range("G3")

Set p1 = myVol.Offset(1, -1)
Set p2 = myVol.Offset(1, 0)
Set p3 = myVol.Offset(1, 1)

If myVol = 1000000 Then

p1.Value = myVol.Value * 40 / 100
p2.Value = myVol.Value * 40 / 100
p3.Value = myVol.Value * 20 / 100

For j = 1 To 3

myV1 = p1.Value

最佳答案

使用数组:

Sub MyPrg()

Dim myVol As Range
Dim p(1 to 3) As Range
Dim myV1 As Long
Dim I As Integer, j As Integer

Set myVol = Range("G3")

Set p(1) = myVol.Offset(1, -1)
Set p(2) = myVol.Offset(1, 0)
Set p(3) = myVol.Offset(1, 1)

If myVol = 1000000 Then

p(1).Value = myVol.Value * 40 / 100
p(2).Value = myVol.Value * 40 / 100
p(3).Value = myVol.Value * 20 / 100

For j = 1 To 3

myV1 = p(j).Value

关于excel - 在 Range 变量中连接 For 循环计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62679279/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com