How to output .csv rows to a series of seperate text files?
I saw this link, and the approach should be similar..
Outputting Excel rows to a series of text files
Can anyone help me know the steps and if i should use excel and suggest the steps, to achieve the above results as in the link or otherwise. Thanks in advance...
Assuming you are actually using Excel now...
First, like I said in one of my comments, you should start here to learn about using Visual Basic for Applications (VBA) on your own.
In Excel, it will be easiest to activate the Developer tab to access the the Visual Basic Editor.
Go to File>Options>Customize the Ribbon.
Check the Developer tab. Click Ok.
Go to Developer and under the Code section is Visual Basic. Click this to open the VBA editor.
Now you can make VBA subs. You can put them in specific sheets, the workbook as a whole, or modules in your PERSONAL.XLSB which allows any Excel workbook with macros enabled to run them.
You may need to create a module. If so:
Right-click on VBAProject (PERSONAL.XLSB).
Insert>Module
Name it.
You can now paste code into the module and you have a "macro".
The easiest way to run this is to
Go to the Developer tab.
Hit Macros.
Select your macro.
Hit Run.
You can also assign a keyboard shortcut by hitting Options... in that same Macros menu.
Now you can essentially copy and paste the code from here. With a slight modification:
Sub Export_Files()
Dim sExportFolder, sFN
Dim rTitle As Range
Dim rContent As Range
Dim oSh As Worksheet
Dim oFS As Object
Dim oTxt As Object
'sExportFolder = path to the folder you want to export to
'oSh = The sheet where your data is stored
sExportFolder = "C:\Disclaimers"
Set oSh = Sheet1
Set oFS = CreateObject("Scripting.Filesystemobject")
For Each rTitle In oSh.UsedRange.Columns("A").Cells
Set rContent = rTitle.Offset(, 1) & ", " & rTitle.Offset(, 2) '<--This will put in your Column B and C value. You can delimit with whatever you desire; I used a comma and space.
'Add .txt to the article name as a file name
sFN = rTitle.Value & ".txt"
Set oTxt = oFS.OpenTextFile(sExportFolder & "\" & sFN, 2, True)
oTxt.Write rContent.Value
oTxt.Close
Next
End Sub
Related
I am trying to link a powerpoint presentation to data in excel. However the excel and powerpoint will change locations every day.
Here is my methodology so far:
- Copy from excel
- Paste special... paste link... As: Microsoft Excel Worksheet (code) object
This allows me to live edit the excel document and the powerpoint updates in real time.
However, when I change the location of the excel document, all 100+ links are broken.
How do I dynamically set the path of the links so they always follow the correct excel document, no matter where the excel document is? The excel document will never name change, neither will the powerpoint.
I have been looking for a solution for over 6 months...
Thank you.
I've got a page on my PPTFAQ site that includes code that might help:
Batch Search and Replace for Hyperlinks, OLE links, movie links and sound links
http://www.pptfaq.com/FAQ00773_Batch_Search_and_Replace_for_Hyperlinks-_OLE_links-_movie_links_and_sound_links.htm
Here's the specific code ... it deals with both hyperlinks and OLE links (ie, your Excel links). You can remove the hyperlink stuff if you don't need it and pass the new path as a parameter rather than getting it from an InputBox:
Sub HyperLinkSearchReplace()
Dim oSl As Slide
Dim oHl As Hyperlink
Dim sSearchFor As String
Dim sReplaceWith As String
Dim oSh As Shape
sSearchFor = InputBox("What text should I search for?", "Search for ...")
If sSearchFor = "" Then
Exit Sub
End If
sReplaceWith = InputBox("What text should I replace" & vbCrLf _
& sSearchFor & vbCrLf _
& "with?", "Replace with ...")
If sReplaceWith = "" Then
Exit Sub
End If
On Error Resume Next
For Each oSl In ActivePresentation.Slides
For Each oHl In oSl.Hyperlinks
oHl.Address = Replace(oHl.Address, sSearchFor, sReplaceWith)
oHl.SubAddress = Replace(oHl.SubAddress, sSearchFor, sReplaceWith)
Next ' hyperlink
' and thanks to several astute user suggestions, let's fix OLE links
' and movie/sound linkes too
For Each oSh In oSl.Shapes
If oSh.Type = msoLinkedOLEObject _
Or oSh.Type = msoMedia Then
oSh.LinkFormat.SourceFullName = _
Replace(oSh.LinkFormat.SourceFullName, _
sSearchFor, sReplaceWith)
End If
Next
Next ' slide
End Sub
You can link data from a saved Excel spreadsheet or copy cells from an Excel spreadsheet into your Microsoft PowerPoint 2010 presentation.
https://support.office.com/en-us/article/import-data-from-excel-into-powerpoint-3ec295e9-1bfd-47ff-8d7d-8b838caef853
Use the "Name manager" function in "Formula" and define the name of the table/certain cells you want to paste on PowerPoint.
So that when you paste "Excel Object" on PPT, the link will not simply be the location of the table in a worksheet (something like R1C1), but the name you defined and will not be affected if you move the table around.
We have 2 different files with basic vlookup infos:
Source file is: JP2-CSV CRAWLER
Destination file is: JP2-CATEGORIES
We are trying to copy one full column from the Source file to the first column of the destination file automatically ( It should work when opening it or using it)
That's our code:
Sub Copysubcat()
Dim wbSource As Workbook
Dim wbDestination As Workbook
Set wbSource = Workbooks.Open( _
Filename:="C:\Users\user\Desktop\crawler file\JP2-CSV CRAWLER.xlsx")
Set wbDestination = Workbooks("C:\Users\user\Desktop\crawler file\JP2-CATEGORIES.xlsx")
wbSource.Sheets("CSV Crawler").Range("P2:P10000").Copy
wbDestination.Sheets("Cats & Subcats").Range("A2:A10000").PasteSpecial (xlPasteValues)
Application.CutCopyMode = False
ActiveWorkbook.Save
End Sub
We are having an error "Subscript out of range"
Anybody could help on that?
Workbooks isn't a method, so it cannot take any parameters. This is class, which represents workbooks, which contains methods to handle them. One of them is Open which you will need in this case. So you'll need to switch this:
Set wbDestination = Workbooks("C:\Users\user\Desktop\crawler file\JP2-CATEGORIES.xlsx")
to this:
Set wbDestination = Workbooks.Open("C:\Users\user\Desktop\crawler file\JP2-CATEGORIES.xlsx")
Also, there is an event such as opening the workbook, you can place your corrected code there, so the macro would run every time this event is raised (i.e. on every opening). If you are using standard VBA development environment in Excel, click on This_workbook on right side (in tree view), you will have to dropdown lists at the top, in the one on the left choose Workbook, in the other one select Open (this is list of events, that is raised by Workbook). Then, inside generated method place youre code :)
I was trying to copy a text file and paste the data in excel using the following code. The code works satisfactorily to the extent copying and pasting data in the destination excel sheet, but additionally, opens up another excel file and sheet with the same name as the Text file, without being prompted anywhere in the code, and pastes the data there as well. This is not desirable. I don't want to split the data to columns or perform any other actions. It is a plain and simple copy and paste task. I have searched this and various other websites for an answer but failed to get one that appropriately addresses my problem. I am unable to figure out the flaw in the code, and therefore, seek your help. Any help would be most thankfully acknowledged.
Here is my code:
Sub CopyTextFile()
Set TxtFileName = Workbooks.Open("D:\Spares\Inventory\list_of_spares.txt")
TxtFileName.Sheets(1).Range("A1").CurrentRegion.Copy
Workbooks("Macro Test.xlsm").Activate
ActiveWorkbook.Sheets(1).Range("A1").Select
ActiveSheet.Paste
End Sub
You're getting the "extra file" because you are opening the text file in Excel (with the Workbooks.Open statement) and then copying the data from it.
Instead, open the file with a filesystemobject and read the data, then write it directly into your workbook:
Sub CopyTextFile()
Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oFile : Set oFile = oFso.OpenTextFile("D:\Spares\Inventory\list_of_spares.txt", 1)
Dim sText
sText = oFile.ReadAll
oFile.Close
ThisWorkbook.Sheets(1).Range("A1").Value = sText
End Sub
See how that works for you?
I found a way to make your code work, you had to close the second workbook that was open, your code helped me thats why i am pointing out what was missing.
Sub CopyTextFile()
Set TxtFileName = Workbooks.Open("D:\Spares\Inventory\list_of_spares.txt")
TxtFileName.Sheets(1).Range("A1").CurrentRegion.Copy
Workbooks("Macro Test.xlsm").Activate
ActiveWorkbook.Sheets(1).Range("A1").Select
ActiveSheet.Paste
Workbooks(2).Close
End Sub
If you mention Workbooks(1).Close, it would close the first opened excel, here we want to close the second workbook hence Workbooks(2).Close
I found some excel vba code on this link which helped:https://www.excel-easy.com/vba/examples/close-open.html
I have a worksheet that often needs to be printed in a specific order of worksheets and ranges. What I'm trying to do is
Print out specific worksheets and ranges in order to a single PDF file.
Save the PDF file as a specific date to a certain folder.
The obstacle I'm running into is getting the numerous sheets and ranges printed into one PDF file, as currently each worksheet or range prints out to its own single PDF file.
I'm assuming there's a way to put all the necessary sheets and ranges in something like an array and then do a .PrintOut to that variable, however I haven't gotten this to work.
I'm using Excel 2010 and so I just use the "Adobe PDF" Printer.
How can I print multiple ranges and worksheets to a single .pdf?
To print more than one worksheet, you can put the worksheet names in an array like this
Sub PrintArrayOfWorksheets()
Dim vaWorksheets As Variant
vaWorksheets = Array("Sheet1", "Sheet2", "Sheet3")
ThisWorkbook.Worksheets(vaWorksheets).PrintOut
End Sub
Printing to PDF has special problems. If you're using the "official" add-in for creating PDFs it will probably work as above (sorry I can't try it right now). In the past when I've worked with other printer drivers that print to PDF, I found that all (or at least most) of the PageSetup properties had to be identical on the sheets or it would print in multiple jobs. That means no shrink-to-fit and all the margins have to be the same. I can't remember all the properties that caused problems, just that those two definitely did and Orientation definitely didn't.
To limit the ranges that print, you need to set the print area on each sheet. You can use the PageSetup.PrintArea property to do it in code. But if doesn't change, it's probably just as well to do it manually one time and it will persist.
See code below. This was used to print multiple sheets in a custom order using a VBA Button. It prints to whatever printer is selected prior to clicking the button.
In essence what it does is selects the sheets that you want and rearranges them at the end of the workbook then prints them. after printing it reorders them to the original order.
Option Explicit
Private Sub Print_Sheets_InOrder_Click()
Dim ary
Dim orig() As String
Dim a As Variant, fp As String, i As Variant
ary = Array("Sheet1", "Sheet4", "Sheet3")
fp = ActiveWorkbook.Path
ReDim orig(1 To Sheets.Count)
For i = 1 To Sheets.Count
orig(i) = Sheets(i).Name
Next i
For Each a In ary
Sheets(a).Move after:=Sheets(Sheets.Count)
Next a
ThisWorkbook.Worksheets(ary).PrintOut
For Each a In orig
Sheets(a).Move after:=Sheets(Sheets.Count)
Next a
End Sub
Excel 2010 has a print to pdf feature. If each of your worksheets are set up to print the information that you want, then highlight the various worksheets you want to print to one pdf file, then go to "file" and "save-as" and pick save as type "pdf". It should print the worksheets you have highlighted as you have the print set up for each worksheet.
Here is an approach I have tried ... I print the worksheets to a PS file, then double-click on the PS file to generate a PDF. I know it is a workaround, but printing to PS is lightning quick. It requires you have Adobe Acrobat on your PC.
Dim GetPath, SavePath, ReportSuffix
GetPath = ActiveWorkbook.Path & "\"
ReportSuffix = Range("ReportName").Text & ".ps"
SavePath = GetPath & ReportSuffix
Dim PrintSheets
PrintSheets = Array("Exec1", "Intro1", "Intro2", "Intro3", "Intro4", "Intro5")
Sheets(PrintSheets).PrintOut Copies:=1, PrintTofile:=True, PrToFileName:=SavePath
I am new to macros.I want to write a macro to copy specific data in columns in MPP to another using excel.
I have found a code that will copy data from one excel to another.please help
Option Explicit
Sub CopytoPS()
Dim sfil As String
Dim owbk As Workbook
Dim sPath As String
sPath = "C:\Users\HYMC\Excel\Test\" 'Change the file path for your purposes
sfil = Dir(sPath & "Management Report PS.xls")
Range("A2:I22").Copy
Set owbk = Workbooks.Open(sPath & sfil)
owbk.Sheets("Sales Data").Range("B65536").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
owbk.Close True 'Save opened workbook and close
sfil = Dir
End Sub
I want to copy certain coloums in MPP to a set of cloumns in Excel.I also want user to just give the destination file path,source file ,source cells to be copied and destination cells
To work with MPP file in Excel, Open the VBA Editor and click References on the Tools menu. In the Available References list, click to select the Microsoft Project xx.xx Object Library check box. If the Microsoft Project 9.0 Object Library is not listed, click Browse to locate the MsprjXX.olb file, which is in the folder where you have Microsoft Project installed. The default location is C:\Program Files\Microsoft Office\Office. Click OK to close the References dialog box. Then Use this code.
Since you have not mentioned what you want to copy and where exactly, i will give you a very basic code which you can then work on.
'~~> Code to open MPP file in Excel
Sub Sample()
Dim appProj As MSProject.Application
Dim aProg As MSProject.Project
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
'~~> This is the Sheet Where you want the data to be copied
Set ws = wb.Sheets("Sheet1")
Set appProj = CreateObject("Msproject.Application")
'~~> This is a MS Project File. Change path as applicable.
appProj.FileOpen "C:\MS Project.mpp"
Set aProg = appProj.ActiveProject
appProj.Visible = True
'~~> Now you have the MPP file opened, rest of the code goes here
End Sub
prerna: Can u also provide me the tutorial to learn macros to be used in excel.It would be very helpful
You can visit this link which is a good start. But ultimately, it all depends on how much you practice :)
Topic: Record and use Excel macros
Link: http://office.microsoft.com/en-us/excel-help/record-and-use-excel-macros-HA001054837.aspx
More On Macros:
http://www.excel-vba.com/
http://www.excel-vba-easy.com/
http://www.mrexcel.com/articles.shtml