嗨你的錯誤,在這些線路:
.Range(Cells(i, 4) I removed Range()
= y) I removed ")"
Range(Cells(i - 5, 3)) I removed Range()
下面的代碼應該工作
Option Explicit
Sub fetchdata()
Dim x As Integer
Dim y As Integer
Dim finalrow As Long
Dim i As Integer
Dim LastRow As Long
x = Application.Workbooks("Site.csv").Worksheets("Site").Range("A2").Value 'Stores the x-coordinate of the Site file which contains the list of required coordinates
y = Application.Workbooks("Site.csv").Worksheets("Site").Range("B2").Value
finalrow = Application.Workbooks("Main.csv").Worksheets("Main").Range("D70000").End(xlUp).Row 'Stores the row detail of the last row in the Main file
For i = 7 To finalrow
If Application.Workbooks("Main.csv").Worksheets("Main").Cells(i, 4) = x And Cells(i, 5) = y Then 'ERROR IDENTIFIED HERE
Application.Workbooks("Site.csv").Worksheets("Site").Cells(i - 5, 3).Copy
Application.Workbooks("Main.csv").Worksheets("Main").Range("F7").PasteSpecial xlPasteFormulasAndNumberFormats 'Here I basically want to replace the existing site number with that in my reference file (site.csv)
Else
Application.Workbooks("Main.csv").Worksheets("Main").Rows(i).EntireRow.Delete 'Delete Everything else
End If
Next i
End Sub
新代碼的08/12與目錄循環:
Sub fetchdata()
Dim x As Integer
Dim y As Integer
Dim finalrow As Long
Dim i As Integer
Dim site As Workbook
Dim main As Workbook
Dim site_sh As Worksheet
Dim main_sh As Worksheet
Dim LastRow As Long
Dim finalrow_main, finalrow_site, i_site, i_main, site_val_x, site_val_y, main_val_x, main_val_y As Variant
Dim criteria As String
Dim delete_row As Boolean
Dim MyObj As Object, MySource As Object, file As Variant
Dim file_path, list_file, final_message As String
file_path = "C:\Users\u6042371\Documents" 'Set directory for "Main" file types here
If Right(file_path, 1) <> "\" Then file_path = file_path & "\"
list_file = "" 'this will store a file list for later
criteria = "main*.xls" 'this will search for all files beginning with main ending with .xls, you can use * as a wildcard, just change main
file = Dir(file_path & criteria)
While (file <> "")
Workbooks.Open Filename:=file_path & file
Set main = Workbooks(file) 'will auto open
Set site = Workbooks("Site.xlsx") 'manual open this workbook
Set main_sh = main.Worksheets("Main") 'name of sheet ex Main sheet in Main workbook
Set site_sh = site.Worksheets("Site") 'name of sheet ex Site sheet in Site workbook
finalrow_main = main_sh.Range("D70000").End(xlUp).Row 'gets last row of Main Sheet
finalrow_site = site_sh.Range("A70000").End(xlUp).Row 'gets last row of Site Sheet
delete_row = False 'flag if to delete row at the end of for loop
For i_main = finalrow_main To 7 Step -1 'to loop through all Main x, y, this looks through end to start of data, delete technique
main_val_x = main_sh.Cells(i_main, 4).Value 'set x value of current row of Main sheet
main_val_y = main_sh.Cells(i_main, 5) 'set y value of current row of Main sheet
For i_site = 2 To finalrow_site 'to loop through all Site x,y starts at the beginning of site
site_val_x = site_sh.Cells(i_site, 1) 'set x value of current row of Site sheet
site_val_y = site_sh.Cells(i_site, 2) 'set y value of current row of Site sheet
If site_val_x = main_val_x And site_val_y = main_val_y Then 'compares x,y from Site to x,y from Main
main_sh.Cells(i_main, 6) = site_sh.Cells(i_site, 3)
delete_row = False 'Set delete to false because there has been a match
Exit For 'Exits loop to check next site row
Else
delete_row = True 'if there are no matches this will become True
End If
Next i_site
If delete_row = True Then 'if delete = True then delete
main_sh.Rows(i_main).Delete
End If
Next i_main
Workbooks(file).Save
Workbooks(file).Close
list_file = list_file + file + Chr(13)
file = Dir
Wend
final_message = "The following files have been processed:" + Chr(13) + list_file
MsgBox final_message
End Sub
所有符合條件的對象,以他們的父母。你沒有在代碼中指定你想要使用哪個'workbook'。另外'Cells(i,4)'應該限定在工作表中(工作表指向工作簿)。 –
當使用不同的紙張工作,並專門工作簿的最佳實踐,並要求您是明確你的目標。嘗試先修復它們。實例爲工作簿( 「B」)。工作表( 「B」)。範圍( 「A7」)或工作簿( 「A」)。WorkSheets(「A」) – fcsr
@ScottHoltzman進行了更改,請查看 – out1121