Restricting Macro to Only Accept New Data From Users

Viewed 27

I have created the macro to import CSV data without an issue, but I'm worried the end users will accidentally import an old CSV and screw up the data.

I need to restrict the user from being unable to import old data. So a simple if/else statement to error out if the date in the first cell is older than what is the last cell in the current table.

After:

Set OpenBook = Application.Workbooks.Open(FileToOpen)

I tried using this:

If OpenBook.Sheet1.Range(“B2”).Value >= thisWorkbook.Worksheets(“Home”).Range(“C4”).Value Then 

But, I ended up getting a "run-time error '438', Object doesn't support this property or method".

Here is my original VBA macro without any CSV import validation:

Sub UpdateServicesData()
    
    Dim FileToOpen As Variant
    Dim OpenBook As Workbook

    FileToOpen = Application.GetOpenFilename("CSV or Text Files:,*.csv;*.txt*", , "Browse for your File to Import")
    If FileToOpen <> False Then
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        OpenBook.Sheets(1).Range("A2:L10000").Copy

        Workbooks("TEB.xlsm").Activate
        Sheets("Services Data").Select
        lrTarget = Cells.Find("*", Cells(2, 1), xlFormulas, xlPart, xlByRows, xlPrevious, False).Row
        Cells(lrTarget + 1, 1).Select
        ActiveSheet.PasteSpecial Columns("A:L").AutoFit
        Cells(1, 1).Select
        OpenBook.Close False
    End If
    
End Sub

Any feedback would be appreciated!

0 Answers
Related