Excel is interpreting date wrong - excel

I have a problem. I have data in this format:
13.3.19 00:23:01
I use a macro to import it to one tab, copy it to another tab, and replace the "." with "/" so it in the correct format. But excel said no and interprets most of the data as text, which is not a problem, I just use datevalue on that. But when it comes to this date in particular:
12.3.19 23:52:41
Excel is interpreting it as a date in the US format and instead of leaving it as march the 12th, it makes December the 3rd out of it. This renders the datevalue useless in just a part of my data set.
Any thoughts?
Code of the macro here:
Sub import_data()
Path = Worksheets("Macro").Cells(6, 4).Value
Analysis = ThisWorkbook.Name
Rfrom = Sheets("Macro").Cells(8, 4)
Rto = Sheets("Macro").Cells(9, 4)
Application.DisplayAlerts = False
For Data_Range = Rfrom To Rto
Fname = Sheets("Macro").Cells(Data_Range, 3)
Segment_name = Sheets("Macro").Cells(Data_Range, 4)
'selecting workbook
Workbooks.Open Filename:= _
Path & "\" & Fname _
Sheets(Segment_name).Select
Range("A2:W14000").Select
Selection.Copy
Windows(Analysis).Activate
Sheets("Raw_data_import").Select
Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(Fname).Activate
ActiveWindow.Close
Windows(Analysis).Activate
Next Data_Range
Windows(ThisWorkbook.Name).Activate
Sheets("Raw_data_import").Activate
Range("E:G").Select
Selection.Copy
Sheets("Priprava_dat").Select
Range("A:C").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Priprava_dat").Range("A:B").Replace ".", "/"
Dim lastRow As Long
lastRow = Range("C" & Rows.Count).End(xlUp).Row
Range("D2").AutoFill Destination:=Range("D2:D" & lastRow)
Range("E2").AutoFill Destination:=Range("E2:E" & lastRow)
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)
Range("G2").AutoFill Destination:=Range("G2:G" & lastRow)
Range("H2").AutoFill Destination:=Range("H2:H" & lastRow)
Range("I2").AutoFill Destination:=Range("I2:I" & lastRow)
Range("J2").AutoFill Destination:=Range("J2:J" & lastRow)
Windows(ThisWorkbook.Name).Activate
Sheets("Macro").Activate
End Sub

you can set the number format of the cells using
Sheets("Priprava_dat").Range("A:B").NumberFormat = "dd/mm/yy hh:mm:ss"
just before changing the '.' to '/' using
Sheets("Priprava_dat").Range("A:B").Replace ".", "/"

Related

Pastespecial pasting only the first cell value across entire range

