как добавить дату в качестве параметра в Excel VBA - PullRequest
0 голосов
/ 09 октября 2018

в приведенном ниже коде VBA, как параметризованный "09-Oct-18" и имя файла должно быть _.xlsx из листа в Excel 2016.

column A date; column B filename
09-Oct-18      CATEGORY_99_TAS_09-10-2018.xlsx
15-Oct-18      CATEGORY_99_TAS_15-10-2018.xlsx

Код:

If Format(Range("A" & i).Value, "d-mmm-yy") = "09-Oct-18" Then
Range("B" & i).Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(R2C1,'D:\test\files\[CATEGORY_99_TAS_09-10-2018.xlsx]Sheet1'!R2C1:R30C2,2,FALSE)"

Else
   If Format(Range("A" & i).Value, "d-mmm-yy") = "15-Oct-18" Then
    Range("B" & i).Select
    ActiveCell.FormulaR1C1 = _
            "=VLOOKUP(R2C1,'D:\test\files\[CATEGORY_99_TAS_15-10-2018.xlsx]Sheet1'!R2C1:R30C2,2,FALSE)"

1 Ответ

0 голосов
/ 09 октября 2018

Я не знаю, что должна делать эта часть ... поэтому, я думаю, вы хотите проверить, совпадает ли дата в столбце A с датой в имени листа в столбце b:

If Format(Range("A" & i).Value, "d-mmm-yy") = Format(CDate("18-Oct-15"), "d-mmm-yy") Then

На основеВаш комментарий, я думаю, это может сработать.Если у вас есть дата в столбце A, а затем имя листа в столбце B, код макроса увидит, совпадает ли дата в столбце A с датой в имени листа.Если они равны, он напечатает формулу, которую вы можете увидеть в B2.В противном случае он перейдет к следующему ряду.enter image description here

Sub Macro1()

Dim FileNameVal As String
Dim FileNameDate As String
Dim YearVal As Integer
Dim MonthVal As Integer
Dim DayVal As Integer
Dim DateCorrect As Date

For i = 2 To 10 'Start loop from row 2 to 10, where every step is row (i)

On Error GoTo Handler 'If cell is empty, then go to Handler:
    FileNameVal = Range(Cells(i, "B"), Cells(i, "B")).Value 'Take value in Column  B and row i.
    If FileNameVal = "" Then 'If cell is empty then go to Handler2:
        On Error GoTo HandlerPart2
    Else
    FileNameDate = Split((Split(Range(Cells(i, "B"), Cells(i, "B")).Value, ".xlsx")(0)), "CATEGORY_99_TAS_")(1) 'Split out the date in the filename
    YearVal = Right(FileNameDate, 4) 'Find Year
    MonthVal = Mid(FileNameDate, 4, 2) 'Find Month
    DayVal = Left(FileNameDate, 2) 'Find Day
    DateCorrect = DateSerial(YearVal, MonthVal, DayVal) 'Rearrange date from dd/mm/yyy -> yyyy/mm/dd

        If Format(Range("A" & i).Value, "d-mmm-yy") = Format(CDate(DateCorrect), "d-mmm-yy") Then 'Compare the date in column A with the date in column b (date in sheet name), if they are equal then
            Range("B" & i).Select 'Select column B and row (i), (this will overwrite the sheet name, I would suggest to use column D)
            ActiveCell.FormulaR1C1 = _
                "=VLOOKUP(R2C1,'D:\test\files\[" & FileNameVal & "]Sheet1'!R2C1:R30C2,2,FALSE)" ' Print out vlookup formula based on the sheetname.

        Else
Handler:
HandlerPart2:
        'Do Nothing
        End If
    End If
Next i
End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...