Vous êtes sur la page 1sur 4

can you export multiple ranges from one excel worksheet into separate notepad

files all at once using a formula or a macro? a4:c26 sheet3 into one notepad file,
f4:h26 sheet3 into a second notepad file, etc.

Answer:

This amended code will use tabs instead of commas and will use the value as it is
formatted in the cell.

Sub ExportToNotepad()

WriteRangeToTextFile Range("A4:C26"), "H:\temp\file1.txt", vbTab


WriteRangeToTextFile Range("F4:H26"), "H:\temp\file2.txt", vbTab

Shell "notepad.exe h:\temp\file1.txt", vbMaximizedFocus


Shell "notepad.exe h:\temp\file2.txt", vbMaximizedFocus

End Sub

Sub WriteRangeToTextFile(Source As Range, Path As String, Delimiter As String)


Dim oFSO As Object
Dim oFSTS As Object
Dim lngRow As Long, lngCol As Long

Set oFSO = CreateObject("Scripting.FileSystemObject")


Set oFSTS = oFSO.CreateTextFile(Path, True)

For lngRow = 1 To Source.Rows.Count

For lngCol = 1 To Source.Columns.Count

If lngCol = Source.Columns.Count Then


oFSTS.Write Source.Cells(lngRow, lngCol).Text & vbCrLf
Else
oFSTS.Write Source.Cells(lngRow, lngCol).Text & Delimiter
End If

Next lngCol

Next lngRow

oFSTS.Close

Set oFSTS = Nothing


Set oFSO = Nothing

End Sub

How can I simulate the creation of an array formula thru VBA code?
Situation:
My function needs to output an array of values and each cell of the output should have the formula
{=MyFunction(B10,A10,C15)}, so that if I change any parameter of my function, (for example: If I change B10 to
B12) each value of the array should automatically recalculate it's value as well as modify it's formula to reflect the
change.

I am working on Excel 97.

Answer:

What you really want is unclear to me, but with this you might get it to work the way you want:

Range has formula property which you can set:

Worksheets("Sheet1").Range("A1").Formula = "=$A$4+$A$10"

Let me try again.


To put it in very simple terms, how can I get a function written in VBA to return an
array of values to the Excel Spreadsheet?

Answer:

Are you looking for this?

Sub try_array()
Dim data As Variant
data = Range("A2:A6").Value
Range("C2:C6").Value = data
End Sub

ok?

How can I output VBA function results


(array's for example) to spreadsheet?
I write custom functions that often collect data from an excel
spreadsheet in the open workbook, do a bunch of other calcs with the
data, and return a 2-d array results. The problem is that I want to
output the array results to some new or existing spreadsheet in the
workbook but don't want to use use array formula's (i.e.
Ctrl-Shift-Enter) to place the resulting output result where the array
formula's entered.

In past I've used a work-around by putting all the functions that


return results I want to put into the spreadsheet under a sub(), then
call another sub() to display results in the worksheet of interest by
looping thru the appropriate rows/col's (ie. Cells(r,c).value = x(i)).

But isn't there a way to get the results to be displayed in the


appropriate worksheet cells I chose from within a function() instead of
having to go put everything under a sub()?

For example, the following doesn't work to get data placed in the
requested cells (or anywhere on a the active spreadsheet).

e.g. Assume data is 1-d array of unknown number of elements


function output(data as variant)

dim cnt as integer, i as integer, r as integer c as integer


cnt = Ubound(data)
'assume r & c are given initial values from functions rowstart() &
colstart() that finds someplace on the spreadsheet of interest to start
placing the data.
r = rowstart()
c = colstart()

'place data in column c


for i = 0 to cnt-1
Cells(r,c).value = data(i)
r=r+1
next i

End function

On the other hand, if I replace function routine output(data as


variant) with sub routine sub(data as variant) everything works just
fine.

Is there something I have to put into or call from within the function
to get it to work as I'd like or what am I missing?

Answer:

That looks to be fine as long as R and C are valid and the array was set-up
properly. You do need to give sheet names somewhere, i'm assuming you have
this function in a module.
This is how I did it, I don't like names like data either, it will be bad
too often.
For i = 0 To cnt - 1
Cells(r, c) = myData(i, 1)
r=r+1
Next

Vous aimerez peut-être aussi