如何在For循环中为每个结果分配一个新变量?

huangapple go评论70阅读模式
英文:

How do I assign a new variable to each result in a For loop?

问题

我正在尝试搜索一列(H列)的数值,如果存在数值,就将列C中的数值返回并赋给一个变量。

我想要使用For循环来搜索结果(该列大部分为空)。在有结果的地方,我想要为每个结果创建一个新变量,以供以后使用。结果数永远不会超过5个。

这是我尝试过的,但是组合变量名对我来说不起作用。

Dim xDat1 As String
Dim xDat2 As String
Dim xDat3 As String
Dim xDat4 As String
Dim xDat5 As String

d = 1
For i = 186 To 235
    If Not wsElog2.Range("H" & i) Is Nothing Then
        '从列C中获取结果,并赋值给一个变量(xDat + d的值)。
        '上面的代码显示红色错误 - 不确定如何组合xDat和d。
    End If
    d = d + 1
Next i

希望这可以帮助你解决问题。如果需要进一步的帮助,请随时提问。

英文:

I am trying to search a column (H) for values, and if there are values, return the value in column C and assign it to a variable.

I would like to use a For loop to search for results (the column is mostly blank). Where there is a result, I want to create a new variable for each one, for later use. There will never be more than 5 results.
This is what I've tried, but putting together the variable name is not working for me.

Dim xDat1 As String
Dim XDat2 As String
Dim xDat3 As String
Dim xDat4 As String
Dim xDat5 As String

d = 1
For i = 186 To 235
If Not wsElog2.Range("H" & i) Is Nothing Then
    'get result from column C, and assign to a variable (xDat + the d value).
    xDat & d = wsElog2.Range("C" & i).Text
    'the above is showing red error - not sure how to put xDat and d together
End If
d = d + 1
Next i

答案1

得分: 2

你的主要问题是这一行:

If Not wsElog2.Range("H" & i) Is Nothing Then

没有检查相应的单元格是否为空... Is Nothing 应该只用于对象,而你尝试检查对象(一个 Range)的值,它永远不会是 Nothing

第二个问题是你不能通过连接字符串来构建变量名...

所以,请测试下面适应的代码。它将使用一个 Scripting.Dictionary,可以保存你需要的多个变量:

Sub testUseVariables()
   Dim wsElog2 As Worksheet, i As Long, dict As Object

    Set wsElog2 = ActiveSheet '请使用必要的工作表

    Set dict = CreateObject("Scripting.Dictionary")
    For i = 186 To 235
        If wsElog2.Range("H" & i).Value <> "" Then
            dict("H" & i) = wsElog2.Range("C" & i).Text '我只使用了 Text,因为你要求这样做。
                                                        '但是,如果单元格/列没有自动调整宽度,它只会返回可见的部分!
        End If
    Next i
    '你可以以以下方式测试结果:
    If dict.Count > 0 Then
        Debug.Print Join(dict.Keys, "|")     '只是为了查看从哪里来的(在 H:H 中)
        Debug.Print Join(dict.Items, "|")    '返回的结果
        Debug.Print dict.Keys()(0), dict.Keys()(1) '返回特定键号的项目的方式
    End If
End Sub

下一个版本可以模拟构建变量的方式,但现在,作为字符串,它可以工作:

Sub testUseVars()
   Dim wsElog2 As Worksheet, d As Long, i As Long, dict As Object
   Const xD As String = "xDat"
   
    Set wsElog2 = ActiveSheet '请使用必要的工作表
    
    Set dict = CreateObject("Scripting.Dictionary")
    For i = 186 To 235
        If wsElog2.Range("H" & i).Value <> "" Then
            d = d + 1
            dict(xD & d) = wsElog2.Range("C" & i).Text '我建议使用 Value 而不是 Text...
        End If
    Next i

    '你可以以以下方式测试结果:
    If dict.Count > 0 Then
        Debug.Print Join(dict.Keys, "|")     '只是为了查看从哪里来的(在 H:H 中)
        Debug.Print Join(dict.Items, "|")    '返回的结果
        Debug.Print dict("xDat1"), dict("xDat2") '返回特定键的项目的方式
    End If
