小编Cyr*_*ril的帖子

循环遍历变量数组时是否不能将If语句与多个条件(AND / OR)一起使用?

背景:

在使用某些变体数组将数据根据条件分类到多个位置时,我注意到每次使用if语句将多个条件标记为false。这是用来创建字典的,尽管它从未涉及字典方面,因为仅在变量数组中循环时,响应错误。

我将它们移到两个单独的if语句中,并且一切正常。


题:

为什么在遍历变量数组中的数据时无法使用多条件if语句?


有问题的代码:

生成变体数组的通用代码:

Public ex_arr As Variant, ex_lr As Long, ex_lc As Long
Public dc As Scripting.Dictionary 

Private Sub capture_export_array()
    With Sheets("export")
        ex_lc = .Cells(1, .Columns.Count).End(xlToLeft).Column
        ex_lr = .Cells(.Rows.Count, ex_lc).End(xlUp).Row
        ex_arr = .Range(.Cells(1, 1), .Cells(ex_lr, ex_lc)).Value
    End With
End Sub
Run Code Online (Sandbox Code Playgroud)

导致False条件的代码(立即窗口打印= 0):

Private Sub find_unique_items()
    Set dc = New Scripting.Dictionary
    Dim i As Long
    For i = LBound(ex_arr) To UBound(ex_arr)
        If InStr(ex_arr(i, ex_lc), "CriteriaA") And InStr(ex_arr(i, 4), "CriteriaB") Then dc(ex_arr(i, 2)) = ex_arr(i, 3) …
Run Code Online (Sandbox Code Playgroud)

excel vba loops if-statement variant-array

6
推荐指数
1
解决办法
100
查看次数

在不格式化单元格的情况下在Debug.Print中返回5位数字的日期

背景:

我想从格式化显示日期的单元格中获取5位数字的日期。5位数字的日期只能是即时窗口(通过debug.print)。

我的测试结果在“ 脚本”部分(底部)中。

I have a feeling that the answer will involve datediff()+2 based on the testing I did, but I can't figure out why that +2 is needed and don't want to just add that in if it's wrong in the future.


Issue:

I don't seem to be able to display the correct 5-digit date (as displayed with "general" format) by means of Debug.Print.


Question:

How do you get the 5-digit date, most often …

excel vba

5
推荐指数
2
解决办法
61
查看次数

多维数组,用于存储和计数唯一ID的出现

背景:

为了更好地理解动态多维数组,我试图构建一个数组以捕获唯一值并计算唯一值的出现次数(我应该能够使用countif迅速验证这一点)。

在阅读有关尝试重新保存多维数组的imim时,我读到只能重新对最后一个参数进行imim,因此我尝试设置2个参数,其中第一个是唯一值,第二个是count:arr (2,k)。如果我的理解是错误的,那也很重要。

我将把数组的最终输出放入第3列(唯一ID)和第4列(出现次数)中。


问题:

将值添加到数组时,我无法收集所有唯一值。当数据中有6个值,并且每个值的出现都保持为1时(例如不进行迭代),我已经能够收集3个唯一值。


题:

我很抱歉这实际上是2个问题...

  • 1)我对redim保存器arr(2,0到k)的使用是否合适?

  • 2)我的动态数组生成是否存在明显问题,这可以解释为什么我没有捕获所有唯一值?

我可能会问三分之一关于为什么我无法使发生次数起作用的原因,但是我希望,如果我理解了上述问题,我有望在这一部分中奋斗。


数据如下所示:

所有数据均在A列中

cat
dog
mouse
cat
mouse
bear
frog
cat
moose
cat
dog
Run Code Online (Sandbox Code Playgroud)

有问题的代码:

Option Explicit

Private Sub unique_arr()
    Dim arr As Variant, i As Long, lr As Long, k As Long
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    ReDim arr(2, k)
    For i = 1 To lr
        If Application.IfError(Application.Match(Cells(i, 1).Value, arr, 0), 0) = 0 Then
            ReDim Preserve arr(2, 0 To k)
            arr(1, k) = Cells(i, 1).Value
            arr(2, k) = …
Run Code Online (Sandbox Code Playgroud)

excel vba

2
推荐指数
1
解决办法
46
查看次数

在if / then中选择大小写

我在用户窗体上有一个ToggleButton(TB)和ComboBox(CB),如果TB.value = True,则它必须确定CB.value是什么才能插入正确的文件。

我正在使用if / then语句来解决TB.value(我已经在用户窗体上的其他切换中使用了它,并且它本身已经有效。要解决CB.value,我尝试使用select案例陈述。

当我在if / then语句中使用select case语句运行脚本时,没有得到要插入的文件。

这是我的代码示例,包括如何列出CB进行初始化:

Private Sub CommandButton1_Click()

CreateObject (Word.Application.Documents.Add)

If ToggleButton1.Value = True Then
Select Case File
    Case ComboBox1.Value = "File A"
        Selection.InsertFile FileName:="C:\File A"
    Case ComboBox1.Value = "File B"
        Selection.InsertFile FileName:="C:\File B"
    End Select
Else:
    ToggleButton1.Value = False
End If

Unload Me

End Sub



Private Sub Userform_Initialize()

With ComboBox1
    .AddItem "File A", 0
    .AddItem "File B", 1
    End With

End Sub
Run Code Online (Sandbox Code Playgroud)

如果有人能够帮助我弄清楚为什么我无法使用上述方法插入文件,将不胜感激。如果有更好的方法可以做到这一点,我也很想听听!

先感谢您。

vba word-vba

1
推荐指数
1
解决办法
90
查看次数

VBA问题:如何使用VBA在excel中将值从“ X”更改为“ Y”

在Excel中,首先,我想检查位于下方的单元格内部的值Range (E2:E40)是否为“ X”,并且如果该特定单元格具有该值,则应将该单元格内部的值更改为“ Y”。使用VBA有什么办法可以提这样的吗?

我想创建一个宏,以便在报告中,如果我们看到E2-E40范围内的值“ X”,则该值应更改为“ Y”。

我尝试使用此命令,但收到错误消息:

If Range("E2:E40").Value = "X" Then Range("E2:E40").Value = "Y"
Run Code Online (Sandbox Code Playgroud)

我试图运行下面提到的代码,但收到一条错误消息: Run time error '13': Type Mismatch

excel vba

0
推荐指数
1
解决办法
75
查看次数

标签 统计

vba ×5

excel ×4

if-statement ×1

loops ×1

variant-array ×1

word-vba ×1