Extract from year to year Using VBA in Excel

I have an Excel workbook with multiple sheets. What I need to do is transfer the year from the column on one sheet to another sheet. I can get the column to pass the fine to, but I can't get it to give me only the date.

The date is in the format dd-mon-yy(for example, 14-Jul-14). I need to get it in yyyy(like 2014). Any help would be great.

The code I'm trying to do is, but it does not work.

[...]
For I=5 to 5
    If Not TransferCol (5) = 0 Then
        Worksheets ("Results").Cells.(Row, StartColumn + I) = Worksheets("Program").Cells(RowTransfer, Year (TransferCol (5)))
        Exit Do
    End If
Next
[....]
+4
source share
4 answers

It seems to me that there are (at least) 2 ways to achieve the desired.

1. Formatting

, , , . , , . "Sheet1"/Cell(1,1) "Sheet2"/Cell(1,1) NumberFormat, 4- :

Public Sub test1()

  Dim rSrc As Range
  Dim rDst As Range

  Set rSrc = Sheets("Sheet1").Cells(1, 1)
  Set rDst = Sheets("Sheet2").Cells(1, 1)

  rDst = rSrc
  rDst.NumberFormat = "yyyy"

End Sub

2. Year()

, ( ). , , 1901 . . :

Public Sub test2()

  Dim rSrc As Range
  Dim rDst As Range

  Set rSrc = Sheets("Sheet1").Cells(1, 1)
  Set rDst = Sheets("Sheet2").Cells(1, 1)

  rDst = Year(rSrc)

End Sub

, , . , .

+3

Year().

, :

Worksheets ("Results").Cells.(

.

+2

" VBA Excel" ( ), Year() VBA. DatePart DateInterval.Year Interval (re: http://msdn.microsoft.com/en-us/library/88k2aec8%28v=vs.90%29.aspx)

0

Cells , :

Worksheets ("Results").Cells.(Row, StartColumn + I) = Worksheets("Program").Cells(RowTransfer, Year (TransferCol (5)))
                                                                           ^^^^^^^^^^^^^^^^^^^^^^^^

What this means is to extract the year from TransferCol(5)and access the cell value (RowTransfer, extracted year)of the worksheet Program.

You might want to do something like

 Worksheets ("Results").Cells.(Row, StartColumn + I) = Year(TransferCol(5))

if TransferColcontains your date values.

0
source

Source: https://habr.com/ru/post/1548345/


All Articles