End Sub
英文:

Your main problem is the line

   If Not wsElog2.Range(&quot;H&quot; &amp; i) Is Nothing Then

which does not check if the respective cell is empty... Is Nothing should be used only for objects and you try checking the value of an object (a Range), which will always NOT be Nothing.

The second problem is that you cannot build the variable name by concatenation...

So, please, test the next adapted code. It will use a Scripting.Dictionar able to keep as many variables you need:

Sub testUseVariables()
   Dim wsElog2 As Worksheet, i As Long, dict As Object

    Set wsElog2 = ActiveSheet &#39;please, use the necessary sheet

    Set dict = CreateObject(&quot;Scripting.Dictionary&quot;)
    For i = 186 To 235
        If wsElog2.Range(&quot;H&quot; &amp; i).Value &lt;&gt; &quot;&quot; Then
            dict(&quot;H&quot; &amp; i) = wsElog2.Range(&quot;C&quot; &amp; i).text &#39;I used Text only because you wonted that.
                                                        &#39;but, if the cell/column is not AutoFit, it will return only what you can see!
        End If
    Next i
    &#39;you can test the result in the next way:
    If dict.count &gt; 0 Then
        Debug.Print Join(dict.keys, &quot;|&quot;)     &#39;just to see where from the it comes (in H:H)
        Debug.Print Join(dict.Items, &quot;|&quot;)    &#39;the returned result
        Debug.Print dict.keys()(0), dict.keys()(1) &#39;a way of returning the item of a specific key number
    End If
End Sub

The next version is able to simulate the way of building the variables, but now, being a string, it works:

Sub testUseVars()
   Dim wsElog2 As Worksheet, d As Long, i As Long, dict As Object
   Const xD As String = &quot;xDat&quot;
   
    Set wsElog2 = ActiveSheet &#39;please, use the necessary sheet
    
    Set dict = CreateObject(&quot;Scripting.Dictionary&quot;)
    For i = 186 To 235
        If wsElog2.Range(&quot;H&quot; &amp; i).Value &lt;&gt; &quot;&quot; Then
            d = d + 1
            dict(xD &amp; d) = wsElog2.Range(&quot;C&quot; &amp; i).text &#39;I would suggest using Value instead of Text...
        End If
    Next i

    &#39;you can test the result in the next way:
    If dict.count &gt; 0 Then
        Debug.Print Join(dict.keys, &quot;|&quot;)     &#39;just to see where from the it comes (in H:H)
        Debug.Print Join(dict.Items, &quot;|&quot;)    &#39;the returned result
        Debug.Print dict(&quot;xDat1&quot;), dict(&quot;xDat2&quot;) &#39;a way of returning the item for a specific key
    End If
End Sub

答案2

得分: 1

使用数组代替许多编号变量

快速修复 (For...Next)

Sub Test()
    
    Dim rCount As Long: rCount = 235 - 186 + 1
    
    Dim dArr() As String: ReDim dArr(1 To rCount)
    
    Dim i As Long, dCount As Long
    
    For i = 186 To 235
        If Len(CStr(wsELog2.Range("H" & i).Value)) > 0 Then
            dCount = dCount + 1
            ' 从列C获取结果,并赋给一个变量(xDat + dCount的值)。
            dArr(dCount) = CStr(wsELog2.Range("C" & i).Value)
            ' 上述显示红色错误 - 不确定如何将xDat和dCount组合在一起
        End If
    Next i

    ' 检查所有查找单元格是否为空。
    If dCount = 0 Then
        MsgBox "所有查找单元格均为空。", vbCritical
        Exit Sub
    End If

    ' 检查是否找到了空白。
    If dCount < rCount Then ' 不是所有查找单元格都为空
        ReDim Preserve dArr(1 To dCount) ' 调整数组大小
    'Else ' 所有查找单元格都不为空;不执行任何操作
    End If

    ' 继续使用数组...

    ' 在即时窗口中返回逗号分隔的结果(Ctrl+G)。
    Debug.Print Join(dArr, ", ")

    Dim d As Long

    ' 遍历结果,并在即时窗口中每行返回一个。
    For d = 1 To dCount
        Debug.Print dArr(d)
    Next d