I'm trying to paste the formula results from one column to the next. I need to perform this through a macro without using a loop as it slows down the tool due to large number of records. The following methods are not working -
a) range("G5:G10").value = range("H5:H10").value
b) Selection.PasteSpecial paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
The issue is that excel copies the formula result of the first cell ("G5") to all the cells at the destination ("H5:H10"). The formula in the source is an array formula. Could anyone please share how to resolve the issue.
Edit :
Sub paste()
'
' paste Macro
'
'
Dim ColName As String, sheetname1 As String, lookup_col As String, lastrow_range As Long
ColName = "A"
sheetname1 = "Sheet1"
lookup_col = "C"
lastrow_range = 8
Worksheets("Sheet1").Range("F5").Select
ActiveCell.FormulaArray = _
"=IF(ISERROR(MATCH(TRUE,EXACT($" & ColName & ActiveCell.Row & "," & sheetname1 & "!$" & lookup_col & "$5:$" & lookup_col & "$" & lastrow_range & "),0)) , ""-" & ColName & """&"" is Invalid. This should match with any one of the values in column "" &""" & lookup_col & """&"" of "" & """ & sheetname1 & """&"". Please refer ""&""" & sheet_name & """ ,""It Matches"")"
Worksheets("Sheet1").Range("F5").Select
Selection.AutoFill Destination:=Range("F5:F8")
Range("F5:F8").Select
Selection.Copy
Range("G5").Select
Selection.PasteSpecial paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
Copy and paste special values of range
Range("H5:H10").Copy
Range("G5:G10").PasteSpecial (xlPasteValues)
Application.CutCopyMode = False
Copy and paste range
Range("H5:H10").Copy
Range("G5:G10").PasteSpecial (xlPasteAll)
Application.CutCopyMode = False
Or copy and paste the entire column
Range("H:H").Copy
Range("G:G").PasteSpecial (xlPasteAll)
Application.CutCopyMode = False
Thanks for the input.
I found that the calculation method had to be changed to 'Automatic' from 'Manual' :
Application.Calculation = xlAutomatic

EXCEL VBA Formula - Pad a cell

I am attempting to pad an XLS cell via VBA & XLS Formula but I am stumbling:
Example: I want to append " - Test" to the contents of Cell AC2 via a formula, then paste that formula to all the remaining rows in Col AC:
lastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
strTest = " - Test"
'append
Range("AC2:AC2").Formula = "=AC2.value & value(strTest)"
'then cut and paste
Range("AC2").Select
Selection.Copy
Range("AC2:A" & lastRow).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
I am guessing I need to adjust the syntax of the formula to append. I've tried several variants without success.
Any suggestions?
Note: the following formula works, but only in the first row. Copy & Paste simply copies the VALUE from X2 into subsequent rows, not the formula:
Range("X2:X2").Formula = Range("AC2:AC2").Value & strTest
Range("X2").Select
Selection.Copy
Range("X2:X" & lastRow).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
To append strTest to the cell values, you can use:
With Range("X2:X" & lastRow)
.Value2 = .Worksheet.Evaluate("Index(" & .Address & "&""" & strTest & """,)")
End With
I think instead of this:
Range("AC2:AC2").Formula = "=AC2.value & value(strTest)"
You want this:
Range("AC2:AC2").Formula = "=AC2.value & value(" & strTest & ")"

I want to copy column D:I,K,O,AG,AH,AA but it currently copies D:AH

Sub AutofillData()
Dim wkbkSource As Workbook
Dim strPath As String
Dim myRange As Range
Dim i As Integer
Dim c As Range
Dim wkbkTarget As Workbook
Application.ScreenUpdating = False
strPath = "\\temp\"
Set wkbkA = ThisWorkbook
Set wkbkB = Workbooks.Open(strPath & Range("E8").Value)
Set myRange = wkbkA.Sheets("Stand-up Request").Range("B13:B25")
offs = 0
For Each c In myRange
i = c.Value
wkbkB.Worksheets("Main Data").Range("D" & i & ":AH" & i).Copy
wkbkA.Sheets("Stand-up Request").Range("C13").Offset(offs, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
Application.CutCopyMode = False
offs = offs + 1
On Error Resume Next
Next
wkbkB.Close savechanges:=False
Application.ScreenUpdating = True
End Sub
If you don't mind the column order, you should only change one row:
wkbkB.Worksheets("Main Data").Range("D" & i & ":I" & i & ",K" & i & ",O" & i & ",AG" & i & ",AH" & i & ",AA" & i).Copy
Note that this way AA gets pasted before AG. If you mind the column order, you should split your range and copy/paste them separately:
wkbkB.Worksheets("Main Data").Range("D" & i & ":I" & i & ",K" & i & ",O" & i & ",AG" & i & ",AH" & i).Copy
wkbkA.Sheets("Stand-up Request").Range("C13").Offset(offs, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
wkbkB.Worksheets("Main Data").Range("AA" & i).Copy
wkbkA.Sheets("Stand-up Request").Range("C13").Offset(offs, 10).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
Note that this is a static code, if you change the columns to copy, you should also set the column value of the second paste's offset accordingly. If you want a dynamic code, you should create another range object that you will copy and you can get the dimensions of that range.
Also it might be better avoiding copy/paste altogether, using code like:
wkbkA.Sheets("Stand-up Request").Range("C13").Offset(offs, 0).Resize(1, 6).Value = wkbkB.Worksheets("Main Data").Range("D" & i & ":I" & i).Value
But it needs to be done for each sub-range, so it gets more complicated.
Also note that your code worked fine for me on ActiveSheet without On Error Resume Next, so you better check your code again :)

Saving In new File using macro in VBA

Im using the following code, but when I run it, it only saves the last part (& Day(Now) & "-" & Month(Now) & "-" & Year(Now)) of the name. Is not taking into consideration de string "Asesor". I don't know what part I'm missing:
Sub SaveFile()
Dim Asesor As String
Asesor = Range("B3").Value
Dim DestFile
DestFile = "C:\Users\ST\"
Application.ScreenUpdating = False
Workbooks("Grillas_QA.xlsm").Activate
ActiveSheet.Range("A1:K51").Select
Selection.Copy
Workbooks.Add
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWindow.DisplayGridlines = False
Application.DisplayAlerts = False
Here's where I save the file.
ActiveWorkbook.SaveAs Filename:=DestFile & Asesor & " " & Day(Now) & "-" & Month(Now) & "-" & Year(Now), _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
Thnx in advance for the help.
As follow up from comments, next line
Asesor = Range("B3").Value
should be changed to something like this:
Asesor = ThisWorkbook.Worksheets("Sheet1").Range("B3").Value
Also as a recommendation, I suggest you to read this post: How to avoid using Select/Active statements

Optimize VBA code and get rid of Workbook.activate and range.select?

I've got a macro which copies a code from one workbook to another one. My code is pretty long and I would like to shorten it if possible (also to be a bit more independent from the macro-recorder)
My code looks like this:
Workbooks("export.XLSX").Activate
Range("A2:A" & Range("A" & Rows.Count).End(xlUp).row).Copy
Workbooks("ORDERS.CSV").Activate
Range("X3").Select
Selection.End(xlDown).Offset(1, -18).Select
ActiveSheet.Paste
Workbooks("export.XLSX").Activate
This block repeats itself for different columns. Is there a way to shorten this?
More examples:
Range("C2:C" & Range("A" & Rows.Count).End(xlUp).row).Copy
Workbooks("ORDERS.CSV").Activate
Range("X3").Select Selection.End(xlDown).Offset(1, -14).Select
ActiveSheet.Paste
Workbooks("export.XLSX").Activate
Range("G2:G" & Range("A" & Rows.Count).End(xlUp).row).Copy
Workbooks("ORDERS.CSV").Activate
Range("X3").Select Selection.End(xlDown).Offset(1, -13).Select
ActiveSheet.Paste
Workbooks("export.XLSX").Activate
Firt of all add Application.ScreenUpdating = False
Workbooks("export.XLSX").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Copy
Workbooks("ORDERS.CSV").Range("X3").End(xlDown).Offset(1, -18).Select.PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
and follow the same pattern, then
Application.Screenupdating = True

Resources