1

enter image description hereI'm trying to compare two variables within a macro but those two variables are going to be changing. Here's what I have so far:

Selection.End(xlToRight).Select

Dim Year As Integer
    Year = ActiveCell.Value

ActiveCell.Offset(-1, 0).Select

Dim Year2 As Integer
    Year 2 = ActiveCell.Value

If Year2 <> Year + 1 Then
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Offset(0, -1).Select
    Range(Selection, Selection.End(xlToLeft)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.ClearContents
End If

Okay so basically this is what I'm trying to do. I asked a question already about the first part so sorry for repeating myself but this may lend clarity to this question. Essentially, I have to create a macro that allows the user to choose a file and have it uploaded and appended to the end of a worksheet. However, the data files go in sequential order named "2015 latest.txt", "2016 latest.txt", "2017 latest.txt", etc. I need the macro to stop the data from being appended if the user uploads a file out of sequence. So if the last file uploaded was "2016 latest.txt" and they choose "2018 latest.txt" I need the data to be deleted or the file to be blocked from importing all together. I couldn't figure out how to manipulate the macro as to the name of the file (I tried using a static variable but that didn't work). Instead I just went with the data (since it lists the year) and tried naming two variables and comparing them and then deleting the data if it doesn't match. Here's my full macro:

Option Explicit

Sub ImportTextFile()

Range("A1").Select
   Selection.End(xlDown).Select
   ActiveCell.Offset(1, 0).Select


Dim fName As String, LastRow As Long

fName = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If fName = "False" Then Exit Sub


LastRow = Range("A" & Rows.Count).End(xlUp).Row + 1

With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fName, _
    Destination:=Range("A" & LastRow))
        .Name = "sample"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = True
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
        .Refresh BackgroundQuery:=False
End With

Selection.EntireRow.Delete

Selection.End(xlToRight).Select

Dim Year As Integer
    Year = ActiveCell.Value

ActiveCell.Offset(-1, 0).Select

Dim Year2 As Integer
    Year2 = ActiveCell.Value


If Year2 <> Year + 1 Then
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Offset(0, -1).Select
    Range(Selection, Selection.End(xlToLeft)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.ClearContents
End If



End Sub
  • 2
    I highly suggest reading through [How to Avoid Using `.Select`/`.Activate`](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros) that will help you and help cut your code down. Also you have a typo, `Year 2 = ActiveCell.Value` should just be `Year2 = ...` – BruceWayne Apr 17 '17 at 23:13
  • 2
    Listen to Bruce. Among other things, each time you do a .select, you clear any previous .selects. – Rich Holton Apr 17 '17 at 23:15
  • 1
    The key is *how does the active cell change*? In code, or do you have loop or something that would change the variable value? The link @BruceWayne shared will take you very far, but until we know how the active "is moved" (as you wrote) it's hard to help more. – Scott Holtzman Apr 17 '17 at 23:24
  • I updated the question with more info. – Cameron Kormylo Apr 17 '17 at 23:35
  • Isn't there a date field you could sort on and then remove duplicates on the off-chance that someone managed to push in a file that was out of order? Seems like good pro-active user-repair to me. –  Apr 17 '17 at 23:51

0 Answers0