简体   繁体   English

VBA打开将继续运行程序的新工作簿

[英]VBA to open new workbook that will continue to run programme

I have a massive amount of data, that i spooled from a really old system.我有大量数据,是从一个非常旧的系统中提取的。 Each record for the client does not appear in columns, but a mix of rows and columns.客户端的每条记录都不会出现在列中,而是行和列的混合。 The only way I could split the data was by using the empty row to separate each record to new worksheet (in same workbook) then I will manipulate the data that way.我可以拆分数据的唯一方法是使用空行将每条记录分隔到新工作表(在同一个工作簿中),然后我将以这种方式处理数据。 My vba is working for this, but I have too many records and the worksheet capacity in the workbook reaches its limit (approx 1400, saved as sheet1, sheet2..) .我的 vba 正在为此工作,但是我的记录太多,工作簿中的工作表容量达到了极限(大约 1400,另存为 sheet1,sheet2..)。 Is there anyway I can incorporate into my vba to save the current workbook as perhaps record1, open a new workbook maybe entitled record2 and when this workbook is also full save, and continue process until all of the data is separated accordingly.无论如何我可以合并到我的vba中以将当前工作簿保存为可能的record1,打开一个新的工作簿可能名为record2并且当这个工作簿也完全保存时,并继续处理直到所有数据都被相应地分开。

Here is my vba这是我的vba

        Private Sub excelsplit()
Dim wbk As Workbook
Dim l_str, l_end, l_row As Long

Set wbk = ThisWorkbook


Application.DisplayAlerts = False
Do Until wbk.Sheets.Count = 1
wbk.Sheets(wbk.Sheets.Count).Delete
Loop
Application.DisplayAlerts = True


l_str = 2
l_row = 2
Do While l_row <= wbk.Sheets(1).Range("A1000000").End(xlUp).Row + 1
If wbk.Sheets(1).Range("A" & l_row).Value = "" And _
wbk.Sheets(1).Range("B" & l_row).Value = "" And _
wbk.Sheets(1).Range("c" & l_row).Value = "" And _
wbk.Sheets(1).Range("d" & l_row).Value = "" And _
wbk.Sheets(1).Range("e" & l_row).Value = "" And _
wbk.Sheets(1).Range("f" & l_row).Value = "" And _
wbk.Sheets(1).Range("g" & l_row).Value = "" And _
wbk.Sheets(1).Range("h" & l_row).Value = "" And _
wbk.Sheets(1).Range("i" & l_row).Value = "" And _
wbk.Sheets(1).Range("j" & l_row).Value = "" And _
wbk.Sheets(1).Range("k" & l_row).Value = "" And _
wbk.Sheets(1).Range("l" & l_row).Value = "" Then
wbk.Sheets.Add after:=wbk.Sheets(wbk.Sheets.Count)
wbk.Sheets(wbk.Sheets.Count).Range("A2:l" & l_row - l_str + 1).Value = wbk.Sheets(1).Range("A" & l_str & ":l" & l_row).Value
l_str = l_row + 1
End If
l_row = l_row + 1
Loop

End Sub

I have 52000 rows of data, so would have to perform an open and shut scenario about 35 times.我有 52000 行数据,因此必须执行大约 35 次打开和关闭场景。 Any help would be much appreciated.任何帮助将非常感激。

Attached is a screen shot of my data....the problem is the varying rows, and some cells have relevant enter image description here data but some don't.附件是我的数据的屏幕截图....问题是不同的行,有些单元格在此处输入相关的图像描述数据,但有些则没有。 (I think to parse it) (我想解析它)

this is my end result enter image description here这是我的最终结果在此处输入图像描述

Please ignore this answer if you know that your data in Sheet1 has not a consistent pattern.如果您知道 Sheet1 中的数据没有一致的模式,请忽略此答案。

The code below is based by the look of the data in the first image (Sheet1).下面的代码基于第一张图像 (Sheet1) 中数据的外观。

Sub test()
Dim rg As Range: Dim cell As Range
Dim oFill As Range: Dim cnt As Long
Dim regID
Dim mil
Dim custBname
Dim custPHnum
Dim ref
Dim dt
Dim vat
Dim tot
Dim amt

With Sheets("Sheet1")
Set rg = .Range("i2", .Range("i" & Rows.Count).End(xlUp))
End With

Set oFill = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

cnt = 0

For Each cell In rg.SpecialCells(xlConstants)

    If IsNumeric(cell.Value) Then

        If cell.Offset(-1, -2).Value <> "" Then
            cnt = cnt + 1
            regID = "id-" & Format(cnt, "000")
            mil = cell.Offset(-2, -1).Value 'value for mileage
            custBname = cell.Offset(-2, 1).Value 'value for customer business name
            custPHnum = cell.Offset(-2, 2).Value 'value for customer phone number
        End If
    
        ref = cell.Value 'value for column Ref
        dt = cell.Offset(0, -1).Value 'value for column Date
        vat = Split(cell.Offset(0, 2).Value, " ")(0) 'value for VAT
        tot = Split(cell.Offset(0, 2).Value, " ")(Application.CountA(Split(cell.Offset(0, 2).Value, " ")) - 1) 'value for total
        amt = tot - vat 'value for amount
    
        oFill.Value = regID
        oFill.Offset(0, 1).Value = mil
        oFill.Offset(0, 2).Value = custBname
        oFill.Offset(0, 3).Value = custPHnum
        oFill.Offset(0, 4).Value = ref
        oFill.Offset(0, 5).Value = dt
        oFill.Offset(0, 6).Value = vat
        oFill.Offset(0, 7).Value = tot
        oFill.Offset(0, 8).Value = amt
    
        Set oFill = oFill.Offset(1, 0)
    
    End If
    
