如何在更改活动单元格后lockingVBA中的“ActiveCell.Vale”variables?

我试图比较macros中的两个variables,但这两个variables将会改变。 以下是我到目前为止:

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 

好吧,基本上这就是我想要做的。 我已经提出了一个关于第一部分的问题,所以很抱歉重复自己,但这可能会澄清这个问题。 实质上,我必须创build一个macros,允许用户select一个文件,并将其上传并附加到工作表的末尾。 但是,这些数据文件按照“2015 latest.txt”,“2016 latest.txt”,“2017 latest.txt”等顺序排列。如果用户上传文件,我需要macros停止附加数据不按顺序。 因此,如果最后上传的文件是“2016 latest.txt”,并select“2018 latest.txt”,我需要删除数据或阻止文件一起导入。 我无法弄清楚如何操纵macros的文件名(我尝试使用一个静态variables,但没有工作)。 相反,我只是去了数据(因为它列出了年份),并尝试命名两个variables,并比较它们,然后删除数据,如果不匹配。 这是我的全部macros:

 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