/ / Ricerca da cartella di lavoro diversa e copia dei valori - excel, vb.net, vba, excel-vba

Ricerca da cartella di lavoro diversa e copia di valori - excel, vb.net, vba, excel-vba

Ho 2 wb to_update_example_1 e purchasing_list

fondamentalmente quello che sto cercando di fare è un ciclo riga per riga sulla cartella di lavoro to_update_example_1 se si trova lo stesso nome per copiare la variabile su purchasing_list cartella di lavoro.

Tuttavia continua a darmi errore 91 alla parte di ricerca e avrei bisogno di un consiglio come scrivo vVal2(che è la Qtà) alla cartella di lavoro Elenco acquisti, la colonna è proprio accanto al nome trovato, quindi ho provato a utilizzare l'offset delle celle attivo ma non ho funzionato troppo

ogni consiglio è apprezzato grazie

Sub Macro1()

Application.ScreenUpdating = False

Dim x As Integer
Dim vVal1, vVal2 As String

Numrows = Range("A1", Range("A1").End(xlDown)).Rows.Count " Set numrows = number of rows of data.
Range("A1").Select  " Select cell a2.
For x = 1 To Numrows " Establish "For" loop to loop "numrows" number of times.
vVal1 = Cells(x, 8)
vVal2 = Cells(x, 7)

Windows("Purchasing List.xls").Activate

ActiveSheet.Cells.Find(What:=vVal1, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row

""write to Qty cell beside the found name ActiveCell.Offset(0, -2) = vVal2
Windows("To_update_example_1.xlsm").Activate

""""""""ActiveCell.Offset(1, 0).Select
Next
Application.ScreenUpdating = True

End Sub

risposte:

1 per risposta № 1

Quando si usa il Find funzione, è consigliabile se si imposta a Range oggetto al Find risultato, e anche preparare il codice per uno scenario che Find non ho trovato vVal1 in "Purchasing List.xls" cartella di lavoro. Puoi ottenerlo usando la seguente riga If Not FindRng Is Nothing Then.

Nota: evitare di utilizzare Select, Activate e ActiveSheet, invece qualifica completamente tutti i tuoi oggetti - vedi nel mio codice qui sotto (con commenti).

Codice modificato

Option Explicit

Sub Macro1()

Application.ScreenUpdating = False

Dim x As Long, Numrows As Long
Dim vVal1 As String, vVal2 As String
Dim PurchaseWb As Workbook
Dim ToUpdateWb As Workbook
Dim FindRng As Range

" set workbook object of "Purchasing List" excel workbook
Set PurchaseWb = Workbooks("Purchasing List.xls")

" set workbook object of "To_update_example_1" excel workbook
Set ToUpdateWb = Workbooks("To_update_example_1.xlsm")

With ToUpdateWb.Sheets("Sheet1") " <-- I think you are trying to loop on "To_update_example_1.xlsm" file , "<-- change "Sheet1" to your sheet"s name
" Set numrows = number of rows of data.
Numrows = .Range("A1").End(xlDown).Row

For x = 1 To Numrows " Establish "For" loop to loop "numrows" number of times
vVal1 = .Cells(x, 8)
vVal2 = .Cells(x, 7)

" change "Sheet2" to your sheet"s name in "Purchasing List.xls" file where you are looking for vVal1
Set FindRng = PurchaseWb.Sheets("Sheet2").Cells.Find(What:=vVal1, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns)
If Not FindRng Is Nothing Then "<-- make sure Find was successful finding vVal1
" write to Qty cell beside the found name ActiveCell.Offset(0, -2) = vVal2
" Not sure eactly what you want to do now ???

Else " raise some kind of notification
MsgBox "Unable to find " & vVal1, vbInformation
End If
Next x
End With

Application.ScreenUpdating = True

End Sub

1 per risposta № 2

Modificato per tenere conto del commento dell'OP su dove cercare e scrivere valori

ShaiRado ti ha già detto dov'era il difetto

ecco un codice alternativo

Option Explicit

Sub Macro1()
Dim cell As Range, FindRng As Range

Dim purchListSht As Worksheet
Set purchListSht = Workbooks("Purchasing List.xls").Worksheets("purchaseData") "(change "purchaseData" to your actual "purchase" sheet name)

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With Workbooks("to_update_example_1").Sheets("SourceData") " reference your "source" worksheet in "source" workbook (change "SourceData" to your actual "source" sheet name)
For Each cell In .Range("H1", .Cells(.Rows.Count, 8).End(xlUp)).SpecialCells(xlCellTypeConstants) " loop through referenced "source" sheet column "H" not empty cells
Set FindRng = purchListSht.Columns("G").Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns) " try finding current cell content in "purchase" sheet column "G"
If Not FindRng Is Nothing Then FindRng.Offset(, -2).Value = cell.Offset(, -1).Value " if successful, write the value of the cell one column left of the current cell to the cell two columns to the left of found cell
Next
End With
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub