1
我有兩個文件可以使用。一個是要更新的文件,另一個包含新的數據。如果跨工作簿匹配,複製行的部分
這兩個文件都包含材料編號和關於這種材料(數量等)的信息,所以我想要將所有新值(它們是同一行的一部分)複製到另一個文件中該行的正確位置。
我設法讓除NVOOKUP以外的所有東西都能正常工作(應該需要..)。
有人嗎? :)
'################################################################################################
'################################################################################################
'######### fnopen(): Opens a FileDialog, allowing the user to choose the GLA File. #########
'######### Returns directory/filename of selected as String #########
'################################################################################################
'################################################################################################
Function fnopen() As String
Dim strFileToOpen As String
strFileToOpen = Application.GetOpenFilename _
(Title:="Please choose GLA501 to open")
'# Change name of FileDialog (is being displayed)
Workbooks.Open filename:=strFileToOpen
'# Open Workbook
fnopen = strFileToOpen
MsgBox fnopen & " 1"
MsgBox strFileToOpen & " 2"
End Function
'################################################################################################
'################################################################################################
'################################################################################################
'######### MakeRow(): Creates String of Cell out of row and String #########
'################################################################################################
Function MakeRow(rowno As Integer, col As String) As String
MakeRow = col & CStr(rowno)
End Function
'################################################################################################
'######### getmat(): Requires row no. and returns material no. #########
'################################################################################################
Function getmat(rowno As Integer, col As String) As String
getmat = Range(MakeRow(rowno, col)).Value
End Function
'################################################################################################
'################################################################################################
Function fcat(gla_path As String, gla_name As String, lastrow As Integer) As Integer
Dim srchRange As Range, found_in_location As Range, lookFor As Range
Dim rowno As Integer, counter As Integer
Dim col As String
rowno = 16
col = "F"
counter = 0
Dim book1 As Workbook
Dim book2 As Workbook
Set book1 = ThisWorkbook
Set book2 = Workbooks(gla_name)
Set lookFor = book2.Sheets(1).Cells(rowno, 6) ' value to find
Set srchRange = book1.Sheets(2).Range(MakeRow(rowno, col), MakeRow(lastrow, col)) 'source
Set found_in_location = srchRange.Columns(1).Find(What:=lookFor, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not found_in_location Is Nothing Then
found_in_location.Offset(0, 85).Value = lookFor.Offset(0, 79)
Else
counter = counter + 1
End If
fcat = counter
End Function
'################################################################################################
'################################################################################################
Sub annualazy()
Dim gla_path As String, gla_name As String, col As String, rowno As Integer, counter As Integer, lastrow As Integer
MsgBox ("This VBA updates 'DC_Annual_Planning' by copying values from '4510_GLA501_DC'. Make sure to select the correct files!")
gla_path = fnopen()
gla_name = Right(gla_path, Len(gla_path) - InStrRev(gla_path, "\"))
rowno = 16
col = "F"
lastrow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
MsgBox lastrow
MsgBox gla_name
MsgBox getmat(rowno, col)
MsgBox fcat(gla_path, gla_name, lastrow)
End Sub
非常感謝您的幫助,只是一個問題:我是對的,這將取距離found_in_location 2個單元格的單元格的值,並將其粘貼到單元格右邊的79個單元格中,右鍵單擊? – Lukas
是的,這是正確的(那些是if語句第一部分的偏移量)。如果你想嘗試一下,你總是可以製作一份你的項目的副本,可能會在代碼中包含一些'MsgBox'es或'Debug.Print'-語句,以幫助您瞭解它發生在不同的地方。哦,如果你覺得這個答案令人滿意,如果你通過檢查它的頂部旁邊的標記(在箭頭下)來標記它,我將不勝感激@Lukas – eirikdaude
剛剛測試過它......它運行!非常感謝:) – Lukas