定义可变范围的单元格

时间:2016-03-07 11:11:36

标签: excel vba excel-vba

我在定义可变范围的单元格时遇到问题。有问题的线由每端的两颗星表示。在它下面有一些注释的线条,我也试过没有成功。 Public Sub Dreiecke_gleichen()工作正常,我只包含它,以便你可以看到“sheetvektor”是什么。

最后,关于我如何改进代码的任何建议,经验丰富的程序员在阅读时都不会晕倒? =)

非常感谢你,

Option Explicit
Private sheetvektor() As String

Public Sub Dreiecke_gleichen()

Dim ws As Worksheet
Dim Blatt1, Blatt2 As String
Dim Anfangsjahr1, Anfangsjahr2 As Integer
Dim reporting_Jahr1, reporting_Jahr2 As String
Dim i As Integer

i = 1

For Each ws In Worksheets

    If ws.Name Like "RVA_H*" Then
            ReDim Preserve sheetvektor(i)
            sheetvektor(i) = ws.Name

            If IsEmpty(Blatt1) = False Then
            Blatt2 = ws.Name
            Anfangsjahr2 = ws.Range("A3").Value
            reporting_Jahr2 = ws.Range("A1").Value
            i = i + 1
            Else
            Blatt1 = ws.Name
            Anfangsjahr1 = ws.Cells(3, 1).Value
            reporting_Jahr1 = ws.Cells(1, 1).Value
            i = i + 1
            GoTo X
            End If
    Else: GoTo X
    End If

    If reporting_Jahr1 <> reporting_Jahr2 Then
       MsgBox "Dreiecke von unterschiedlichen Jahren"
       Exit Sub

    ElseIf reporting_Jahr1 = reporting_Jahr2 Then

        If Anfangsjahr1 < Anfangsjahr2 Then
        Worksheets(Blatt2).Rows("3:" & 3 + Anfangsjahr2 - Anfangsjahr1 - 1).Insert


        ElseIf Anfangsjahr1 > Anfangsjahr2 Then
        Worksheets(Blatt1).Rows("3:" & 3 + Anfangsjahr1 - Anfangsjahr2 - 1).Insert

        ElseIf Anfangsjahr1 = Anfangsjahr2 Then GoTo X
        End If


   End If

   X: Next ws

   End Sub

Public Sub Dreiecksummieren()

Dim j, n As Integer
Dim lastcol, lastrow As Integer
Dim grosematrix() As Variant
Dim myrange As String


        If IsEmpty(sheetvektor()) = True Then
            MsgBox "Solche Blätter existieren nicht"
        Else
            j = 3

            Do While IsEmpty(Worksheets(sheetvektor(1)).Cells(j, 1)) = True
            j = j + 1
            Loop

            lastcol = Worksheets(sheetvektor(1)).Cells(j, 1).End(xlToRight).Column
            lastrow = Worksheets(sheetvektor(1)).Cells(j, 1).End(xlDown).Row

        End If


For n = 1 To UBound(sheetvektor)

**    grosematrix(n) = Worksheets(sheetvektor(n)).Range(Cells(j, 1), Cells(lastrow, lastcol)).Value  **

'    grosematrix(n) = Worksheets(sheetvektor(n)).Range("A" & j: Cells(lastrow, lastcol)).Value

'     grosematrix(n) = Worksheets(sheetvektor(n)).Range(Cells(j, 1).Address(), Cells(lastrow, lastcol).Address()).Value

'    Let myrange = "Cells(j, 1), Cells(lastrow, lastcol)"
'    Let grosematrix(n) = Worksheets(sheetvektor(n)).Range(myrange).Value
Next n

Debug.Print (WorksheetFunction.Sum(grosematrix))

End Sub

2 个答案:

答案 0 :(得分:0)

我会尝试删除一些()

Dim grosematrix As Variant   'no ()
...
grosematrix = Worksheets(sheetvektor(n)).Range(Cells(j, 1), Cells(lastrow, lastcol))

答案 1 :(得分:0)

我相信您必须自行解决,以逐页更新“groseSum”

此外,我试图掌握你的代码的逻辑,这就是我的目标

Option Explicit

Private sheetvektor As Collection

Public Sub Dreiecke_gleichen2()

Dim wsRef As Worksheet, ws As Worksheet
Dim Blatt1 as string, Blatt2 As String
Dim Anfangsjahr1 as integer, Anfangsjahr2 As Integer
Dim reporting_Jahr1 as string, reporting_Jahr2 As String
Dim iWs As Integer


Set sheetvektor = GetSheets(ActiveWorkbook, "RVA_H*") ' get all "wanted" worksheets of the active workbook whose name matches the given one

