Share via

VBA code to update data on a condition

Anonymous
2023-07-10T19:05:12+00:00

I need a simple macro to update my w/book.

Source w/book > Nifty > File name > Nifty.xlsx > file path> C:\Users\Raju\Dropbox\PC\Desktop> Sheet name>>niftycsv---- data in this book varies daily which I download from web.

Destination W/book >markets >>File name > markets.xlsx >file path > C:\Users\Raju > > Sheet name > Sheet1.---- this w/book has to be updated with Nifty w/book data

Task : If Nifty.xlsx w/sheet niftycsv Cell A2 date value exists in Destination w/book in Column B (entire column) > msg "Date Data exists. Data already Updated".> Exit sub > If Nifty.xlsx w/sheet Cell A2 date value does not exists in destination column B > Copy A2:E (used range data of source file) > paste into Destination file last row (columns B:F)> with msg "Sheet Updated". >>> updated data be visible in destination file > If I rerun the code > msg"Date Data Exists.Data already Updated". should pop up. Vba code be placed in Destination w/book.Thank you Every body. https://1drv.ms/f/s!AkkzGPwDbRX6qkcERy3C4WXneJuK?e=xMP7UI

Microsoft 365 and Office | Excel | For education | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Anonymous
    2023-07-12T15:49:42+00:00

    I have tried again, but still not work.

    You can refer to this article: Office VBA support and feedback | Microsoft Docs to go to Stack Overflow by using the VBA tag, along with any other relevant tags as there are also many experienced engineers and experts in the forums there.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2023-07-11T18:29:14+00:00

    This code will not run. I will try my best to correct it next day.

    ======================================

    Sub ssss()

    Dim SourceWB As Workbook

    Dim SourceSheet As Worksheet

    Dim TargetWB As Workbook

    Dim TargetSheet As Worksheet

    Set SourceWB = Workbooks.Open(Filename:="File Path\Nifty.xlsx")

    Set SourceSheet = SourceWB.Worksheets("niftycsv")

    Set TargetSheet = ThisWorkbook.Worksheets("Sheet1")

    SourceTrow = SourceSheet.Cells(Rows.cout, 1).End(xlUp).Row

    TargetTrow = TargetSheet.Cells(Rows.cout, 2).End(xlUp).Row

    For i = 2 To SourceTrow

    x = 0

    For j = 3 To TargetTrow

    If SourceSheet.Cells(i, 1) = TargetSheet.Cells(j, 2) Then

    x = x + 1

    MsgBox "Date alread exist"

    End If

    Next j

    If x = 0 Then

    TargetTrow = TargetTrow + 1

    SourceSheet.Range(Cells(i, 1), Cells(i, 5)).Copy Destination:=TargetSheet.Range(Cells(TargetTrow, 2), Cells(TargetTrow, 6))

    End If

    Next i

    End Sub

    Was this answer helpful?

    0 comments No comments