简体   繁体   English

将VB.NET应用程序加速到Excel

[英]Speeding Up VB.NET application to Excel

I have written a VB.NET application in Visual Studio 2015. (first time ever had any contact with visual basic or VS). 我已经在Visual Studio 2015中编写了VB.NET应用程序。(第一次与Visual Basic或VS有任何联系)。 The application takes an input csv file, analyses it and splits it according to that analysis into 2 output csv files. 该应用程序获取一个输入的csv文件,对其进行分析,并根据分析将其拆分为2个输出的csv文件。 For one of these output files, I then need to change every blank cell to have the value of zero. 对于这些输出文件之一,然后需要将每个空白单元格更改为零值。 My prob, is that the code i've made is processing 750 input csv files to produce 1500 output files and each process in the loop is taking 5 mins meaning it's taking up to 5 days to run!! 我的问题是,我编写的代码正在处理750个输入的csv文件,以生成1500个输出文件,循环中的每个进程需要5分钟,这意味着最多要花5天的时间来运行! That is too long! 太久了!

I'm trying to work out how to make the code run quicker. 我正在尝试找出如何使代码更快地运行。 One easy first step would be in the blank cell to zero operation as i'm currently doing it cell by cell. 一个简单的第一步是在空白单元格中进行零操作,因为我目前正在逐个单元地进行操作。 I read that better to do via an array but i'm unsure how to code it...Can someone help? 我读起来更好,可以通过数组来完成,但是我不确定如何编码...有人可以帮忙吗?

My code now is: 我的代码现在是:

    Dim forceDataRangeDest, cell As Excel.Range
    Dim blank As String
    Dim forceDataRow, lastDataRow As Integer

'copy force data from original workbook to sheet 1 of new workbook
            If ws.Range("Z" & (forceLegRowStart + 1)).Value = "Force Plate 3" Then
                forceDataRow = forceDataRow + 2
                forceDataRangeSrc = ws.Range("Z" & forceDataRow & ":AK" & lastDataRow)
            Else forceDataRangeSrc = ws.Range("A" & forceDataRow & ":M" & lastDataRow)
            End If
            wsData = wbForce.Sheets("Sheet1")
            wsData.Name = "Data"
            forceDataRangeDest = wsData.Range("A1")
            forceDataRangeSrc.Copy(forceDataRangeDest)

            'insert new column A if Force Plate 3 data is one taken for the time interval data of column A
            If ws.Range("Z" & (forceLegRowStart + 1)).Value = "Force Plate 3" Then
                wsData.Columns("A:A").Insert(1)
                'write in the Data
                forceDataRangeSrc = ws.Range("A" & forceDataRow & ":A" & lastDataRow)
                forceDataRangeSrc.Copy(wsData.Range("A1"))
            End If

    forceDataRangeDest = wsData.Range("A1:M" & ((lastDataRow - forceDataRow) + 1))
                For Each cell In forceDataRangeDest
                    blank = cell.Value
                    If String.IsNullOrWhiteSpace(blank) Then
                        cell.Value = 0
                    End If
                Next

It is the For Each cell at the bottom of this sample code that i think is really increasing the process time...how would i write that as an array and then write array into excel in one go? 我认为实际上是在此示例代码底部的For Each单元格增加了处理时间...我将如何将其写为数组,然后将数组一次性写入excel?

Many thanks for any help you can give. 非常感谢您提供的任何帮助。

You could use the Range.Find and Range.FindNext methods in VBA which would be quicker than looping through all the cells. 您可以在VBA中使用Range.FindRange.FindNext方法,这比遍历所有单元格要快。 Here's an example of the VBA code: 这是VBA代码的示例:

Sub FindEmptyCells()

Dim found As Range
Dim firstCell As String

    Set found = ActiveSheet.UsedRange.Find(What:="", LookAt:=xlWhole)
    firstCell = found.Address

    Do While Not (found Is Nothing)
        Debug.Print found.Address
        Set found = ActiveSheet.UsedRange.FindNext(found)
        If found.Address = firstCell Then
            Exit Do
        End If
    Loop

End Sub

EDIT: Added the code to use OP's range object 编辑:添加了代码以使用OP的范围对象

Dim found As Range
Dim firstCell As String

    Set found = forceDataRangeDest.Find(What:="", LookAt:=xlWhole)
    firstCell = found.Address

    Do While Not (found Is Nothing)
        found.Value = 0
        Set found = forceDataRangeDest.FindNext(found)
        If found.Address = firstCell Then
            Exit Do
        End If
    Loop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM