我只提供片段。基本上,我在主花名册上有两组数据,它们用名册H列中的X
值除。我希望将X's
打印到Wb
的Sheet1中并将Blanks
打印到Sheet2。
我可以使用它,但是由于它将FinalDest
声明为单数变量,因此它不会从Sheet2的第2行开始。
示例:如果X填充到Sheet1的第10行,它将在第11行而不是2(在标题之后)开始Sheet2的数据。
Sub Main()
Dim Wb As Workbook
Dim Data, Last, Login, SaveTyping
Dim i As Long, j As Long, k As Long, a As Long
Dim Dest1 As Range, Dest2 As Range, FinalDest As Range
Set Wb = Workbooks("Template.xlsx")
Set Dest1 = Wb.Sheets("Currently Eligible").Range("A2")
Set Dest2 = Wb.Sheets("Newly Eligible").Range("A2")
With ThisWorkbook.Sheets("Roster")
Data = .Range("AA2", .Range("A" & Rows.Count).End(xlUp))
End With
在声明数组之后,这就是我将打印到模板的内容分开的方式。
SaveTyping = Data(i, 8) 'Column my X's and Blanks are
If InStr(SaveTyping, "X") Then
Set FinalDest = Dest1
End If
If SaveTyping = "" Then
Set FinalDest = Dest2
End If
For k = 1 To UBound(Data, 2)
FinalDest.Offset(j, a) = Data(i, k) 'Where I need to tell array to print
a = a + 1
Next
j = j + 1
Next
FinalDest
范围从Sheet1离开后的下一行开始拾取,如何防止这种情况发生,并使其从两张表的第2行开始?
Option Explicit
Sub Main()
Dim Wb As Workbook 'Workbook I'm printing each managers employee roster to and saving off a copy to a folder
Dim Data, Last, Login, chkVal 'Data = data I'm printing into template / Last = Manager name / Login = Manager Login ID
Dim i As Long, j As Long, k As Long, a As Long 'i = Data(row) / k = Data(column) / a = Wb(row) / j = Wb(column)
Dim Dest1 As Range, Dest2 As Range, FinalDest As Range 'Dest1 = Sheets(1) of Wb / Dest2 = Sheets(2) of Wb
Set Wb = Workbooks("Template.xlsx") 'Sets template for each file cut
Set Dest1 = Wb.Sheets("Currently Eligible").Range("B2")
Set Dest2 = Wb.Sheets("Newly Eligible").Range("B2")
With ThisWorkbook.Sheets("Sheet1")
Data = .Range("AA2", .Range("A" & Rows.Count).End(xlUp)) 'Raw data
End With
Wb.Activate
Application.ScreenUpdating = False
For i = 1 To UBound(Data) 'Row 1 to Ubound of Data(rows)
If Data(i, 1) <> Last Then 'only print array to Wb one manager at a time, we see when managers change because values in Data(i,1) will <> the next cell
If i > 1 Then 'skip header
Wb.SaveCopyAs ThisWorkbook.Path & Application.PathSeparator & _
ValidFileName(Login & " - " & Last & " - Shift Differential Validation.xlsx")
End If
With Sheets("Exempt Population")
.Rows(2 & ":" & .Rows.Count).ClearContents 'Clears previous managers data
End With
Last = Data(i, 1) 'Manager last name is in Column A
chkVal = Data(i, 8) 'Check for X or Blank in Column H
Login = Data(i, 27) 'Manager login ID is in column AA
j = 0 'Wb Column = 0
End If
a = 0 'Wb Row = 0
SaveTyping = Data(i, 8) 'Column my X's and Blanks are
If InStr(SaveTyping, "X") Then
Set FinalDest = Dest1
End If
If SaveTyping = "" Then
Set FinalDest = Dest2
End If
For k = 1 To UBound(Data, 2) 'Column 1 to Ubound of Data(columns)
FinalDest.Offset(j, a) = Data(i, k)
a = a + 1 'next Wb row
Next
j = j + 1 'next Wb column
Next
SaveCopy Wb, Login, Last '<< save the last report
End Sub
答案 0 :(得分:1)
我稍微整理了一下代码,为索引指定了适当的名称。
关于解决问题的方法,我添加了两个不同的行索引,一个为X,一个为空白。根据它是X还是空格,您可以递增一个或另一个。
Option Explicit
Sub Main()
Dim Wb As Workbook 'Workbook I'm printing each managers employee roster to and saving off a copy to a folder
Dim Data, Last, Login, chkVal 'Data = data I'm printing into template / Last = Manager name / Login = Manager Login ID
Dim row_data As Long, col_wb As Long, col_data As Long, row_wb As Long
Dim Dest1 As Range, Dest2 As Range, FinalDest As Range 'Dest1 = Sheets(1) of Wb / Dest2 = Sheets(2) of Wb
Dim row_index_x As Long, row_index_blank As Long, isX As Long
Set Wb = Workbooks("Template.xlsx") 'Sets template for each file cut
Set Dest1 = Wb.Sheets("Currently Eligible").Range("B2")
Set Dest2 = Wb.Sheets("Newly Eligible").Range("B2")
With ThisWorkbook.Sheets("Sheet1")
Data = .Range("AA2", .Range("A" & Rows.Count).End(xlUp)) 'Raw data
End With
Wb.Activate
Application.ScreenUpdating = False
' initialise row indices to 0, ignore header as Dest1 and Dest2 already at B2.
row_index_x = 0
row_index_blank = 0
For row_data = 1 To UBound(Data) 'Row 1 to Ubound of Data(rows)
' if manager name changed between this row and previous row
If Data(row_data, 1) <> Last Then 'only print array to Wb one manager at a time, we see when managers change because values in Data(row_data,1) will <> the next cell
If row_data > 1 Then 'skip header
' save wb every time manager changes
Wb.SaveCopyAs ThisWorkbook.Path & Application.PathSeparator & _
ValidFileName(Login & " - " & Last & " - Shift Differential Validation.xlsx")
End If
With Sheets("Exempt Population")
.Rows(2 & ":" & .Rows.Count).ClearContents 'Clears previous managers data
End With
Last = Data(row_data, 1) 'Manager last name is in Column A
chkVal = Data(row_data, 8) 'Check for X or Blank in Column H
Login = Data(row_data, 27) 'Manager login ID is in column AA
' reset output row every time manager name changes
row_wb = 0 'Wb Row = 0
End If
' for every data row, reset output column to zero (start a new row)
col_wb = 0 'Wb Col = 0
SaveTyping = Data(row_data, 8) 'Column my X's and Blanks are
' decide output destination
If InStr(SaveTyping, "X") Then
Set FinalDest = Dest1
row_wb = row_index_x
isX = 1 ' remember whether its X or blank
End If
If SaveTyping = "" Then
Set FinalDest = Dest2
row_wb = row_index_blank
isX = 0
End If
' Loop through all columns for one row of data
' keep output row the same, increase the output column
For col_data = 1 To UBound(Data, 2) 'Column 1 to Ubound of Data(columns)
FinalDest.Offset(row_wb, col_wb) = Data(row_data, col_data)
col_wb = col_wb + 1 'next Wb column
Next
'row_wb = row_wb + 1 'next Wb row
' decide which row index to increase
If isX = 1 Then
row_index_x = row_index_x + 1
Else
row_index_blank = row_index_blank + 1
End If
Next
SaveCopy Wb, Login, Last '<< save the last report
End Sub