End Sub

更详细的示例 (For Each...Next)

Sub MatchesToArray()

    ' 引用工作簿和工作表。
    Dim wb As Workbook: Set wb = ThisWorkbook ' 包含此代码的工作簿
    Dim ws As Worksheet: Set ws = wb.Sheets("ELog2") ' 调整!

    ' 引用(相同大小的)单列范围。
    Dim lrg As Range: Set lrg = ws.Range("H186:H235") ' 查找范围
    Dim rrg As Range: Set rrg = lrg.EntireRow.Columns("C") ' 返回范围

    ' 将行数写入变量。
    Dim rCount As Long: rCount = rrg.Rows.Count ' 或 lrg.Rows.Count

    ' 定义与行数相同大小的一维基于一的数组。
    Dim dArr() As Variant: ReDim dArr(1 To rCount)

    Dim lCell As Range, r As Long, dCount As Long

    ' 遍历查找范围的单元格。
    For Each lCell In lrg.Cells
        r = r + 1 ' 当前(查找/返回)行索引
        ' 检查查找单元格是否不为空。
        If Len(CStr(lCell.Value)) > 0 Then ' 查找单元格不为空
            ' 将返回范围的当前行索引的值
            ' 写入数组的下一个(当前)元素。
            dCount = dCount + 1
            dArr(dCount) = rrg.Cells(r).Value
        'Else ' 查找单元格为空;不执行任何操作
        End If
    Next lCell

    ' 检查所有查找单元格是否为空。
    If dCount = 0 Then
        MsgBox "所有查找单元格均为空。", vbCritical
        Exit Sub
    End If

    ' 检查是否找到了空白。
    If dCount < rCount Then ' 不是所有查找单元格都为空
        ReDim Preserve dArr(1 To dCount) ' 调整数组大小
    'Else ' 所有查找单元格都不为空;不执行任何操作
    End If

    ' 继续使用数组...

    ' 在即时窗口中返回逗号分隔的结果(Ctrl+G)。
    Debug.Print Join(dArr, ", ")

    Dim d As Long

    ' 遍历结果,并在即时窗口中每行返回一个。
    For d = 1 To dCount
        Debug.Print dArr(d)
    Next d

End Sub

结果

1, 5, 7, 10, 13
1
5
7
10
13
英文:

Use an Array Instead of Many Numbered Variables

A Quick Fix (For...Next)

<!-- language: lang-vb -->

Sub Test()
Dim rCount As Long: rCount = 235 - 186 + 1
Dim dArr() As String: ReDim dArr(1 To rCount)
Dim i As Long, dCount As Long
For i = 186 To 235
If Len(CStr(wsELog2.Range(&quot;H&quot; &amp; i).Value)) &gt; 0 Then
dCount = dCount + 1
&#39;get result from column C, and assign to a variable (xDat + the dCount value).
dArr(dCount) = CStr(wsELog2.Range(&quot;C&quot; &amp; i).Value)
&#39;the above is showing red error - not sure how to put xDat and dCount together
End If
Next i
&#39; Check if all lookup cells were blank.
If dCount = 0 Then
MsgBox &quot;All lookup cells were blank.&quot;, vbCritical
Exit Sub
End If
&#39; Check if any blanks were found.
If dCount &lt; rCount Then &#39; not all lookup cells were not blank
ReDim Preserve dArr(1 To dCount) &#39; resize the array
&#39;Else &#39; all lookup cells were not blank; do nothing
End If
&#39; Continue using the array...
&#39; Return the comma-separated results in the Immediate window (Ctrl+G).
Debug.Print Join(dArr, &quot;, &quot;)
Dim d As Long
&#39; Loop through the results and return one per row
&#39; in the Immediate window.
For d = 1 To dCount
Debug.Print dArr(d)
Next d
End Sub

In More Detail: A Complete Example (For Each...Next)

如何在For循环中为每个结果分配一个新变量?

<!-- language: lang-vb -->

