在VBA中声明variables来分析string(但抵消看数字!)

在下面的代码中,我已经声明了c被使用,但它似乎只能用于数字。 我正在寻找一个string与另一个表中的另一个string进行比较。

这个棘手的问题是,我想将值偏移到左边的单元格,然后比较一个date和另一个date。 为了更清楚,我的数据格式如下:(称之为SetA)

ABCDEFGHI CreateTime LotID AnalysisSet Slot# XY ResultType TermName ResultName 

再往右几个列….我们称之为SetB

  YZ AA AB AC AD AE AF AG CreateTime LotID AnalysisSet Slot# XY ResultType TermName ResultName 

这里是代码:

 Sub AIM36DBOCompare() Dim n As Integer n = 2 Dim PasteCount As Integer Dim test1 As Long Dim test2 As Long PasteCount = 41 Range("AD2:AD1000").Copy Destination:=Range("S41" & Lastrow) Do While n <= 1000 If Cells(26, n) <> 0 Then '-------------------------------------------- With Worksheets(1).Range("b2:b10000") Set c = .Find(Cells(n, 26).String, LookIn:=xlValues) If Not c Is Nothing Then Do test1 = Cells(n, 25).Value test2 = c.Offset(0, -1) If Abs(Cells(n, 25) - c.Offset(0, -1)) <= 100 Then c.Offset(0, -1).Copy Cells(PasteCount, 17).Select Selection.Paste PasteCount = PasteCount + 1 Exit Do End If Set c = .Find(Cells(n, 26).Value, LookIn:=xlValues) Loop While Not c Is Nothing End If End With '-------------------------------------------- End If If Cells(11, n) = 0 Then Exit Do End If n = n + 1 Loop End Sub 

所以更清楚的是代码:在SetB中findLotID,在SetA中find第一个(多个)对应的LotID。 将SetB中的date与SetA中的date进行比较,方法是将variables偏移一个单元格,然后减去两个(使用绝对值)。

如果是在100天以内,那么做东西!

它没有做的东西:(。在SetA的date显示为无。我认为这是因为正在使用的variables,请帮助!

If Abs(Cells(n, 25) - c.Offset(-1, 0)) <= 100 Then代码在Test2(因此)在“if test”

在这里输入图像说明

你提到你想把单元格放在c的左边。 在代码中,您正在从上面的单元格获取值。 要看到这个,请尝试下面的代码:

 Sub test() Dim add As String Dim c As Range Dim offsetRange As Range Set c = Range("B2") Set offsetRange = c.Offset(-1, 0) add = offsetRange.Address Debug.Print add Debug.Print offsetRange.Value End Sub 

偏移量先行,然后是列,所以你想要交换到0和-1,如下所示:

 Sub test2() Dim add As String Dim c As Range Dim offsetRange As Range Set c = Range("B2") Set offsetRange = c.Offset(0, -1) add = offsetRange.Address Debug.Print add Debug.Print offsetRange.Value End Sub 

此外,您正在声明并获取variablestest1和test2的值,但在进行比较时不使用这些值。