我会简短并坚持我所知道的。这段代码大部分都可以正常工作。唯一的问题是 x 和 z 循环的迭代。这些 to 循环应该设置 Y 循环的范围和 yLABEL。在事情变得疯狂之后,我可以通过一组并提出正确的范围。我知道其中一些与不打破 x 设置 z 然后回到 x 更新范围有关。
它应该工作 z 找到然后 x。它们之间的范围设置为 y。然后下一个 x 但 y 停留然后在 y 和 x 之间响起设置为 y .. 依此类推,有点像一个紧身的楼梯。或计算尺,这取决于我如何设置循环,经过几次迭代后,我最终会遍布整个地方。
我已经做了一些事情,但每次我突破 x 来设置 z 时,X 都会在范围的顶部重新开始。至少这是我认为我所看到的。在示例表中,我已经改变了偏移与循环一起工作的方式,但想法仍然相同。我此时有 goto 语句,我打算在循环工作后尝试找出条件开关。任何帮助方向或建议表示赞赏。
Option Explicit
Sub parse()
Application.DisplayAlerts = False
'Application.EnableCancelKey = xlDisabled
Dim strPath As String, strPathused As String
strPath = "C:\clerk plan2"
Dim objfso As FileSystemObject, objFolder As Folder, objfile As Object
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objfso.GetFolder(strPath)
'Loop through objWorkBooks
For Each objfile In objFolder.Files
If objfso.GetExtensionName(objfile.Path) = "xlsx" Then
Dim objWorkbook As Workbook
Set objWorkbook = Workbooks.Open(objfile.Path)
' Set path for move to at end of script
strPathused = "C:\prodplan\used\" & objWorkbook.Name
objWorkbook.Worksheets("inbound transfer sheet").Activate
objWorkbook.Worksheets("inbound transfer sheet").Cells.UnMerge
'Range management WB
Dim SRCwb As Worksheet, SRCrange1 As Range, SRCrange2 As Range, lastrow As Range
Set SRCwb = objWorkbook.Worksheets("inbound transfer sheet")
Set SRCrange1 = SRCwb.Range("g3:g150")
Set SRCrange2 = SRCwb.Range("a1:a150")
Dim DSTws As Worksheet
Set DSTws = Workbooks("clerkplan2.xlsm").Worksheets("transfer")
Dim STR1 As String, STR2 As String, xVAL As String, zVAL As String, xSTR As String, zSTR As String
STR1 = "INBOUND TRANS"
STR2 = "INBOUND CA TRANS"
Dim x As Variant, z As Variant, y As Variant, zxRANGE As Range
For Each z In SRCrange2
zSTR = Mid(z, 1, 16)
If zSTR <> STR2 Then GoTo zNEXT
If zSTR = STR2 Then
zVAL = z
End If
For Each x In SRCrange2
xSTR = Mid(x, 1, 13)
If xSTR <> STR1 Then GoTo xNEXT
If xSTR = STR1 Then
xVAL = x
End If
Dim yLABEL As String
If xVAL = x And zVAL = z Then
If x.Row > z.Row Then
Set zxRANGE = SRCwb.Range(x.Offset(1, 0).Address & " : " & z.Offset(-1, 0).Address)
yLABEL = z.Value
Else
Set zxRANGE = SRCwb.Range(z.Offset(-1, 0).Address & " : " & x.Offset(1, 0).Address)
yLABEL = x.Value
End If
End If
MsgBox zxRANGE.Address ' DEBUG
For Each y In zxRANGE
If y.Offset(0, 6) = "Temp" Or y.Offset(0, 14) = "Begin Time" Or y.Offset(0, 15) = "End Time" Or _
Len(y.Offset(0, 6)) = 0 Or Len(y.Offset(0, 14)) = 0 Or Len(y.Offset(0, 15)) = "0" Then yNEXT
Set lastrow = Workbooks("clerkplan2.xlsm").Worksheets("transfer").Range("c" & DSTws.Rows.Count).End(xlUp).Offset(1, 0)
y.Offset(0, 6).Copy
lastrow.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks:=True, Transpose:=False
DSTws.Activate
ActiveCell.Offset(0, -1) = objWorkbook.Name
ActiveCell.Offset(0, -2) = yLABEL
objWorkbook.Activate
y.Offset(0, 14).Copy
Set lastrow = Workbooks("clerkplan2.xlsm").Worksheets("transfer").Range("d" & DSTws.Rows.Count).End(xlUp).Offset(1, 0)
lastrow.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks:=True, Transpose:=False
objWorkbook.Activate
y.Offset(0, 15).Copy
Set lastrow = Workbooks("clerkplan2.xlsm").Worksheets("transfer").Range("e" & DSTws.Rows.Count).End(xlUp).Offset(1, 0)
lastrow.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks:=True, Transpose:=False
yNEXT:
Next y
xNEXT:
Next x
zNEXT:
Next z
strPathused = "C:\clerk plan2\used\" & objWorkbook.Name
objWorkbook.Close False
'Move proccesed file to new Dir
Dim OldFilePath As String
Dim NewFilePath As String
OldFilePath = objfile 'original file location
NewFilePath = strPathused ' new file location
Name OldFilePath As NewFilePath ' move the file
End If
Next
End Sub