Auto-Update Problem

Diulama1023

New Member
Joined
Dec 12, 2023
Messages
1
Office Version
  1. 365
  2. 2021
  3. 2010
Platform
  1. Windows
  2. 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.

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 !
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top