English 中文(简体)
Excel VBA - 协助比较范围和阵列
原标题:Excel VBA - Assistance on comparing ranges and arrays
  • 时间:2011-10-20 07:53:18
  •  标签:
  • excel
  • vba

我借着这份工作手册,我正试图通过宏观法典开展工作。 我曾得到过一些援助,但似乎没有人了解我之后的情况。 这份工作手册用于跟踪和总结我们公司每个用户所收到工作服的数量。 因此,基本上,我在这份工作手册中翻了三张:

  • Summary
  • Users
  • Articles

一般想法是,我将从汇总表中的数据中得出一个可计算点。 但是,我希望该工作手册能够使用真.的法典。 因此,我穿过这里的每一页。

用户:这份工作手册仅包含一栏(A),A1称为“Name”,每行各有公司每个用户。

文章: 这份工作手册有两栏,A1是本条的名称(英文等),另一栏是该项目的价格。

概述:这是trick。 该表应反映其他两个表的数据,但我需要跟踪每个用户收到的每个项目的。 我将这一数据保留在汇总表D栏。 因此,用户表中的每个名字都需要重复,因为文章中都有项目。 如果在条款表中有10项,则必须重复10次。 这样,我就可以说用户收到的每个项目有多少。

So, the tricky part is to actually mirror the content from the Users and Articles sheet, but still keep the data from column D in the summary sheet. Also keep in mind that if I remove a row from the Users sheet then that user needs to be removed completely from the Summary Sheet, including the amount of each item that was registered. And if I add an item to the Articles sheet, then that item needs to be added for each user in the Summary sheet.

我在那里看到了一些宏观法典,有些人帮助我,但我确实没有获得正在做的事情。 我不想随阵列和 lo而.。 这是我现在试图学习的,因为我看到学习这一潜力。

不过,我确实认为,我需要从所有表格中收集数据,储存所有数据。 然后,我需要将用户范围与《概述》范围进行比较,以了解用户是否在场。 如果是的话,确保更新《条款范围》的数据,并从哥伦德保留数据。 如不列入汇总表,则添加。 每个项目也是如此。

但是,如果我错了用户的型号,在我早就为用户增加数额之后,不会实现用户的字面? 如果我回到用户名册并重新命名用户,那么我是否会放松我以前增加的所有数据? 还是能够重新命名用户? 在该案中,我可能需要为每个用户提供某种身份识别,如Windows的CID? 这是否太高了? 这一切都是在时间上越好。 我真的赞赏这里的一些帮助:

Public Sub NewCollect()
  Declare variables
Dim shtUsers, shtmyArticles, shtmySummary, shtmyAmount As Worksheet
Dim arrUsers, arrarticles, arramount, arrsummary As Long

  Set worksheets
Set shtUsers = Sheets("Brukere")
Set shtArticles = Sheets("Artikler")
Set shtSummary = Sheets("Oppsummering")
Set shtAmount = Sheets("Antall")

  Get range from shtUsers
With shtUsers
    If Not .Range("A2") = "" Then
        arrUsers = .Range("A2", .Cells(Rows.Count, "A").End(xlUp)).Resize(, 2)
    End If
End With

  Get range from shtArticles
With shtArticles
    If Not .Range("A2") = "" Then
        arrarticles = .Range("A2", .Cells(Rows.Count, "A").End(xlUp)).Resize(, 3)
    End If
End With

  Get range from shtAmount (The new sheet)
With shtAmount
    If Not .Range("A2") = "" Then
        arramount = .Range("A2", .Cells(Rows.Count, "A").End(xlUp)).Resize(, 2)
    End If
End With

  Get range from shtSummary
With shtSummary
    If Not .Range("A2") = "" Then
         Here I have no idea where to even begin
    Else
          If Summary sheet is blank, get data from other sheet and insert
        ReDim tempArr(1 To UBound(arrUsers) * UBound(arrarticles), 1 To 6)
        For u = 1 To UBound(arrUsers)
            For i = 1 To UBound(arrarticles)
                j = j + 1
                tempArr(j, 1) = arrUsers(u, 1)
                tempArr(j, 2) = arrUsers(u, 2)
                tempArr(j, 3) = arrarticles(i, 1)
                tempArr(j, 4) = arrarticles(i, 2)
                tempArr(j, 6) = arrarticles(i, 3)
            Next
        Next
          Add the data
        .Range("A2").Resize(j, 6).Value = tempArr
    End If
End With

Edit:我只给用户增加了一个新的栏目,而书目则为固定的身份证,我可以在每个项目中增加一个身份证。 更新了我天空司机的概况介绍。

最佳回答

首先,我要把你的意见与你的产出完全分开。 这是基于经验的,因为几年前,我为会计人员制定了相当复杂的会计电子表格,作为总分类账和P&L。

控制信息载于一份表格,《全球升温潜能值编码》是另一个表格,交易是另一个报表,宏观基本上通过,在另外四个表上编制资产负债表和收入/支出报表。

最初的尝试试图在投入方面操纵信息,但后来变成了梦.。 一旦投入和产出分开,就变得更容易管理。

换言之,有以下表格:

  • People.
  • Items.
  • Transaction.
  • Output.

头三个只是投入。 交易是一份清单,列出哪些项目是提供给人们的(许多对人的关系)。 然后,我会有一个如下宏观。

