Diulama1023
New Member
- Joined
- Dec 12, 2023
- Messages
- 1
- Office Version
- 365
- 2021
- 2010
- Platform
- Windows
- MacOS
Hello I am new to here and new to VBA, I have tried to make my code using some knowledge and chatGPT, however I have managed to updated only one row and please help me with the following problem.
I have two workbooks (obviosly) ,I got two file, one named "TV" and the other file is called "update", which consist of the entry that i have to update,
the "TV" file contains A-CD column, while the "update" file contain only A-I, 9 columns
The two file are actually similar, both A-E columns are the same, column C contains a unique ID as locator, but the columns have to be updated are "main" file AB-AE column, the things can be directly pasted from F-I column from "update" file.
The manual routine i would do is, copy C column and finding the place the ID located in "main" file, then copying F-I column from "update" file and replace "main" file AB-AE column, the row nunmber should be depands on where the ID is located
Now the code can only update C3 as it is my level and with the help of ChatGPT, is it possible to extend the code to create a loop to search all the cells from "update" C2 until C is empty, thank you so much !
VBA Code:
Sub FindAndCopy()
Dim searchValue As Variant
Dim targetWorkbook As Workbook
Dim targetSheet As Worksheet
Dim resultColumn As Long
Dim resultRow As Long
Dim searchRange As Range
Dim resultCell As Range
' Set the search value to the value in C3 of the current worksheet
searchValue = ActiveSheet.Range("C3").Value
' Set the target workbook and sheet
Set targetWorkbook = Workbooks.Open("TV.xls") ' Update with the correct file path
Set targetSheet = targetWorkbook.Sheets(2) ' Update with the correct sheet index or name
' Set the search range to column C of the target sheet
Set searchRange = targetSheet.Range("C:C")
' Loop through each cell in the search range
For Each resultCell In searchRange
' Check if the cell value matches the search value
If resultCell.Value = searchValue Then
' Get the column number and row number of the found value
resultColumn = resultCell.Column
resultRow = resultCell.Row
' Copy the range F3:I3 from the sheet where the macro is initiated
ThisWorkbook.ActiveSheet.Range("F3:I3").Copy
' Calculate the paste column as AB (column 28)
Dim pasteColumn As Long
pasteColumn = 28 ' Column AB
' Paste the copied range to the target sheet at the corresponding row and column
targetSheet.Cells(resultRow, pasteColumn).PasteSpecial
Application.CutCopyMode = False ' Clear the clipboard
Exit Sub ' Exit the loop if a match is found
End If
Next resultCell
' If no match is found, display a message box
MsgBox "Value not found in the current worksheet"
End Sub
I have two workbooks (obviosly) ,I got two file, one named "TV" and the other file is called "update", which consist of the entry that i have to update,
the "TV" file contains A-CD column, while the "update" file contain only A-I, 9 columns
The two file are actually similar, both A-E columns are the same, column C contains a unique ID as locator, but the columns have to be updated are "main" file AB-AE column, the things can be directly pasted from F-I column from "update" file.
The manual routine i would do is, copy C column and finding the place the ID located in "main" file, then copying F-I column from "update" file and replace "main" file AB-AE column, the row nunmber should be depands on where the ID is located
Now the code can only update C3 as it is my level and with the help of ChatGPT, is it possible to extend the code to create a loop to search all the cells from "update" C2 until C is empty, thank you so much !