简体   繁体   中英

How to Format Date Column in Excel VBA

I have an Excel WorkSheet.I am going to populate Recordset from Sql Data using vba Macro.It works fine but I would like to know how to format Date Column as "dd/MM/yyyy" from (E5:E10).I have tried below code but date format is not working.Please advsie how to do that....

Private Sub CmdShowDetails_Click()

Dim ConKha As New ADODB.Connection
Dim TmpRst As New ADODB.Recordset
ConKha.Open "Provider=SQLOLEDB.1;Password=Pass@123;Persist Security Info=True;User ID=sa;Initial Catalog=GloveERP;Data Source=Arindsql"
ConKha.CommandTimeout = 600
SqlStr = "Select 'Outward' SupplyType,'Job Work' SubType,'Others' DocType,A.GatePassNo DocNo,Convert(Char(10),A.GatePassDate,103) DocDate,'K H EXPORTS INDIA PRIVATE LIMITED GLOVE DIVISION' From_OtherPartyName,'33AAACR1714R1ZA' From_GSTIN,'142/1 Trunk Road' From_Address1 ,'Perumugai' From_Address2,'Perumugai' From_Place,'632009' From_PinCode,'TAMIL NADU' From_State,"
SqlStr = SqlStr & "Replace(Replace(Replace(Replace(Replace(REPLACE(REPLACE( REPLACE( REPLACE( B.CompanyName, '!', ' ' ), '#', ' ' ), '$', ' ' ), '&', ' '), '.', ' '),',',' '),';',' '),':',' '),'*',' ') "
SqlStr = SqlStr & " To_OtherPartyName,B.GSTNo TO_GST,B.Address1 + ' ' + B.Address2 To_Address1 ,B.Address3 + ' ' +  B.Address4 To_Address2,'' To_Place,'632009' To_PinCode,'Tamil Nadu' To_State,C.ItemDescriptions Product,C.Purpose [Description],C.HSNCode,D.UoMName Unit,C.Quantity, (C.Quantity * C.Rate) + ((C.Quantity * C.Rate) * (C.GSTPercentage/100))  AssesableValue,'9+9+0+0' TaxRate, 0 CGSTAmount, 0 SGSTAmount,0 IGSTAmount,0 CESSAmount,'Road' TransMode,10 Distance,"
SqlStr = SqlStr & " '' TransName,'' TransId,'' TransDocNo,Convert(Char(10),A.GatePassDate,103) TransDate,A.VehicleNo,'' ErrorList from Material_Trn_GatePassHeader A Join Common_Ref_Company B On (B.CompanyId = A.SupplierId) Join Material_Trn_GatePassDetails C On (C.GatePassHeaderId = A.GatePassHeaderId) Join Common_Ref_UoM D On (D.UoMID = C.UOMId) Where (A.GatePassNo Between '" & TxtFromGPNo.Text & "' And '" & TxtToGPNo.Text & "')"
TmpRst.Open SqlStr, ConKha, adOpenForwardOnly

Worksheets("eWayBill").Range("A4").CopyFromRecordset TmpRst

Worksheets("eWayBill").Range("E4:E10").NumberFormat = "dd/mm/yyyy"

TmpRst.Close
ConKha.Close

End Sub

The slashes are interpreted in a special way.

Try this:

.NumberFormat = "dd\/mm\/yyyy"

to force showing the slashes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM