Extra subroutine to crop values of one column to 10 characters

Retroshift

Board Regular
Joined
Sep 20, 2016
Messages
119
Office Version
  1. 2019
Platform
  1. Windows
Hi,
I have this macro (see below - code scripting credited to member rlv01) and I would like to add a subroutine which crops all the values of column O to 10 characters only.
So I want to only keep the first 10 characters of the data in every cell of the single column O. For example: if the macro imports "16/05/2022 11:00:00" into a cell in column O, the amount of characters should get cropped down to only the date (10 characters in this case) and not the time, so "16/05/2022".
And when a value of "16/05/2022" gets imported (merely the date (10 characters)), it is fine and it can stay there without any VBA action to it.
I am not a VBA virtuoso so any help with the scripting would be appreciated.

VBA Code:
Sub RefreshClearCopyColumnsData()
    Dim wkBk As Workbook, wkBk2 As Workbook
    Dim wkSht As Worksheet, wkSht2 As Worksheet
    Dim FullPathName As String
    Dim ColArr, ColLetter
    Dim rng As Range
    Dim I As Long, lastrow As Long
    
    'First refresh the open workbook to update all the values
    
    Set wkBk = ActiveWorkbook
    wkBk.RefreshAll
    
    'Then clear the contents of the worksheet in the open workbook
    Set wkSht = wkBk.Worksheets("Sheet1")
    wkSht.UsedRange.ClearContents
    
    'Then get columns data from closed workbook into open workbook
    'Get path and Closed Workbook filename from (indirect?) cell A5 in open workbook
    
    FullPathName = wkBk.Worksheets("Sheet2").Range("A5").Value
    With CreateObject("Scripting.FileSystemObject")
        If Not .FileExists(FullPathName) Then
            MsgBox "File does not exist - abort"
            Exit Sub
        End If
    End With

    Set wkBk2 = Workbooks.Open(FullPathName)
    Set wkSht2 = wkBk2.Sheets(1)
    lastrow = wkSht2.UsedRange.Rows.Count
    
    'Specify the columns until the last row with data
    ColArr = Array("A", "O", "R", "V", "AD", "AG", "AH")
    
    For Each ColLetter In ColArr
        With wkSht2
            I = I + 1
            If I = 1 Then
                Set rng = Application.Intersect(.UsedRange, .Range(ColLetter & "1", .Range(ColLetter & lastrow)))
            Else
                Set rng = Application.Union(rng, Application.Intersect(.UsedRange, .Range(ColLetter & "1", .Range(ColLetter & lastrow))))
            End If
        End With
    Next ColLetter
    
    wkSht.Activate
    rng.Copy wkSht.Range("C2")
    wkBk2.Close False
    
    'Add message box
    MsgBox "Data successfully imported"
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
If sheet1 was your answer then ...

wkSht.Columns("O").NumberFormat = "yyyy-mm-dd"
 
Upvote 0
Indeed, sheet1 is the answer (column O from workbook2 is copied into a sheet of workbook1 with range C2).

The following code "wkSht.Columns("O").NumberFormat = "yyyy-mm-dd"" is highlighted in yellow in VBA and returns nothing.
 
Upvote 0
So what column of wkBk.Worksheets("Sheet1") are you trying to change? Column D?
 
Upvote 0
So what column of wkBk.Worksheets("Sheet1") are you trying to change? Column D?
Six columns ( "A", "O", "R", "V", "AG", "AH" ) from workbook2 are imported into a range of wkBk.Worksheets("Sheet1") starting from cell C2 on "Sheet1".
Column O is the second column, so in Sheet1!C2 this becomes column D.
But I tried to change the "O" to column "D" as well and it did not trim the dates either.
 
Upvote 0
Can you post what you have for Sheet1 with XL2BB so we can see exactly what it looks like?
 