If sheetvektor.Count = 0 Then Exit Sub ' if no "wanted" worksheets then exit sub

Set wsRef = sheetvektor.Item(1) ' set the first "wanted" worksheet as the reference one
With wsRef 'get reference values
    Blatt1 = .Name
    Anfangsjahr1 = .Cells(3, 1).Value
    reporting_Jahr1 = .Cells(1, 1).Value
End With

For iWs = 2 To sheetvektor.Count

    Set ws = sheetvektor.Item(iWs)
    With ws
        Blatt2 = .Name
        Anfangsjahr2 = .Cells(3, 1).Value
        reporting_Jahr2 = .Cells(1, 1).Value
    End With

    If reporting_Jahr1 <> reporting_Jahr2 Then

       MsgBox "Dreiecke von unterschiedlichen Jahren"
       Exit For

    Else

        If Anfangsjahr1 < Anfangsjahr2 Then
            ws.Rows("3:" & 3 + Anfangsjahr2 - Anfangsjahr1 - 1).Insert

        ElseIf Anfangsjahr1 > Anfangsjahr2 Then
            wsRef.Rows("3:" & 3 + Anfangsjahr1 - Anfangsjahr2 - 1).Insert
        End If

    End If

Next iWs

Call Dreiecksummieren

End Sub


Function GetSheets(wb As Workbook, shtNameLike As String) As Collection
Dim ws As Worksheet
Dim wss As New Collection

For Each ws In wb.Worksheets
    If ws.Name Like shtNameLike Then wss.Add ws
Next ws

Set GetSheets = wss

End Function



Public Sub Dreiecksummieren()

Dim lastcol As Integer, lastrow As Integer, j As Integer
Dim refAdress As String
Dim groseSum As Double

If sheetvektor.Count = 0 Then
    MsgBox "Solche Blätter existieren nicht"
    Exit Sub
End If

With sheetvektor(1)
    j = 3
    If IsEmpty(.Cells(j, 1)) Then j = .Cells(j, 1).End(xlDown).Row

    lastcol = .Cells(j, 1).End(xlToRight).Column ' warning: if there's no values on the right you'll hit the last column of the sheet...
    lastrow = .Cells(j, 1).End(xlDown).Row ' warning: if there's no values down you'll hit the last row of the sheet.... if there's at least one value down the column followed by an empty cell, you'll miss the rows following that empty cell

End With

refAdress = Range(Cells(j, 1), Cells(lastrow, lastcol)).Address
groseSum = 0
For j = 1 To sheetvektor.Count
    groseSum = groseSum + WorksheetFunction.Sum(sheetvektor(j).Range(refAdress))
Next j

Debug.Print (groseSum)

End Sub

以下是使用grosematrix()的替代方法,它以“矩阵向量”形式出现,其索引为“i”,您将放置2D范围的内容。

因此grosematrix(3)(2,3)将返回相对于所选范围的单元格(2,3)的内容(反过来可以是某些“A3:H9”,因此其单元格(2,3)实际上是“C4” “)第三个工作表中的所有工作表都被过滤掉了。

因为您无法将3D元素作为WorksheetFunction.Sum的参数,所以在迭代sheetvektor

的每个工作表时,仍然必须更新groseSum
Public Sub Dreiecksummieren2()

Dim lastcol As Integer, lastrow As Integer, j As Integer
Dim refAdress As String
Dim grosematrix() As Variant
Dim groseSum As Double

If sheetvektor.Count = 0 Then
    MsgBox "Solche Blätter existieren nicht"
    Exit Sub
End If

With sheetvektor(1)
    j = 3
    If IsEmpty(.Cells(j, 1)) Then j = .Cells(j, 1).End(xlDown).Row

    lastcol = .Cells(j, 1).End(xlToRight).Column ' warning: if there's no values on the right you'll hit the last column of the sheet...
    lastrow = .Cells(j, 1).End(xlDown).Row ' warning: if there's no values down you'll hit the last row of the sheet.... if there's at least one value down the column followed by an empty cell, you'll miss the rows following that empty cell
End With
refAdress = Range(Cells(j, 1), Cells(lastrow, lastcol)).Address

groseSum = 0
ReDim grosematrix(1 To sheetvektor.Count)
For j = 1 To sheetvektor.Count
    grosematrix(j) = sheetvektor(j).Range(refAdress)
    groseSum = groseSum + WorksheetFunction.Sum(grosematrix(j))
Next j

Debug.Print (groseSum)

End Sub

至于逻辑,假设我确实得到了你的实际(我不太确定......),我认为你必须改进它以捕捉异常(没有适当/最小数量的纸张符合标准,正确数据范围的定义要考虑到......)