I have multiple .txt files in the form of
Name1|number1
Name2|number2
Name3|number3
(...)
And I would need to transpose them to look like:
Name1|Name2|Name3|(...)
Number1|Number2|Number3|(...)
This is easy enough to do for a single file in excel, but I would need a way to do this in multiple files in a folder. Since Notepad++ allows finding and replacing for multiple files, I would like to know if there is a way to achieve this somewhat automatically.
While all files have the same pattern (two columns), not all of them have the same number of rows.
I have tried the following aproaches using Notepad++:
Replacing | for a carriage return to have everything in a single column, in hopes to somehow group all numbers, move them to the bottom and reorganize.
Repeat the whole text below using a symbol to separate both copies (find [\s\S]*.*, replace with \0\n%\n\0), in an attempt to delete all numbers above and all names| below to finally reorganize. Also got stuck in the middle.
As you can see, my Regex knowledge is extremely limted, even using Regex101. I don't know if this is easy or hard to achieve, I simply cannot find the solution by myself after several hours.
What can I do?
Thanks for your patience in advance.
Thanks for the answer. I thought about macros, but all my searches ended up mentioning Power Query. It is a business oriented add-in, and I would rarely need it, so I was't willing unless there was no choice (and I also happen to like Notepad++ quite a bit, that might have blinded me).
However, I was googling the wrong question; as soon as I rephrased my search, I found everything I needed. It is extremely simple to achieve this using excel VBA.
Source:
Apply a macro to all files in prompted folder
I basically recorded a macro to transpose the 2 columns into rows and deleted the original columns, so that only the transposed rows would show. Then, I copied that macro and pasted it inside another one (see source) which loops through all files in a folder (and added another piece of code to automatically save and close the files).
With this new macro, there is no need to open any file; simply open excel, insert the macro and run it. Voilà.
Final macro:
Sub LoopThroughFiles()
Dim xFd As FileDialog
Dim xFdItem As Variant
Dim xFileName As String
Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
If xFd.Show = -1 Then
xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
xFileName = Dir(xFdItem & "*.txt*")
Do While xFileName <> ""
With Workbooks.Open(xFdItem & xFileName)
'Start of transpose code
Range("A1").CurrentRegion.Select
Selection.Copy
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Columns("A:B").Delete
Range("A1").Select
Application.CutCopyMode = False
'End of transpose code
'Save and close
ActiveWorkbook.Close SaveChanges:=True
'End of save and close
End With
xFileName = Dir
Loop
End If
End Sub
It was so simple I feel stupid now, sorry to all who had to read the post and rolled their eyes.
Related
Currently I have macros set up in my excel that pastes a list when clicked.
However I am encountering an issue where I have to paste the copied list (from a pdf) into notepad before pasting into excel, so that it separates into cells instead of trying to excel cram the entire list into one cell when done directly.
I have tried creating a macro that would open a cell directly paste into it then cut out before pasting (Which works when done manually) as well as a number of different methods that were all dead ends.
My procedure is currently:
Open PDF, ctrl a, ctrl c
paste into notepad then ctrl a, cut
paste into excel
If I could get help removing the notepad part of the procedure, I would be incredibly happy!
If you paste the whole thing inside a cell like this:
Then you can use this script to do a text to rows operation.
Option Explicit
Sub TextToRows()
Dim RG As Range
Dim RGValue As String
Dim LinesArray
Dim LineCount As Long
Dim I As Long
Set RG = Selection
If RG.Cells.Count <> 1 Then Exit Sub
RGValue = RG.Value
LineCount = Len(RGValue) - Len(Replace(Replace(RGValue, Chr(13), ""), Chr(10), "")) + 1
If InStr(1, RGValue, Chr(10)) = 0 Then
LinesArray = Split(RGValue, Chr(13))
Else
LinesArray = Split(RGValue, Chr(10))
End If
RG.Offset(1, 0).Resize(LineCount, 1).Value = Application.Transpose(LinesArray)
End Sub
Viola!
Your aim is reduced notepad step however I suggest I would remove the pdf step since poppler or xpdf pdftotext -layout is usually good to add the needed white space to keep text tabular. That can be drag and drop pdf on a shortcut that calls open new spreadsheet with text. And here is the usual core issue with cut and paste plain text as a framework.
Most spreadsheets as far back as last century have several text import methods, most common is add commas for csv import, but space separated text is accepted too. (I still use MSeXcel 97 portable as its an old familiar) It often requires some intervention to check detection, so here in Modern Open Office I specified combine spaces. Thus, introduces just one minor error here, "Addresss" is moved left-wards.
No Problem it has a spelling mistake so it's just that one that needs managing twice. 1st spell check and then move it right.
Every case can be different but if you write a macro to cover those corrections you repeat then it pays to run for the next import to include all the steps.
The simplest is plan ahead by tidy the text (here used tabs to replace spaces), then drag and drop, the results are usually cleaner data in = cleaner the cells are aligned.
Thank you everyone for your advice, I finally found a solution. It was as simple as a paste special text.
ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:= _
Hopefully this helps someone else! Found it whilst looking into ways the window operating system formats copy/paste. It can be manually done by right clicking > Paste Special > Text
I would like to copy the text from a excel file in a specify range to another excel file in the same position
Here is the code that I tried
Sub OneCell()
ChDir "C:\Workfile"
Windows("simple av & cv template(Level).xls").Activate
Sheets("Table ENG SG").Select
Range("C9:C44").Select
Selection.Copy
Windows("Finalize").Activate
Sheets("sheet1").Select
ActiveSheet.Paste
End Sub
Do I need to define sth at the beginning of my program first or did I make any mistakes?
Assuming both your workbooks are open in the same instance of Excel which you can confirm by going to the View tab > Switch Windows and seeing if both workbook names are listed:
Sub ModifiedOneCell()
Workbooks("simple av & cv template(Level).xls").Sheets("Table ENG SG") _
.Range("C9:C44").Copy _
Destination:=Workbooks("Finalize").Sheets("sheet1").Range("C9:C44")
End Sub
The help for this can be found by opening the Object Browser in VBA (press F2), scrolling down to the Range object, clicking Copy from the list in the right pane then press F1.
The [space][underscore at the end of lines is the line continuation key combination in VBA allowing you to split long lines of code for readability.
If your workbooks are open in separate instances of Excel (i.e. only one is visible in Switch Windows), then copying to the clipboard and selecting Windows as you did in your code is the correct approach.
You would need to add
Range("C9:C44").PasteSpecial xlPasteAll
in place of
ActiveSheet.Paste
to get the result into the desired range
Ranges don't have a Paste method - only PasteSpecial.
I have been working on this project for a long time and am suddenly getting a new error whenever I close my Excel file. I get the error twice "The picture is too large and will be truncated." There is no picture in my file. I am pasting formats.
This seems to be one of the Excel "Unsolved Mysteries".
I am using MS Office Professional Plus 2010 on Windows 7.
I have researched this and tried the following:
Deleted all %temp% files
Ran CCleaner
Set CutCopyMode =
False after all paste special (formats)
Went to add/remove
programs and reconfigured Office to stop the Clip Organizer from
running. (Control Panel\Programs\Programs and Features -> MS Office
Professional Plus 2010 -> Change -> Add or Remove Features -> Office
Shared Features -> Clip Organizer -> Not Available, etc.)
Rebooted
None of that helped, so I narrowed down the source of the problem by commenting out function and subroutine calls, running the program, saving and then pressing "x" to close. I did this until I found the right sub. Then I commented out all the lines of the sub and added them back in one logical chunk at a time until I found the problem area. Here it is:
' *********** APPLY BASIC ROW FORMATTING FROM TEMPLATE ***********
' Copy basic row formatting from template and paste over all rows
wksTemplate.Rows(giHEADER_ROW + 1).Copy
myWS.Rows(lFirstRow & ":" & lLastRow).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
The paste contains formatting only - colors, borders, number formats, wrapping etc. It probably pastes on a range of 200 rows on average.
I have not changed these 3 lines of code in months. Why now?
Has anyone solved this mystery?
Thanks,
Shari
I got this error after copying a range and then using a set of pastespecial calls:
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteValuesAndNumberFormats
.PasteSpecial xlPasteFormats
solution was to copy an empty cell and pastespecial xlvalues back into itself:
' to avoid the message on closing the book - "picture is too large and will be truncated", copy and paste a singe empty cell
ThisWorkbook.Worksheets(1).Cells(1, 1).Copy
ThisWorkbook.Worksheets(1).Cells(1, 1).PasteSpecial xlValues
' clear clipboard
Application.CutCopyMode = False
I sometimes have the same problem as you, but i have many pictures in my files...
Also sometimes i have slow down (open/close or just standard calculations(menu popup...)).
Usually when i close the workbook, and reopen it, it works fine again.
I have maybe some answer, not sure if any help :
Do you use Global variables ?
for example, in module 1:
Option Explicit
Public BigObject as AnyBigSizeType
Sub xxx() 'code following
try to avoid using global variables, usually its a mess and not even usefull.
Also, just to be safe, try in the immediate window : activesheet.pictures.delete
(or even activesheet.shapes.delete , but this one also deletes comments and other stuff...)
I get the error when closing a file after a macro used the Range.Copy command, even if the clipboard is empty. Doing Application.CutCopyMode = False is not enough, but adding this at the end of macros that use the Range.Copy method seems to be the solution:
This is my solution:
'without this it will say "The image will be truncated because it's too large", because Excel is stupid
[A1].Copy
Application.CutCopyMode = False
I am almost completely new to using VBA and Macros on Excel 2010. I know little to nothing about macro coding, and I just started a day ago trying to pick it up.
I was asked to create a macro that copies ONLY the data from 12 workbooks and pastes it onto a blank workbook (they all have 1 sheet each, with the data on each workbook starting on cell A3 while stretching to column S (the amount of data on the sheets vary)).
NOTE: When I mean "ONLY the data," the cells that I want to be copied include the blank cells that are in between the first and last parts of the data.
1) When copying data from one workbook to another, do you HAVE to specify the cells that you would like to copy? Or is there a way to specify where the data ends on the sheet, and then copy all of that data? If so, then could someone show me how to do so? The reason for doing so is because the macro will be used weekly.
and 2) Could someone simply help me develop this macro? Help would be massively appreciated :)
Some Extra Notes:
- The Workbooks are named "Status by offering ID [1-12]
Thanks again!
Ripster has given one example of missing information from your specification.
Santosh recommends a pretty neat tool but my reading of your question is that your knowledge is not up to using a tool yet.
I deduce this is a work question and your boss has asked you to write this macro despite your knowledge of VBA being zero. This seems to be a growing problem: you can discover the answer to anything on the net so why bother with training people. Even if you produced a complete specification of your requirement, I doubt anyone will give you a complete solution. If they do provide a complete solution, it will not help with the next requirement. So I am going to break your problem down into the type of questions you can ask the net.
Each week you get 12 source workbooks and create a summary by consolidating data from the source workbooks. Do you overwrite the previous week's summary with the new summary or do you want to save all the summaries? It will not add much to the complexity to keep all the summaries but you need to decide what you want.
If you discard the previous summary, the macro can be in Summary.xlxm, say. If you save the summaries, the macro will need to be in its own workbook, Macro.xlsm say, and it will create a different summary workbook, SummaryYYWW.xlsm say, each week.
If you search for "Workbook Open" and "Workbook Create", you will find instructions on how a macro in one workbook can open other workbooks and create new workbooks.
Where are the source workbooks? Are they in the folder CurrentData which is overwritten each week? Are they in the folders Week1301, Week1302, Week1303 and so on? Are they in the folder NewData and the macro is to move them to folders Week1301, Week1302, Week1303 and so on after processing? All these are options but I suggest you start by moving the source and summary workbooks to/from a folder convenient for the macro.
Look up "ThisWorkbook" and "ActiveWorkbook". In brief: "ThisWorkbook" is the workbook containing the macro and "ActiveWorkbook" is the most recently opened workbook.
Look up workbook property "Path". ThisWorkbook.Path, for example, gives you the name of the folder containing ThisWorkbook. Look up workbook property "Name".
If the source workbooks always have the same names, you could hard code the names into the macro. I do not recommend this. Look up the function "Dir".
I hope the above has given you a start on breaking your total problem down into its components.
There are many different VBA tutorials available on the web. Try a few and pick one you like.
"Debug.Print xxx" outputs the value of xxx to the Immediate Window. This can be very helpful as you start. Try this as your first macro:
Option Explicit
Sub First()
Dim FilenameCrnt As String
Dim WbookCrnt As Workbook
Debug.Print ThisWorkbook.Name
Debug.Print ThisWorkbook.Path
FilenameCrnt = Dir$(ThisWorkbook.Path & "\*.*")
Do While FilenameCrnt <> ""
Debug.Print FilenameCrnt
If FilenameCrnt = ThisWorkbook.Name Then
With ThisWorkbook
Debug.Print " Used range: " & .Worksheets(1).UsedRange.Address
End With
Else
If LCase(Right(FilenameCrnt, 3)) = "xls" Or _
LCase(Right(FilenameCrnt, 4)) = "xlsm" Or _
LCase(Right(FilenameCrnt, 4)) = "xlsx" Then
WbookCrnt = Workbooks.Open(ThisWorkbook.Path & "\" & FilenameCrnt)
With WbookCrnt
Debug.Print " Used range: " & .Worksheets(1).UsedRange.Address
End With
WbookCrnt.Close
WbookCrnt = Nothing ' Free resource
End If
End If
FilenameCrnt = Dir$
Loop
End Sub
Best of luck
I have two different workbooks with approx 15 columns and 50k rows in one workbook and 10columns and 1000 rows in another workbook and only 2 columns(partnumber, changelevel)are in common. So I want to pull two reports from these two workbooks.
Records with common partnumber & changelevel in to a different workbook as one report.
I want to delete the common part number & changelevel records from first workbook and copy all the remaining records into a different workbook as another report.
Angiee . . .
You have a couple of questions that will need to answered before anyone can help you with this.
Is this a one time deal where you are trying to clean up data and come up with a new starting point and you won't need to run this process over and over again?
Can we assume that the data rows are not in the same Worksheet row in both Workbooks?
If the answer to both questions is YES then I would have to say that Excel is decidedly NOT the Office Application that you should be using. I would suggest that you import both Workbooks into an Access Database as separate tables. That way you can use SQL to perform the matches and lookups with little or no code needed. You can easily export the query results back to an Excel Workbook once you have the results you want. You could probably have your answer in an hour. If you go with this option you can also link the Worksheets into the Access DB and avoid importing them. It won't be as fast but it will work.
Otherwise, if you are stuck with Excel then you either have a significant amount of code to write that pretty much consists of looping through all of the records in one Workbook, looking up the values in the other Workbook then generating the output in still more Workbooks. . . or . . . you could try copying the Worksheet with the 1000 records into the other Workbook and then using the Worksheet Functions VLOOKUP and/or HLOOKUP to create a lot of formulas. (I can't in good conscience endorse this second approach but if you are not very experienced at VBA then it may be the better approach for you).
Either way you go with the Excel solution there'll be a lot of work invloved.
If you have any specific coding issues then you are in the right spot. But you will need to pick an approach first.
Good luck!
Doug
The question is an easy one to answer, the problem comes into two fold.
How fast is your computer?
How often do you need to run this code?
The reason I ask these questions are because to run any code on 50,000 lines no matter how small the code to actually make this work is... you need to have a computer that is rather robust, otherwise this code is going to stall your computer, or at least excel for a good minute to three minutes+ depending on how fast and how much memory you actually have.
Without seeing your workbook you need some very simple formulas, but what you are going to have to do is add another line into the workbook. In Column P, you need a verification formula. This formula is simple, but it will depend on how many points of reference you require.
=COUNTIFS('Sheet2'!$A:$A,$A3,'Sheet2'!$E:$E,$E3)
From there you can see what are duplicates or not. You can then have in column Q a formula like this:
=IF($P3,"SAME","")
And it will tell you if the data is the same or not. Basically it says if there is anything but 0 in the cell P3 it will say there is something the same, otherwise it's not.
From there you need a code sort of like this:
Sub Update_TNOOR()
Dim wsS1 As Worksheet
Dim wsS2 As Worksheet
Dim lastrow As Long, fstcell As Long
Set wsS1 = Sheets("Sheet1")
Set wsS2 = Sheets("Sheet2")
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
With wsS1
wsS1.Columns("P:Q").ClearContents
ThisWorkbook.Sheets("Sheet1").Cells(1, 16).Value = “=COUNTIFS('Sheet2'!$A:$A,$A3,'Sheet2'!$E:$E,$E3)"
ThisWorkbook.Sheets("Sheet1").Cells(1, 17).Value = “=IF($P3,"Same",””””)"
wsS2.Columns("P:Q").ClearContents
ThisWorkbook.Sheets("Sheet2").Cells(1, 16).Value = “=COUNTIFS('Sheet1'!$A:$A,$A3,'Sheet1'!$E:$E,$E3)"
ThisWorkbook.Sheets("Sheet2").Cells(1, 17).Value = “=IF($P3,"Same",”Different”)"
End With
With Intersect(wsS1, wsS1.Columns("Q"))
.AutoFilter 1, "<>Same"
With Intersect(.Offset(2).EntireRow, .Parent.Range("B:Q"))
.EntireRow.Delete
End With
.AutoFilter
End With
'Blow away rows that are useless
lastrow = wsS2.Range("A2").End(xlDown).Row
wsS2.Range("P1:Q1").Copy wsS2.Range("P2:Q" & lastrow)
With Intersect(wsS2.UsedRange, wsS2.Columns("Q"))
wsS2.Range("P:Q").Calculate
.AutoFilter 1, "<>Different"
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
With wsS2
lastrow = wsS2.Range("A1").End(xlDown).Row
Intersect(.UsedRange, .Range("A1:N" & lastrow)).Copy wsS1.Cells(Rows.Count, "B").End(xlUp).Offset(1)
End With
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
End Sub
this should get you on your way... if I read what you are attempting to do correctly.
As people have said though, what you want done can be done in excel, should it, I don't know... people here seem to think not, but if you need to use excel, this should get you on your way.
Again, I don't know what your workbook looks like, so I hope this helps. This compares data and merges it into the first sheet. IT won't do everything you want to do... but this should get you on your way.