2017-05-25 25 views
0

這個想法背後是使用vba vlookup G列:AI從sheet11-13到sheet1。標題在所有工作表的第3行結束。跨多頁的查找

我已經寫了如下的代碼。代碼停止在ws1.Cells(r, c).Value = Application.WorksheetFunction.VLookup(ws1.Cells(r, 1).Value, ws2.Range("A1:AI500"), colnum, False)顯示子集超出範圍,有時甚至

運行時錯誤'1004':無法獲取WorksheetFunction類的VLookup屬性。

請在前進的道路諮詢。

我想發送文件爲了更好地澄清,但似乎無法找到附加功能。謝謝 !

Sub green_update() 


Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet 

Set wb = ThisWorkbook 
Set ws1 = wb.Sheets("Sheet1") 
Set ws2 = wb.Sheets("Sheet13") 

Dim bil As String 
Dim lastrow As Long 
Dim for_col As Long, i As Long, r As Long, c As Long, colnum As Long 
r = 4: c = 7: colnum = 7 

'mysheets = "sheet11:sheet12:sheet13" 
'i would like to allow vlookup to search through all sheet 11-13 

For for_col = 1 To ws2.Cells("4", Columns.Count).End(xlToLeft).column 
lastrow = ws2.Cells(Rows.Count, "A").End(xlUp).row 

    For i = 1 To lastrow - 3 

    ws1.Cells(r, c).Value = Application.WorksheetFunction.VLookup(ws1.Cells(r, 1).Value, ws2.Range("A1:AI500"), colnum, False) 
    r = r + 1 
    Next 

r = 4 
colnum = colnum + 1 
c = c + 1 

Next 

End Sub 
+1

你說這是你的代碼,如果你不知道它是否有效,我們怎麼知道的? – sktneer

+1

我認爲他試圖在他的問題@sktneer :)中提出一些幽默。我不知道他的工作簿,但可能因爲他的變數而失蹤。 – Mertinc

+1

簡單的例子https://stackoverflow.com/a/42800024/4539709 – 0m3r

回答

1

因爲你完全改變了你的要求..我張貼另一個答案來說清楚。 您的請求仍然不完全清楚,因此有些輸入可能會引用錯誤的目的地,但您可以輕鬆更改這些輸入。 如果你不明白任何部分,請隨時再問一次。

Option Explicit 
Sub green_update() 

Application.ScreenUpdating = False 

Dim zaman As Double 
zaman = Timer 
Dim wb As Workbook, ws1 As Worksheet, wsNames as Worksheet 

Set wb = ThisWorkbook 
Set ws1 = wb.Sheets("Sheet1") 

Dim colNo As Long, OthARowNo As Long, sh1ARowNo As Long 
Dim for_col As Long, i As Long, r As Long, c As Long, colnum As Long 

r = 4: c = 7: colnum = 7 

For Each wsNames In Sheets(Array("sheet11", "sheet12", "sheet13")) 
    colNo = wsNames.Cells("4", Columns.Count).End(xlToLeft).column 
    'Column numbers are 35 but you are working between G:AI which is 29 columns! 

    OthARowNo = wsNames.Cells(Rows.Count, "A").End(xlUp).row 
    sh1ARowNo = ws1.Cells(Rows.Count, "A").End(xlUp).row 

    For for_col = 7 To colNo 'colNo Is 35 Green columns start at 7th column, totally 29 loop, till 35th one. 
     For i = 1 To sh1ARowNo 'This should run until sh1's row number 
      ws1.Cells(r, c).Value = Application.VLookup(ws1.Cells(r, 1).Value, wsNames.Range("A1:AI" & OthARowNo), colnum, False) 
      If IsError(ws1.Cells(r, c).Value) Then 
       ws1.Cells(r, c).Value = "" 
      End If 
      r = r + 1 
     Next i 
     r = 4 
     colnum = colnum + 1 
     c = c + 1 
    Next for_col 
    colnum = 7 
    c = c + 6 'There are 6 columns between AI:AP and BR:BY 
Next wsNames 

Application.ScreenUpdating = True 

MsgBox Format(Timer - zaman, "00.00") & "secs" 
End Sub 
+0

我使用的代碼,它正在運行。但是,列停止在X列時,列未完全填充.vlookup函數不捕獲表11-13中的數據。我所得到的是標準日期「0/1/1990」。並感謝您的最後一個代碼,我確實希望從每張表中的第7列中捕獲數據。 – Carmen

+0

如果它不捕獲所有內容,這是因爲sheet1的D列的非空行號可能小於其他工作表行號。您可以更改該部分以使其適合您的工作表。只要我們無法預測和管理工作簿的每個細節,就必須嘗試弄清楚一些您想要更改的細節。 – Mertinc

+0

我可以在這裏輸入拉斯特羅代碼嗎? lastrow = Cells(Rows.Count,1).End(xlUp).row因此它運行到D列的最後一行然後移動到E列等等? – Carmen

1

我解釋的代碼中我的答案,但總結你的問題:

你沒有定義的變量,尤其是工作表。不要假設你的工作,總是定義,並設置爲工作簿和工作表引用

2-就會限制你的For loops與第三行的列數和列數行數,但如果是空的或不兼容什麼用你的查找輪?然後你可能會得到錯誤或錯誤的結果。仔細定義它們。

Option Explicit 

Sub green_update() 
Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet 

Set wb = ThisWorkbook 
Set ws1 = wb.Sheets("Sheet1") 'Change this Sheet1 name with your current Worksheet name 
Set ws2 = wb.Sheets("mysheets") 

Dim bil As String 'I don't know where do you use that variable. 
Dim lastrow As Long 'Prefer to work with Long instead of Integer 
Dim for_col As Long, i As Long, r As Long, c As Long, colnum As Long 
r = 4: c = 7: colnum = 7 


    For for_col = 1 To ws2.Cells("4", Columns.Count).End(xlToLeft).Column 
'This is important! Here in this case are you sure you, _ 
'you would like to define how many times your For loop will run based on 3rd row?  

lastrow = ws2.Cells(Rows.Count, "A").End(xlUp).Row 

'This is also important! Are you sure you would like to run your For loop_ 
'based on the row number of A column? I think you should define it -3 _ 
'because you start your lookup from D4 (so first 3 one Is not necessary) 

     For i = 1 To lastrow - 3 
      ws1.Cells(r, c).Value = WorksheetFunction.VLookup(ws1.Cells(r, 4).Value, ws2.Range("A1:AI500"), colnum, False) 
      r = r + 1 
     Next 
r = 4 
colnum = colnum + 1 
c = c + 1 

    Next 
End Sub 
+0

如果我這樣定義它會更好嗎?我很新,很想得到指導。因此,主要目標是從G列到AI的信息從3張單張紙到1張主紙張。 我想從上到下循環整個vlookup公式,然後從列G到列AI從左到右。我的標題在第3行停止,因此隨後可用的行是第4行。 我已經合併了您的解決方案,但在行 For_col = 1至ws.Cells(「3」,Columns.Count).End(xlToLeft)。列 它顯示當我運行它時,「對象是必需的」。 – Carmen

+0

@Carmen你應該看看** VBA最佳實踐**,這幫助我/並且仍然幫助我很多。[https://stackoverflow.com/documentation/excel-vba/1107/vba-best-practices#t = 201705250528371943986] – Mertinc

+0

謝謝你的建議!一定會看看它。但同時,解決當前字符串的任何其他建議?真的需要一個快速解決方案。 :) – Carmen