简体   繁体   English

在循环vba excel上调用sub时更新字符串

[英]update string when calling sub on loop vba excel

Bonjour! 你好!

I have a loop on my project, and within the loop I call a sub (Stcount) to count the column number of an element. 我在项目上有一个循环,在循环中我调用了一个子(Stcount)来计算元素的列数。 This sub feeds on a string (ststring) that changes with each loop. 该子提供一个随每个循环而变化的字符串(ststring)。 However, the sub (Stcount), does not appear to recognize the change in the string (ststring). 但是,子(Stcount)似乎无法识别字符串(ststring)中的更改。

First, I start by defining some variables I will use for this sequences 首先,我先定义一些将用于此序列的变量

Public counterSt As Integer
Public ststring As String
Public rng As Range
Public rng2 As Range

In this sub, I define the strings that will be the basis for my sub 在此子代码中,我定义了将作为子代码基础的字符串

Sub test()

Dim Stx(9) As String
    Stx(1) = "Stx1"
    Stx(2) = "Stx2"
    Stx(3) = "Stx3"
    Stx(4) = "Stx4"
    Stx(5) = "Stx5"
    Stx(6) = "Stx6"
    Stx(7) = "Stx7"
    Stx(8) = "Stx9"
    Stx(9) = "Stx10"

Set rng2 = Range("G10")
For i = 1 To 9
    ststring = "ist_" & Stx(i)
    Call StCount
    rng2.Value = counterSt
    Set rng2 = rng2.Offset(1, 0)

Next i

End Sub

When I call StCount, it will update the value for counterSt. 当我调用StCount时,它将更新counterSt的值。 For the first string, it works fine. 对于第一个字符串,它可以正常工作。 But when it loops, it does not seem to recognize there is a new string that it should use (ststring). 但是当它循环时,似乎无法识别应该使用的新字符串(ststring)。 And thus, it returns the same value each time! 因此,每次都返回相同的值!

Sub StCount()
Set rep = ActiveWorkbook.Sheets("Sheet2")
Set sh1 = ActiveWorkbook.Sheets("Sheet1")


    Dim trng As Range
    Set trng = rep.Range("A4:HV4")

    For Each rng In trng
        If rng.Value = ststring Then
        counterSt = Range(rng, rng.End(xlToLeft)).Columns.count
        End If
    Next rng
End Sub

As check, I tried to see if Ststring was updated correctly (it was). 作为检查,我试图查看Ststring是否已正确更新。 I also tried to update the value of ststring manually and call StCount different times in the same sub with different ststrings (it also worked). 我还尝试手动更新ststring的值,并在具有不同ststrings的同一个子程序中多次调用StCount(它也起作用)。 Thus, I believe the problem is with the loop. 因此,我相信问题出在循环上。

Ideas? 有想法吗?

Your code works fine for me but I don't see any need for public variables. 您的代码对我来说很好用,但我认为不需要任何公共变量。 I'd suggest something more like this: 我建议更像这样的东西:

Sub test()
    Dim rng2                  As Range
    Dim i                     As Long
    Dim Stx(9)                As String

    Stx(1) = "Stx1"
    Stx(2) = "Stx2"
    Stx(3) = "Stx3"
    Stx(4) = "Stx4"
    Stx(5) = "Stx5"
    Stx(6) = "Stx6"
    Stx(7) = "Stx7"
    Stx(8) = "Stx9"
    Stx(9) = "Stx10"

    Set rng2 = Range("G10")
    For i = 1 To 9
        rng2.Value = StCount("ist_" & Stx(i))
        Set rng2 = rng2.Offset(1, 0)

    Next i

End Sub
Function StCount(sTest As String)
    Dim rng                   As Range
    Dim trng                  As Range
    Dim rep                   As Worksheet
    Dim sh1                   As Worksheet
    Set rep = ActiveWorkbook.Sheets("Sheet2")
    Set sh1 = ActiveWorkbook.Sheets("Sheet1")


    Set trng = rep.Range("A4:HV4")

    For Each rng In trng
        If rng.Value = sTest Then
            StCount = Range(rng, rng.End(xlToLeft)).Columns.Count
            Exit For
        End If
    Next rng
End Function

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

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