Datum formata VBA - Kako promijeniti format datuma u VBA kodu?

Excel VBA format datuma

Za formatiranje datuma u VBA koristimo samu ugrađenu funkciju FORMAT, uzima unos kao format datuma i vraća željeni format, argumenti potrebni za ovu funkciju su sam izraz i vrsta formata.

Formatiranje datuma i vremena osjetljive su stvari u Excelu, a isto se odnosi i na VBA. Zadani datum i vrijeme temelje se na datumu sustava na kojem radimo, a koji se mogu razlikovati od sustava do sustava. U ovom ćemo vam članku pokazati različite tehnike formatiranja datuma s VBA kodovima.

Da bismo promijenili format datuma pomoću VBA kodiranja, moramo biti svjesni koji su formati datuma i njegov utjecaj na datum.

Tablica u nastavku prikazuje različito oblikovanje datuma i njihove kodove.

Dakle, ako imate na umu da gornji grafikon formatiranje datuma putem VBA kodiranja uopće nije težak zadatak.

Kako promijeniti format datuma u VBA?

Ispod su primjeri excel formata datuma VBA.

Primjer # 1

Na primjer, imamo isti datum u više ćelija radnog lista, kao što je prikazano u nastavku.

Sada ćemo primijeniti različite formate datuma za isti datum da bismo vidjeli utjecaj na različite kodove formata datuma.

Prvo kopirajte iste podatke i u sljedeći stupac da biste vidjeli utjecaj.

Za prvi datum, tj. Ćeliju A1 primijenit ćemo format "DD-MM-GGGG" .

U kodu prvo moramo odabrati ćeliju pomoću objekta RANGE .

Kodirati:

Sub Date_Format_Example1 () Raspon ("A1") Kraj Sub

Budući da mijenjamo format datuma ćelije, moramo pristupiti svojstvu " Format broja " objekta RANGE.

Kodirati:

Sub Date_Format_Example1 () Raspon ("A1"). NumberFormat End Sub

Nakon pristupanja " Formatu broja ", moramo postaviti format broja stavljanjem znaka jednakosti i primijeniti kôd formata u dvostruke navodnike.

Kodirati:

Sub Date_Format_Example1 () Raspon ("A1"). NumberFormat = "dd-mm-yyy" 'Ovo će datum prebaciti na "23-10-2019" Kraj Sub

Kada izvršimo ovaj kôd, primijenit će format broja na ćeliju A1 kao " DD-MM-GGG".

Izlaz:

Primjer # 2

Slično tome, primijenio sam različite kodove oblikovanja i za druge ćelije, a dolje je VBA kôd za vas.

Kodirati:

Sub Date_Format_Example2() Range("A1").NumberFormat = "dd-mm-yyy" 'This will change the date to "23-10-2019" Range("A2").NumberFormat = "ddd-mm-yyy" 'This will change the date to "Wed-10-2019" Range("A3").NumberFormat = "dddd-mm-yyy" 'This will change the date to "Wednesday-10-2019" Range("A4").NumberFormat = "dd-mmm-yyy" 'This will change the date to "23-Oct-2019" Range("A5").NumberFormat = "dd-mmmm-yyy" 'This will change the date to "23-October-2019" Range("A6").NumberFormat = "dd-mm-yy" 'This will change the date to "23-10-19" Range("A7").NumberFormat = "ddd mmm yyyy" 'This will change the date to "Wed Oct 2019" Range("A8").NumberFormat = "dddd mmmm yyyy" 'This will change the date to "Wednesday October 2019" End Sub

The result of this code will be as follows.

Output:

Change Date Format by Using FORMAT Function

In VBA, we have a function called FORMAT, which can be used to apply the desired format to the cell.

We just need to specify what the value is for “Expression” and apply the “Format” accordingly.

Look at the below code for an example.

Code:

Sub Date_Format_Example3() Dim MyVal As Variant MyVal = 43586 MsgBox Format(MyVal, "DD-MM-YYYY") End Sub

In the above code, I have defined the variable as variant (which can hold any value).

Code:

Dim MyVal As Variant

Next for this variable, I have assigned the value as 43586.

Code:

MyVal = 43586

Next, in the message box, I have shown the result of the variable, but before we show the result, we have used the “FORMAT” function to format the value of the variable “MyVal,” and the format is given. “DD-MM-YYYY.”

Code:

MsgBox Format(MyVal, "DD-MM-YYY")

Ok, let’s run the code and see the result in the message box in VBA.

Output:

As you can see above, the result shows as “01-05-2019”.

Now you must be wondering we have supplied the serial number but the result showing as the date. This is because Excel stores the date as serial numbers, so the value 43586 is equal to the date “01-05-2019,” and if you increase the number by 1 i.e., 43587, the date will be “02-05-2019”.

Code:

Sub Date_Format_Example3 () Zatamni MyVal kao varijantu MyVal = 43586 MsgBox Format (MyVal, "DD-MM-YYY") Kraj Sub

Stvari koje treba zapamtiti

  • Zadani datum vašeg sustava primijenit će se i na vaš Excel.
  • Svojstvo Format broja može se koristiti za promjenu formata datuma u VBA.
  • Pomoću funkcije FORMAT možemo promijeniti format datuma.
  • Excel pohranjuje datum kao serijski broj i ako primijenite format datuma, prikazat će se u skladu s tim.

Zanimljivi članci...