第一,完全清楚第四页(产出)。 接着,在《人民报》中,每名活跃人员都通过交易单,并为与其相关的任何交易创造出一个产出条目。

顺便说一句,因为你可能想保持历史,为已经离开的人保存记录。 这在《人民报》中将是某种旗帜。

作为这一进程的一部分,你或许需要研究这些物品和价格。

You can report on errors as part of the macro as well, such as Transaction entries that have no valid People or Items.

你们还可能想考虑某些人/项目可能具有相同的名称(甚至一个相同的项目可能定期改变价格)。 为此,最好给每个人提供独一无二的身份证和物品,以确保不存在错误识别的可能性。 这些独特的身份证将存放在交易单上。


Since the macro I discussed weighs in at 37K, I can t post the lot here. But this is the main processing bit which processes the transactions sheet and updates the accounts page with balances:

Rem Attribute VBA_ModuleType=VBAModule
Option VBASupport 1
Option Explicit

Public Const TxnSheet = "Txns"
Public Const TxnColId = "a"
Public Const TxnColDate = "b"
Public Const TxnColAcct = "c"
Public Const TxnColAmt = "d"
Public Const TxnColDesc = "e"
Public Const TxnColNotes = "f"
Public Const TxnRowStart = "2"

Public Const AcctSheet = "Accts"
Public Const AcctColReport = "a"
Public Const AcctColType = "b"
Public Const AcctColBold = "c"
Public Const AcctColItalic = "d"
Public Const AcctColFontPlus1 = "e"
Public Const AcctColOther2 = "f"
Public Const AcctColOther3 = "g"
Public Const AcctColOther4 = "h"
Public Const AcctColOther5 = "i"
Public Const AcctColLevel = "j"
Public Const AcctColSign = "k"
Public Const AcctColAcct = "l"
Public Const AcctColVal = "m"
Public Const AcctColNotes = "n"
Public Const AcctRowStart = "2"

  Process all transactions.

Sub ProcessTransactions()
    Dim TxnId As Integer
    Dim Balance As Double
    Dim WsTxn As Worksheet
    Dim WsAcct As Worksheet

    Dim RowTxn As String
    Dim RowAcct As String

    Dim RowTxn2 As String
    Dim RowTxn3 As String

    Dim StartDate As Date
    Dim EndDate As Date
    Dim CutoffDate As Date
    Dim PastCutoff As Boolean

      Get user-configurable stuff

    StartDate = GetConfig("start_date")
    EndDate = GetConfig("end_date")
    CutoffDate = GetConfig("cutoff_date")
    PastCutoff = False

      For filling in transaction IDs.

    TxnId = 1

    Set WsTxn = Worksheets(TxnSheet)
    Set WsAcct = Worksheets(AcctSheet)
    RowTxn = TxnRowStart


      Select the worksheet and cell so we can see what s happening.

    WsTxn.Select
    Range(TxnColAcct + RowTxn).Select
    Range(TxnColAcct + RowTxn).Show

      Process all transaction lines.

    Do While Range(TxnColAcct + RowTxn).Value <> ""
          Check for start of transaction (non-blank date).

        If Range(TxnColDate + RowTxn).Value <> "" Then
              Check date within range.

            If Range(TxnColDate + RowTxn).Value < StartDate Or Range(TxnColDate + RowTxn).Value > EndDate Then
                Range(TxnColDate + RowTxn).Select
                MsgBox "ERROR: ProcessTransactions: Date out of range"
                End
            End If

            If Range(TxnColDate + RowTxn).Value > CutoffDate Then
                PastCutoff = True
            End If

              Start of transaction, fill in transaction ID and increment.

            Range(TxnColId + RowTxn).Value = TxnId
            TxnId = TxnId + 1

              Check that transaction is balanced.

            RowTxn2 = FindNextTxn(RowTxn)
            RowTxn3 = PrevRow(RowTxn2)

            Balance = 0
            Do While RowTxn2 <> RowTxn
                RowTxn2 = PrevRow(RowTxn2)
                Balance = Balance + Range(TxnColAmt + RowTxn2).Value
            Loop
            If Balance > 0.001 Or Balance < -0.001 Then
                Range(TxnColAmt + RowTxn + ":" + TxnColAmt + RowTxn3).Select
                MsgBox "ERROR: ProcessTransactions: Unbalanced transaction"
                End
            End If
        Else
              Not transaction start, clear transaction ID column.

            Range(TxnColDate + RowTxn).Clear
        End If

          Get account line, error if account not in accounts worksheet.

        RowAcct = FindAccount(Range(TxnColAcct + RowTxn).Value)
        If RowAcct = "" Then
            MsgBox "ERROR: ProcessTransactions: Invalid account  " & Range(TxnColAcct + RowTxn).Value & " "
            End
        End If

          Update accounts value.

        If Not PastCutoff Then
            WsAcct.Range(AcctColVal + RowAcct) = WsAcct.Range(AcctColVal + RowAcct) + Range(TxnColAmt + RowTxn).Value
        End If

          Move to next transaction.

         Sleep 50
        RowTxn = NextRow(RowTxn)
        Range(TxnColAcct + RowTxn).Select
        Range(TxnColAcct + RowTxn).Show
    Loop

    Range(TxnColDate + RowTxn).Select
    Range(TxnColDate + RowTxn).Show
End Sub

It may not be that useful without knowing the sheet layout but it s the best I can do without being able to send you the whole workbook.

问题回答

暂无回答




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签