Upvote 0
Can you post what you have for Sheet1 with XL2BB so we can see exactly what it looks like?
testmacroblad mrexcel new.xlsm
ABCDEFGHIJKLMNOP
1
2DocRef TypeFinal DeadlineNBPagesStepStatus
3WD2022-05-16 11:000,5TRACTIVE
4SN2022-05-16 12:001,7TRACTIVE
5ST2022-05-16 12:006,3TRACTIVE
6WD2022-05-16 12:000,5TRACTIVE
7WD2022-05-16 12:000,5TRACTIVE
8WD2022-05-16 12:002,6TRACTIVE
9WD2022-05-16 12:000,5TRACTIVE
10WD2022-05-16 13:000,5TRACTIVE
11WD2022-05-16 13:000,5TRACTIVE
12WD2022-05-16 13:000,5TRACTIVE
13WD2022-05-16 13:000,6TRACTIVE
14CM2022-05-16 17:003,5TRACTIVE
15CM2022-05-16 17:004,2TRACTIVE
16WD2022-05-16 17:003,6TRACTIVE
17SN2022-05-16 23:59FP15,5TRACTIVE
18SN2022-05-16 23:590,5TRACTIVE
19SN2022-05-16 23:590,5TRACTIVE
20SN2022-05-16 23:5910,4TRACTIVE
21SN2022-05-16 23:593,3TRACTIVE
22SN2022-05-16 23:595,4TRACTIVE
23SN2022-05-16 23:598,7TRACTIVE
24SN2022-05-16 23:59COD0,5TRACTIVE
25ST2022-05-16 23:590,5TRACTIVE
26ST2022-05-16 23:590,5TRACTIVE
27ST2022-05-16 23:5911,5TRACTIVE
28ST2022-05-16 23:591,5TRACTIVE
29ST2022-05-16 23:590TRACTIVE
30ST2022-05-16 23:590TRACTIVE
31SN2022-05-17 12:000,9TRACTIVE
32
33
Blad1
 
Upvote 0
This was the mini sheet version. I clicked on the mini sheet button. This is a mini sheet version again:

testmacroblad mrexcel new.xlsm
ABCDEFGHIJKL
1
2DocRef TypeFinal DeadlineNBPagesStepStatus
3WD2022-05-16 11:000,5TRACTIVE
4SN2022-05-16 12:001,7TRACTIVE
5ST2022-05-16 12:006,3TRACTIVE
6WD2022-05-16 12:000,5TRACTIVE
7WD2022-05-16 12:000,5TRACTIVE
8WD2022-05-16 12:002,6TRACTIVE
9WD2022-05-16 12:000,5TRACTIVE
10WD2022-05-16 13:000,5TRACTIVE
11WD2022-05-16 13:000,5TRACTIVE
12WD2022-05-16 13:000,5TRACTIVE
13WD2022-05-16 13:000,6TRACTIVE
14CM2022-05-16 17:003,5TRACTIVE
15CM2022-05-16 17:004,2TRACTIVE
16WD2022-05-16 17:003,6TRACTIVE
17SN2022-05-16 23:59FP15,5TRACTIVE
18SN2022-05-16 23:590,5TRACTIVE
19SN2022-05-16 23:590,5TRACTIVE
20SN2022-05-16 23:5910,4TRACTIVE
21SN2022-05-16 23:593,3TRACTIVE
22SN2022-05-16 23:595,4TRACTIVE
23SN2022-05-16 23:598,7TRACTIVE
24SN2022-05-16 23:59COD0,5TRACTIVE
25ST2022-05-16 23:590,5TRACTIVE
26ST2022-05-16 23:590,5TRACTIVE
27ST2022-05-16 23:5911,5TRACTIVE
28ST2022-05-16 23:591,5TRACTIVE
29ST2022-05-16 23:590TRACTIVE
30ST2022-05-16 23:590TRACTIVE
31SN2022-05-17 12:000,9TRACTIVE
32SN2022-05-17 12:002,1TRACTIVE
33
34
35
Blad1
 
Upvote 0
Last try from me:
VBA Code:
wkSht.Columns("D:D").NumberFormat = "yyyy-mm-dd"
 
Upvote 0
Solution

Forum statistics

Threads
1,224,737
Messages
6,180,668
Members
452,992
Latest member
TokugawaIesuma

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top