<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; VBA: Каждую строку экспортироваьь в отдельный файл]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=11514&amp;type=atom" />
	<updated>2016-04-20T08:58:18Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=11514</id>
		<entry>
			<title type="html"><![CDATA[VBA: Каждую строку экспортироваьь в отдельный файл]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=102739#p102739" />
			<content type="html"><![CDATA[<p>Всем привет! Есть макрос загрузки в эксель:<br /></p><div class="codebox"><pre><code>
&#039;Variable to hold default root folder name
Dim strRootFolder
strRootFolder = &quot;X:\МАКРОСЫ\&quot;

Dim reportName 
reportName=&quot;Product&quot;

Dim WidgetID 
WidgetID = &quot;ProductB&quot;

Dim widgetProductA
widgetProductA = &quot;A&quot;

Dim widgetProductB 
widgetProductB = &quot;B&quot;

Dim widgetProductC 
widgetProductC = &quot;C&quot;

Function ExportProduct()

CALL CheckFolderExists(strRootFolder)	

ActiveDocument.ClearAll true

Set xlApp = CreateObject(&quot;Excel.Application&quot;)
xlApp.Visible = true	
Set xlDoc = xlApp.Workbooks.Add &#039;open new workbook
nSheetsCount = 0
CALL RemoveDefaultSheet(xlDoc)	

nSheetsCount = xlDoc.Sheets.Count 
xlDoc.Sheets(nSheetsCount).Select
Set xlSheet = xlDoc.Sheets(nSheetsCount)	

CALL ExportRevenueWidgets(xlDoc,xlSheet)	

&#039;Save generated report
xlApp.ActiveWorkBook.SaveAs strRootFolder &amp;&quot; &quot;&amp;reportName &amp; &quot;.xlsx&quot; 
xlApp.Quit	

End Function

&#039;Call Export Widgets By Sheet
Function ExportRevenueWidgets(xlDoc,xlSheet)
ActiveDocument.GetField(&quot;ProductNam e&quot;).select widgetProductA
CALL ExportWidget(xlDoc,xlSheet,WidgetID , widgetProductA)
ActiveDocument.GetField(&quot;ProductNam e&quot;).Clear	
ActiveDocument.GetField(&quot;ProductNam e&quot;).select widgetProductB
CALL ExportWidget(xlDoc,xlSheet,WidgetID , widgetProductB)
ActiveDocument.GetField(&quot;ProductNam e&quot;).Clear
ActiveDocument.GetField(&quot;ProductNam e&quot;).select widgetProductC
CALL ExportWidget(xlDoc,xlSheet,WidgetID , widgetProductC)
ActiveDocument.GetField(&quot;ProductNam e&quot;).Clear
End Function

&#039;Export Widgets by Type
Function ExportWidget(xlDoc,xlSheet,widget, Value)	
Select Case Value
Case widgetProductA:	
Call Export(0,xlSheet,widget,xlDoc,widge tProductA)	
Case widgetProductB:	
Call Export(1,xlSheet,widget,xlDoc,widge tProductB)
Case widgetProductC:	
Call Export(1,xlSheet,widget,xlDoc,widge tProductC)
End Select
End Function

&#039;Export Widgets
Function Export(IsNeedNewSheet,xlSheet,widge tID,xlDoc,sheetName)	

If IsNeedNewSheet = 1 then
CALL AddExcelSheet(xlDoc,sheetName)
nSheetsCount = xlDoc.Sheets.Count
xlDoc.Sheets(nSheetsCount).Select
Set xlSheet = xlDoc.Sheets(nSheetsCount)
Else
xlSheet.Name = sheetName
End If	

nRow = xlSheet.UsedRange.Rows.Count

If nRow &gt; 1 Then
nRow = nRow + 4
Else
nRow = nRow + 2
End If

Set SheetObj = ActiveDocument.GetSheetObject(widge tID)	

ObjCaption = SheetObj.GetCaption.Name.v
xlSheet.Range(&quot;A&quot;&amp;nRow-1) = ObjCaption
xlSheet.Range(&quot;A&quot;&amp;nRow-1).Font.Bold = true

&#039;Copy the chart object to clipboard
SheetObj.CopyTableToClipboard true

&#039;Paste the chart object in Excel file
xlSheet.Paste xlSheet.Range(&quot;A&quot;&amp;nRow) 

&#039;Format the excel file	
xlSheet.cells.Font.Size = &quot;8&quot;
xlSheet.cells.Font.Name = &quot;Tahoma&quot;	

End Function

&#039;Add New Sheet in Excel File
Sub AddExcelSheet(xlDoc, strSheetName)

xlDoc.Sheets.Add, xlDoc.Sheets(xlDoc.Sheets.Count)
Set xlSheet = xlDoc.Sheets(xlDoc.Sheets.Count)
xlSheet.Name = Left(strSheetName, 31)
End Sub

&#039;Remove Default Sheets from Excel Files
Sub RemoveDefaultSheet(xlDoc)
Do
nSheetsCount = xlDoc.Sheets.Count
If nSheetsCount = 1 then
Exit Do
Else
xlDoc.Sheets(nSheetsCount).Select
xlDoc.ActiveSheet.Delete
End If
Loop
End Sub


&#039;Checks whether given folder exists if not creates the given folder
Function CheckFolderExists(path)	

Set fileSystemObject = CreateObject(&quot;Scripting.FileSystemO bject&quot;) 

If Not fileSystemObject.FolderExists(path) Then
fileSystemObject.CreateFolder(path) 
End If

End Function
</code></pre></div><p>Проблема в том, что таблицу, которую макрос экспортирует в эксель, он ее разбивает по строкам. И каждой строке таблицы соответствует свой отдельный лист.<br />Задача: чтобы макрос разбивал ТАКЖЕ построчно, но КАЖДАЯ строка экспортировалась в ОТДЕЛЬНЫЙ эксель файл (рисунок ниже).<br />Могу предположить, что проблема в функции Function ExportProduct(), но не уверен, т.к. в макросах очень слабоват.</p>]]></content>
			<author>
				<name><![CDATA[kevlevmax]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=33842</uri>
			</author>
			<updated>2016-04-20T08:58:18Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=102739#p102739</id>
		</entry>
</feed>
