以下代码是获取员工姓名(列A)并将该行的范围(“A:J”)输入该员工的新工作表(如果他们没有工作表),然后制作一个和广告标题。然而,它正在跳过每一行,这导致它正在扫描名称的行,并且它正在复制的行是不同的(即:员工进入错误的表,只有1/2正在获得移动) 任何帮助都会很棒
Set rngEmpSales = wsSales.Range("A2", wsSales.Range("A" & Rows.Count).End(xlUp).Address)
Dim wsSales As Worksheet, wsDesSales As Worksheet
Set wsSales = ThisWorkbook.Sheets("Sales")
Dim SalesCount as Range
For Each SalesCount In rngEmpSales
On Error Resume Next
Set wsDesSales = ThisWorkbook.Sheets(Trim(SalesCount.Value))
On Error GoTo 0
If wsDesSales Is Nothing Then
Set wsDesSales = ThisWorkbook.Sheets.Add(after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
wsDesSales.Name = SalesCount.Value
End If
SalesCount(1 - (SalesCount.Row - 1)).Range("A1:J1").Copy wsDesSales.Range("K2")
SalesCount.Range("A" & SalesCount.Row & ":J" & SalesCount.Row).Copy wsDesSales.Range("K" & Rows.Count).End(xlUp).Offset(1, 0)
Set wsDesSales = Nothing
End If
Next SalesCount
答案 0 :(得分:3)
这是你在尝试什么? (UNTESTED)
Sub Sample()
Dim wsSales As Worksheet, wsDesSales As Worksheet
Dim rngEmpSales As Range, SalesCount As Range
Dim shName As String
Dim lRow As Long, i As Long
Set wsSales = ThisWorkbook.Sheets("Sales")
With wsSales
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set rngEmpSales = .Range("A2:A" & lRow)
For i = 2 To lRow
shName = Trim(.Range("A" & i).Value)
On Error Resume Next
Set wsDesSales = ThisWorkbook.Sheets(shName)
On Error GoTo 0
If wsDesSales Is Nothing Then
Set wsDesSales = ThisWorkbook.Sheets.Add(after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
wsDesSales.Name = shName
End If
.Range("A1:J1").Copy wsDesSales.Range("K2")
.Range("A" & i & ":J" & i).Copy wsDesSales.Range("K" & _
wsDesSales.Rows.Count).End(xlUp).Offset(1, 0)
Set wsDesSales = Nothing
Next i
End With
End Sub
答案 1 :(得分:0)
你应该使用
wssales.Range("A" & SalesCount.Row & ":J" & SalesCount.Row)
代替SalesCount.Range("A" & SalesCount.Row & ":J" & SalesCount.Row)
和
wssales.Range("A1:J1").Copy
代替
SalesCount(1 - (SalesCount.Row - 1)).Range("A1:J1").Copy
原因是SalesCount
本身是一个范围,当您在其后应用另一个.Range
时,它将采取相对位置。
e.g。 Range("A2").Range("A1:J1")
变为Range("A2:J2")
,Range("B2").Range("B2:K2")
变为Range("B2:K2")