Macro to paste value on row's based on column date - macros

I have in sheet1 a cell A1 where I put in a date (DD/MM/YYYY) in cell B2 is a number the info in cell A1 and B1 change but macro will always copy from B1. On sheet2 on column A are the dates(DD/MM/YYYY) for the whole year one under the other, I want a macro that based on the date in cell A1 on sheet1 looks on sheet2 on column A and copy/pastes the info from B2 on column B from sheet2 on same row with the according date in column A.
I looked around for similar things but couldn't find anything to work any help is appreciated.
Thank you in advance.
So far I used a pretty simple copy paste macro but using this means I have to write it over and over again for each day of the year.
Where C1 = A1 in Sheet2 witch is first date
Sub test()
**Sheets("Sheet1").Select
If Range("A1") = Range("C1") Then
If Range("B1").Value > "0" Then
Range("B1").Copy
Sheets("Sheet2").Select
Range("B1").PasteSpecial xlPasteValues
else
end if
else
end if
end sub
So doing it like this I would have to write it again and again for C2..C3.. and put paste to B2.. B3 etc so im looking for a solution to write it just once to look in whole column and paste on the according row.

Related

How do I change the date format from MM/DD/YYYY to DD/MM/YYYY in libreoffice calc?

I have a spreadsheet with existing date values that I'd like to convert from MM/DD/YYYY to DD/MM/YYYY format. Is there any way of doing this?
The spreadsheet comes from a csv file from someone else that i have opened in calc.
Select the cells with the dates and in Cell -> Format, choose the date format you wish (DD/MM/YYYY)
Making sure the cells are selected, go to Data -> Text to columns choose "Separated by" and "Tab" and click "OK"
Cells should now have the desired format
Cut (or copy then erase) all values from the column (CTRL + X).
Choose Edit -> Paste Special -> Unformatted text (MAJ + CTRL + ALT + V).
On "Fields", click on your column, then select "Date (MDY)". It is important here to select the format the data currently is, NOT the format you want it to be.
If date is still not OK, you will need to follow this additional step (response above from #cybernetic.nomad):
Select the cells with the dates and in Cell -> Format, choose the date format you wish (DD/MM/YYYY).
NB: Note that my LibreOffice Calc is not in english so I am not 100% sure about the option names.
You select the cells to format, then you press Ctrl-1 (or choose "Format" - "Cells..." from the menu, or any other way) to open the dialog to format cells. There is a text field "Format code" where you enter "DD/MM/YYYY" without the quotation marks. Click "OK".
If that does not format the date like you want, the value in the cell is not interpretable as a date. Investigate the cell's value, you might have a text marker (a single apostrophe ') as the first character.
To check this out and experiment with the formats, choose "Insert" - "Date" from the menu. It puts the current date into the current cell.
Disclaimer: Since my Calc is not set to English, the names of menus and their entries might be different. I just guessed how they could be named.
EDIT:
If you can't change the cell format because the date is constantly recognized as "text", you might like to try this:
Insert a column right from the date column. This produces in your case the column O. All columns right of it are pushed to the right.
Put this formula in cell O2 on the right of the first date text:
=CONCATENATE(MID(N2;4;3);MID(N2;1;3);MID(N2;7;4))
It takes snippets (MID()) from cell N2 and concatenates them in the new order. Details of the functions are in Calc's documentation.
You will need to adjust N2 to the correct cell reference if it is not the cell directly left of the cell you're entering the formula.
Put the cell cursor on N2.
Press these key combinations in sequence to mark a deep enough range of cells in column O: Control-Down, Right, Shift-Control-Up. The cell cursor will be on cell O2 now, and the range below and including it is marked.
Now press Control-D to copy the formula in O2 into each cell in the range.
Now you will have a new column O with the text of N, but exchanged characters. The values are still "text", though.
If you like, you can shrink the width of column N now, or hide it.
Example:
Input wanted: DD/MM/YYYY (31/12/2021)
Input accepted: MM/DD/YYYY (12/31/2021)
First add new Date type
Go to Format --> Cells
In "Category" select "Date"
In "Format code" write the input wanted. In this case DD/MM/YYYY
Press the green check
Second format style
Go in the cell where you want to add the date
Write the date in the wrong format. Example: 12/31/2021 (for 31st December 2021) even if you want 21/11/2021
Select that cell and press CTRL-X. The text should disappear
Select that now empty cell and do: Edit -> Paste Special -> Pase unformatted text
The date should be there in the wrong format
Select that cell with the wrong date and do: Format --> Cells
In "category" select "date" and double click on the newly added date type (aka DD/MM/YYYY)

LibreOffice calc Hyperlink from macro doesn't work

I have trouble making hyperlink within macro.
For example just create empty spreadsheet and create another sheet within so we have 2 empty sheets.
In Sheet1 A:1 just paste =HYPERLINK("#Sheet2")
For A:3 create macro and execute it
Sub TestMacro
dim document as object
oSheet = thiscomponent.sheets.getByName("Sheet1")
oCell = oSheet.getCellRangeByName("$A$3")
xx="=HYPERLINK(""#Sheet2"")"
oCell.setString(xx)
End Sub
So sheet1 now have something on cell A1 and A3. the both should be link but just A1 works.
If jou press CTRL+click on Sheet1 A1 the Sheet2 is selected.
If you press CTRL+click on Sheet1 A3 nothing happens
I tested ou xubuntu 18.04LTS
LibreOffice Version: 6.0.7.3
Build ID: 1:6.0.7-0ubuntu0.18.04.10
For functions and calculations, set the cell formula instead of a literal string value.
oCell.setFormula(xx)