Sub MatchesToArray()
&#39; Reference the workbook and the worksheet.
Dim wb As Workbook: Set wb = ThisWorkbook &#39; workbook containing this code
Dim ws As Worksheet: Set ws = wb.Sheets(&quot;ELog2&quot;) &#39; adjust!
&#39; Reference the (same-sized) single-column ranges.
Dim lrg As Range: Set lrg = ws.Range(&quot;H186:H235&quot;) &#39; Lookup Range
Dim rrg As Range: Set rrg = lrg.EntireRow.Columns(&quot;C&quot;) &#39; Return Range
&#39; Write the number of rows to a variable.
Dim rCount As Long: rCount = rrg.Rows.Count &#39; or lrg.Rows.Count
&#39; Define a 1D one-based array of the same size as there are rows.
Dim dArr() As Variant: ReDim dArr(1 To rCount)
Dim lCell As Range, r As Long, dCount As Long
&#39; Loop through the cells of the lookup range.
For Each lCell In lrg.Cells
r = r + 1 &#39; current (lookup/return) row index
&#39; Check if the lookup cell is not blank.
If Len(CStr(lCell.Value)) &gt; 0 Then &#39; the lookup cell is not blank
&#39; Write the value from the current row index of the return range
&#39; to the next (current) element of the array.
dCount = dCount + 1
dArr(dCount) = rrg.Cells(r).Value
&#39;Else &#39; the lookup cell is blank; do nothing
End If
Next lCell
&#39; Check if all lookup cells were blank.
If dCount = 0 Then
MsgBox &quot;All lookup cells were blank.&quot;, vbCritical
Exit Sub
End If
&#39; Check if any blanks were found.
If dCount &lt; rCount Then &#39; not all lookup cells were not blank
ReDim Preserve dArr(1 To dCount) &#39; resize the array
&#39;Else &#39; all lookup cells were not blank; do nothing
End If
&#39; Continue using the array...
&#39; Return the comma-separated results in the Immediate window (Ctrl+G).
Debug.Print Join(dArr, &quot;, &quot;)
Dim d As Long
&#39; Loop through the results and return one per row
&#39; in the Immediate window.
For d = 1 To dCount
Debug.Print dArr(d)
Next d
End Sub

Results

1, 5, 7, 10, 13
1 
5 
7 
10 
13 

答案3

得分: 0

以下是翻译好的部分:

使用数组的方法:

Dim xDats(5) As String
Dim d = 1
For i = 186 To 235
    If Not wsElog2.Range("H" & i) Is Nothing Then
        '从列C获取结果,并赋值给变量(xDat + d的值)。
        xDats(d) = wsElog2.Range("C" & i).Text
        '上面显示红色错误 - 不确定如何将xDat和d组合在一起
        d += 1
    End If
Next i

使用List(Of String)的方法:

Dim xDats As New List(Of String)
For i = 186 To 235
    If Not wsElog2.Range("H" & i) Is Nothing Then
        '从列C获取结果,并添加到变量中。
        xDats.Add(wsElog2.Range("C" & i).Text)
        '上面显示红色错误 - 不确定如何将xDat和d组合在一起
    End If
Next i
英文:

The simple answer, if you know there are no more than 5, is to use an array, like this:

Dim xDats(5) As String
Dim d = 1
For i = 186 To 235
If Not wsElog2.Range(&quot;H&quot; &amp; i) Is Nothing Then
&#39;get result from column C, and assign to a variable (xDat + the d value).
xDats(d) = wsElog2.Range(&quot;C&quot; &amp; i).Text
&#39;the above is showing red error - not sure how to put xDat and d together
d += 1
End If
Next i

However, it is probably better to use a List(Of String) as this would give you slightly cleaner code and will eliminate an error in the situation where there are more than 5.

Dim xDats As New List(Of String)
For i = 186 To 235
If Not wsElog2.Range(&quot;H&quot; &amp; i) Is Nothing Then
&#39;get result from column C, and assign to a variable (xDat + the d value).
xDats.Add(wsElog2.Range(&quot;C&quot; &amp; i).Text)
&#39;the above is showing red error - not sure how to put xDat and d together
End If
Next i

huangapple
  • 本文由 发表于 2023年6月22日 14:04:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定