Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 用条目将特定范围限定到最后一行是什么错误?_Excel_Vba - Fatal编程技术网

Excel 用条目将特定范围限定到最后一行是什么错误?

Excel 用条目将特定范围限定到最后一行是什么错误?,excel,vba,Excel,Vba,我尝试了更多的方法来填充E列中变量V2的结果,但它不起作用。您可以将我的尝试视为代码中的注释。有人有主意吗?谢谢 顺便问一下,是否可以通过宏计算出End(xlUp)和End(xlDown) 更新 我可以通过按下shift+Cntrl+ARROWN来解决这个问题 更新 现在怎么样 该怎么办 更新 最后的需要是这样的 据我所知,您正在使用粘贴在D列中的ColA值上的删除重复项(我猜代码没有问题),然后将这些值与ColA匹配,以对ColB中的值进行比较,从而创建一个类似于摘要的东西 如果我的理

我尝试了更多的方法来填充E列中变量
V2
的结果,但它不起作用。您可以将我的尝试视为代码中的注释。有人有主意吗?谢谢


顺便问一下,是否可以通过宏计算出
End(xlUp)
End(xlDown)

更新

我可以通过按下shift+Cntrl+ARROWN来解决这个问题


更新

现在怎么样

该怎么办

更新

最后的需要是这样的


据我所知,您正在使用粘贴在
D
列中的Col
A
值上的删除重复项(我猜代码没有问题),然后将这些值与Col
A
匹配,以对Col
B
中的值进行比较,从而创建一个类似于摘要的东西

如果我的理解是正确的,那么有一个更简单的方法

逻辑

  • 确定您的范围并将值存储在数组中。这是为了加快速度。要标识范围,可以找到如图所示的最后一行,然后使用该范围

  • 从Col
    a
    创建唯一的值集合

  • 基于唯一值定义第二个数组

  • 将唯一值与列
    A
    中的值进行比较,并整理列
    B
    中的值

  • 清除用于输出的列
    D
    E
    ,最后在那里输出数组

  • 确定要使用的最终范围。然后,您可以将颜色、边框等添加到该范围

  • 代码

    我已经对代码进行了注释,但是如果您在理解代码时仍然有问题,请务必让我知道

    Option Explicit
    
    Sub Sample()
        Dim ws As Worksheet
        Dim lRow As Long, i As Long, j As Long
        Dim MyAr As Variant, OutputAr As Variant
        Dim col As New Collection
        Dim itm As Variant
        Dim tmpString As String
        Dim rng As Range
        
        '~~> Change this to the relevant sheet
        Set ws = Sheet1
        
        With ws
            '~~> Find the last row in column A
            lRow = .Range("A" & .Rows.Count).End(xlUp).Row
            
            '~~> Store the Col A and B values in an array
            MyAr = .Range("A1:B" & lRow).Value2
            
            '~~> Loop through the array and get unique values from Col A
            For i = LBound(MyAr) To UBound(MyAr)
                On Error Resume Next
                col.Add MyAr(i, 1), CStr(MyAr(i, 1))
                On Error GoTo 0
            Next i
        End With
        
        '~~> Define your output array based on unique values found
        ReDim OutputAr(1 To col.Count, 1 To 2)
          
        j = 1
        '~~> Compare the unique values with values in Col `A`
        '~~> and collate the values from Col `B`
        For Each itm In col
            OutputAr(j, 1) = itm
            
            tmpString = ""
            
            For i = LBound(MyAr) To UBound(MyAr)
                If MyAr(i, 1) = itm Then
                    tmpString = tmpString & "," & MyAr(i, 2)
                End If
            Next i
            
            OutputAr(j, 2) = "'" & Mid(tmpString, 2)
            j = j + 1
        Next itm
        
        With ws
            '~~> Clear Col D and E for output
            .Columns("D:E").Clear
            
            '~~> Output the array
            .Range("D1").Resize(col.Count, 2).Value = OutputAr
            
            '~~> This is the final range
            Set rng = .Range("D1:E" & col.Count)
            
            With rng
                MsgBox .Address
                '
                '~~> Do what you want with the range here
                '
            End With
        End With
    End Sub
    
    在行动中

    添加边框的示例

    With rng
        .Borders(xlDiagonalDown).LineStyle = xlNone
        .Borders(xlDiagonalUp).LineStyle = xlNone
        
        For i = 7 To 12
            With .Borders(i)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
        Next i
    End With
    
    输出

    With rng
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    

    类似,将文本居中对齐

    With rng
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    
    输出

    With rng
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    

    你能给我一张你希望它看起来怎么样的截图吗?当然,我刚刚添加了图片,谢谢。重要信息:工作表随数据而变化。因此,我不能只说border“E2:E6”,如果我了解您想要实现什么,那么有更好的方法来实现它……您是指代码还是我的具体问题?
    Range(“E:E”)。End(xlDown)
    在VBA中没有任何意义。您应该使用
    Range(“E”和rows.count).End(xlUp).row
    来确定最后一行,并将范围构建为
    Range(“E2:E”和lastRow)
    @Siddarth Rout...........我该怎么说呢..这太完美了,还解决了第一个条目前逗号的问题…我被炸了,我的意思是我不明白你怎么能在10分钟内做这样的事情。我刚把ws改成了Activesheet。现在我正在用框架来计算你的更新。谢谢,真的不知道该说什么。很高兴它成功了。我建议不要使用
    ActiveSheet
    。使用实际图纸名称,如
    Set ws=Sheets(“Sheet1”)
    或代码名称,如
    Set ws=Sheet1
    。因为如果相关工作表未处于活动状态,则无法获得所需的结果。它工作得非常完美,不会与中心和边框开玩笑。如果我不使用ActiveSheet,问题是1。最后,该表必须由两人使用,两人应:。加载外部数据并在Excel中修改这些数据,我为其创建了3个按钮=3个步骤…这里的代码是第二步。我必须解决这个问题,这是最简单的,但我认为在VBA中重写代码,每次代码被分配到一个特定的工作表,这不是一个好主意吗?第三步是将修改后的数据提交到Word sheet如果数据加载到相关的工作表中并且处于活动状态,那么使用activesheet应该不会有问题,尤其是如果代码是从该工作表的按钮调用的。如果在a和B列旁边的C和D列中有更多值,我还有最后一个问题,我“只是”必须将更多的值整理成A,这对我理解正确吗?“~~>并整理Col
    B
    中的值,因此我只需复制注释下的代码,将其修改到特定范围,我执行了2次(一次用于C,一次用于D)?