LibreOffice Calc - how to drag-fill a series from multiple rows that all reference the same row

I have cells in a few rows that all reference one row on another sheet:
=Sheet2.A1
=Sheet2.B1
=Sheet2.C1
When I select these cells and drag the fill handle to fill the cells below, Calc fills them like this:
=Sheet2.A4
=Sheet2.B4
=Sheet2.C4
=Sheet2.A7
=Sheet2.B7
=Sheet2.C7
etc.
I guess it's doing this because each new row is three cells below the original row.
This is not the behaviour I want - I want to fill them in a series, like this:
=Sheet2.A2
=Sheet2.B2
=Sheet2.C2
=Sheet2.A3
=Sheet2.B3
=Sheet2.C3
=Sheet2.A4
=Sheet2.B4
=Sheet2.C4
etc.
Any ideas how to do this?
I'd like to use either a formula or a manual way of doing this - I'm not familiar with macros so would rather avoid if possible, please.
Manual way (I like that suggestion, I never even thought about using Calc to automatize the process of filling in spreadsheet formulas before, but it's doable):
Open a new Calc file
Format cell A1 as text
type =Sheet2. into A1 (A1 should now display =Sheet2. literally)
type A B C into B1, B2 and B3 respectively
select B1 to B3 and drag down (it should repeat series A,B,C,A,B,C...)
type =FLOOR((ROW()-1)/3)+2 into C1 (ROW() starts counting from 1 hence the -1)
drag that down (it should give you the series 2,2,2,3,3,3,4,4,4...)
type =A$1&B1&C1 into D1 (D1 should now display =Sheet2.A2 literally)
drag D1 down and it should give you your desired series
NOW: copy that
paste it into any notepad or similar plain text editor
copy that
paste it into the Calc spreadsheet you wanted to have this series of references in first place
it should open a text import dialogue - just make sure everything is pasted into one column and hit OK
VoilĂ 
pros: when your colleagues see you do that you will be seen as LibreOffice god from now on
cons: not very dynamic (you'll have to repeat the whole process if something changes)
Formula way:
Use OFFSET.
E.g: =OFFSET(Sheet2.$A$2;FLOOR((ROW()-1)/3);MOD(ROW()-1;3))
pros: easier to modify
cons: if the results are not what you're expecting, it's harder to debug

Put excel all cells under each other in one column

Is there a possibility or formula, to extract all cell data from different columns then putting them under each other just on one column, each cell data just in one cell, therewith I can sort them e.g. from A to Z in one column.
Thanks in advance
Solution 1
Using helper column.
In Cell E2 enter the following formula
=INDEX($A$2:$C$15,MOD(ROW()-ROW($G$2),ROWS($A$2:$A$15))+1,TRUNC((ROW()-ROW($G$2))/ROWS($A$2:$A$15))+1)
Drag/Copy down as required.
Then in Cell F2 enter
=IFERROR(INDEX($E$1:$E$45,SMALL(IF($E$1:$E$45<>0,ROW($E$1:$E$45)),ROW(F1)+1)),"")
This is an array formula so commit by pressing Ctrl+Shift+Enter. Drag/Copy down as required. Change range as required.
See image for reference.
Solution 2
Using ugly looking long formula.
Enter the following formula in Cell D2
=IFERROR(INDEX($A$2:$A$15, SMALL(IF(ISBLANK($A$2:$A$15), "", ROW($A$2:$A$15)-MIN(ROW($A$2:$A$15))+1), ROW(A1))), IFERROR(INDEX($B$2:$B$15, SMALL(IF(ISBLANK($B$2:$B$15), "", ROW($B$2:$B$15)-MIN(ROW($B$2:$B$15))+1), ROW(A1)-SUMPRODUCT(--NOT((ISBLANK($A$2:$A$15)))))), IFERROR(INDEX($C$2:$C$15, SMALL(IF(ISBLANK($C$2:$C$15), "", ROW($C$2:$C$15)-MIN(ROW($C$2:$C$15))+1), ROW(A1)-SUMPRODUCT(--NOT((ISBLANK($A$2:$B$15)))))), "")))
Drag/Copy down as required. Change range as per you data.
Note : This formula will work only for three or less columns.
An easier way without using any formula. Just go to Find & Select, go to special, select blanks. All the blanks are now selected go to

Excel assume dates is in US format

I created a macro that copy a column where 02/04/2013 12:35 means 2nd of April
I then paste it and use text to columns to get rid of the time part. Excel for some reason than interpret it as 4th of February. Tried both selecting text at the last step of the text to columns and date but in both cases it get flipped.
Dates that cannot be flipped like 28/03/2013 remains correct.
If I don't use a macro but manually do it Excel doesn't flip the day and month only when running it as a macro that occurs. Noticed it occurs in another macro as well when just copy a cell with a date to another cell with a date.
You could try:
Sub Convert_Dates()
Dim Rng As Range
Set Rng = Selection
For Each c In Rng
c.Value = DateSerial(Right(Split(c)(0), 4), Mid(Split(c)(0), 4, 2), Left(Split(c)(0), 2))
Next c
End Sub
It can also help with "text dates" to use Format and "dd/mmm/yyyy" as this doesn't tend to get mixed up.