Next

End Sub

The sub assumed that in Sheet1 column i, if the row contains a number then the value of this row (for sure) is the ref number.子假设在 Sheet1 第 i 列中,如果该行包含一个数字,则该行的值(肯定)是参考编号。 For example based on your Sheet1 image, cell i16, i17, i22, i27, and so on.例如,基于您的 Sheet1 图像、单元格 i16、i17、i22、i27 等。

The sub also assumed that there is always a value in column G where the row is just right above the row which contains the ref number. sub 还假设 G 列中始终存在一个值,其中该行正好位于包含参考编号的行的正上方。 Sorry it's difficult for me to explain it in English.对不起,我很难用英语解释它。 Anyway, for example :无论如何,例如:
cell i16.offset(-1,-2) ---> is cell G15 ---> so cell G15 must have a value (from your image, the value is 710).单元格 i16.offset(-1,-2) ---> 是单元格 G15 ---> 所以单元格 G15 必须有一个值(根据您的图像,该值为 710)。
cell i22.offset(-1,-2) ---> is cell G21 ---> so cell G21 must have a value(from your image, the value is avav).单元格 i22.offset(-1,-2) ---> 是单元格 G21 ---> 所以单元格 G21 必须有一个值(根据您的图像,该值为 avav)。
And so on.等等。

The process:过程:
It make a range with data from column i Sheet1 into variable rg它使用来自第 i 列 Sheet1 的数据创建一个范围到变量 rg
It make a range to fill in Sheet2 into variable oFill它将Sheet2填充到变量oFill中
Make the cnt variable value as zero使 cnt 变量值为零

Then it loop to each cell (which has value) in rg.然后它循环到 rg 中的每个单元格(具有值)。
On each loop of the cell, it check if the cell value is a number.在单元格的每个循环中,它检查单元格值是否为数字。
if the cell value is a number, then it check if this cell.offset(-1,-2) has a value如果单元格值是数字,则检查此 cell.offset(-1,-2) 是否有值
if it has a value, then it means this looped cell has a new regID,如果它有一个值,那么这意味着这个循环的单元格有一个新的 regID,
then it make a variable which contains the information needed coming from NOT the row of the looped cell.然后它创建一个变量,其中包含来自循环单元格行之外的所需信息。 So the code make a regID, mil, custBname and custPHnum where the row with the value of them is two rows above the looped cell row.因此,代码创建了一个 regID、mil、custBname 和 custPHnum,其中具有它们值的行是循环单元格行上方的两行。

Then it make another variable (ref, dt, vat, tot, amt) where the row with the value of them is the same with the looped cell row.然后它创建另一个变量(ref,dt,vat,tot,amt),其中具有它们值的行与循环单元格行相同。

To get the vat and tot variable, it split the looped cell.offset(0,2) with " " into an array, where the vat value is the first item in the array, and the tot value is the last item in the array.为了得到 vat 和 tot 变量,它将循环的 cell.offset(0,2) 用 " " 拆分成一个数组,其中 vat 值是数组中的第一项,tot 值是数组中的最后一项.
Then it subtract the tot with vat to get the value for amt variable.然后用vat 减去tot 得到amt 变量的值。

Finally it fill the last empty row in column A of Sheet2 with all those needed information.最后,它用所有需要的信息填充 Sheet2 的 A 列中的最后一个空行。

Based on the sample image data, the second looped cell with a number value is 4552 (cell i17).根据样本图像数据,具有数字值的第二个循环单元格为 4552(单元格 i17)。 Since it won't find a value on cell i17.offset(-1,-2) ---> cell G16, then when it fill the last empty row in column A of Sheet2, the same information of regID, mil, custBname and custPHnum --- coming from the previous loop where it find a value in cell.offset(-1,-2) --- will be used.由于它不会在单元格 i17.offset(-1,-2) ---> 单元格 G16 上找到值,因此当它填充 Sheet2 的 A 列中的最后一个空行时,regID、mil、custBname 的信息相同并且 custPHnum ---来自上一个循环,它在 cell.offset(-1,-2) 中找到一个值--- 将被使用。

The data "pattern" in Sheet1 must be consistent to get all those variables have a correct value. Sheet1 中的数据“模式”必须一致才能使所有这些变量都具有正确的值。

I'm sorry as I can't test the sub as it's quite difficult for me to make the same data pattern as it looks in your Sheet1 image.很抱歉,我无法测试 sub,因为我很难制作与 Sheet1 图像中相同的数据模式。 So the sub above is not tested , but maybe can give you an idea to start.所以上面的子没有经过测试,但也许可以给你一个开始的想法。

The code above is tested with this kind of data pattern:上面的代码是用这种数据模式测试的:
在此处输入图像描述

After run the sub, the result is like this :运行 sub 后,结果如下:
在此处输入图像描述

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

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