2014-02-06 61 views
1

我有一个非常基本的问题。我有一个大型的工作簿,有很多列出的信息。我想将一些信息提取到一个新的工作簿中,并将其排序在不同的工作表上。我在使代码了解我希望将信息放入哪个选项卡时遇到了一些问题。 strName = Range(单元格值)不起作用,我不知道自己做错了什么。我怎样才能做到这一点?对于非常混乱的代码抱歉。将一个工作簿中的值排序到另一个工作簿中的正确工作表中

Private Sub CommandButton1_Click() 

Dim strName As String 

Set sourceWq = Workbooks("SD KPIs 2014 onwards").Worksheets("VQN+Concessionn") 
Set front = Workbooks("databank progging").Worksheets("Frontpage") 

For l = 5 To 30 
    For i = 2 To 250000 'Goes through the sourceWq workbook 
    If front.Cells(l, 13).Value = sourceWq.Cells(i, 24).Value Then 'Finds correct supplier 
     strName = Range("l,13") 
     Sheets(strName).Select 'Selects the correct worksheet for the supplier 
     For j = 4 To 15 'Month 
      If sourceWq.Cells(i, 33).Value = Cells(7, j).Value Then 
      For n = 8 To 11 'The type of NCR 
       If sourceWq.Cells(i, 27).Value = Cells(n, 2).Value Then 
       Cells(n, j).Value = Cells(n, j).Value + 1 
       Else: End If 
      Next n 
      Else: End If 
     Next j 
     Else: End If 
    Next i 
Next l 

End Sub 
+0

什么工作簿('sourceWq'或'front')你的'strName'位于? –

+0

同样的问题到'Sheets(strName).Select'。在你想要选择工作表的工作簿中? –

回答

0

strName = Range("l,13")应该读strName = Cells(l,13)

+0

或'strName = Cells(l,13)'? –

+0

我使用了strName = Cells(l,13),范围(l,13)不起作用。现在线表格(strName).Select超出范围..工作簿(“数据库编程”)。工作表(“strName”)也不工作..我可以使用什么呢? @simoco – user2979909

0

我稍微改写了你的代码,而这个循环For i = 2 To 250000(我用Find方法来代替):

Private Sub CommandButton1_Click() 

    Dim strName As String 
    Dim sourceWq As Worksheet, front As Worksheet, sh As Worksheet 
    Dim rng As Range 
    Dim firstAddress As String 
    Dim wb1 As Workbook, wb2 As Workbook 

    Dim l As Long, i As Long, j As Long 

    Set wb1 = Workbooks("SD KPIs 2014 onwards") 
    Set wb2 = Workbooks("databank progging") 

    Set sourceWq = wb1.Worksheets("VQN+Concessionn") 
    Set front = wb2.Worksheets("Frontpage") 

    For l = 5 To 30 
     With sourceWq.Range("X2:X250000") 
      Set rng = .Find(front.Cells(l, 13).Value, LookIn:=xlValues) 
     End With 
     If Not rng Is Nothing Then 
      firstAddress = rng.Address 
      Do 
       strName = front.Cells(l, 13).Value 
       Set sh = wb2.Worksheets(strName) 
       With sh 
        For j = 4 To 15 'Month 
         If rng.Offset(, 9).Value = .Cells(7, j).Value Then 
          For n = 8 To 11 'The type of NCR 
           If rng.Offset(, 3).Value = .Cells(n, 2).Value Then 
            .Cells(n, j).Value = .Cells(n, j).Value + 1 
           End If 
          Next n 
         End If 
        Next j 
       End With 
       Set rng = .FindNext(rng) 
      Loop While Not rng Is Nothing And rng.Address <> firstAddress 
     End If 
    Next l 
End Sub 
相关问题