gpt4 book ai didi

excel - 数组不考虑在 VBA 中创建的字符串

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

出于某种原因,从特定范围创建的动态字符串未在数组 for 循环中考虑。下面是我的代码:

Option Explicit
Dim AccrualFile As Workbook, AccrualSht As Worksheet
Dim AccrualFilePath As String
Dim UniqueNames As String, UniqueAccNames As String
Dim Lrows As Long, Lcols As Long


Sub Segregation()

AccrualFilePath = Application.GetOpenFilename(Title:="Please select Accrual Statement")

Set AccrualFile = Workbooks.Open(AccrualFilePath)
Set AccrualSht = AccrualFile.Sheets(1)

Lrows = AccrualSht.Range("B" & Rows.Count).End(xlUp).Row
UniqueNames = Application.WorksheetFunction.TextJoin(""""", """"", True, Application.WorksheetFunction.Unique(AccrualSht.Range("B2:B" & Lrows)))
UniqueAccNames = """""" & UniqueNames & """"""

Dim i As Long
Dim myarr() As Variant

myarr = Array(UniqueAccNames)



For i = LBound(myarr) To UBound(myarr)
Debug.Print myarr(i)
Next

End Sub
感谢你的帮助!!
谢谢你。

最佳答案

WorksheetFunction.Unique传递 Range 时返回 (2D)1 数组.

myarr = WorksheetFunction.Unique(AccrualSht.Range("B2:B" & Lrows))

Dim i As Long, j As Long
For i = LBound(myarr, 1) to Ubound(myarr, 1)
For j = Lbound(myarr, 2) to Ubound(myarr, 2)
Debug.Print myarr(i, j)
Next
Next
1 除了一些异常(exception)情况,包括:
  • 当输出是单个元素时,它返回一个一维数组。
  • 关于excel - 数组不考虑在 VBA 中创建的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68836819/

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