ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Problems with saving cell values to file (https://www.excelbanter.com/excel-programming/319565-problems-saving-cell-values-file.html)

Mika[_2_]

Problems with saving cell values to file
 
(Sorry, if my message arrives twice. I can't see the first one.)


Const SaveTheseCells As String = "M4:O4;M6:O6;M8:O8;M10:O10;M12:O12"

Sub SaveToFile()
Dim MySheet As Worksheet

For Each MySheet In ActiveWorkbook.Worksheets
Select Case MySheet.CodeName
Case "SpecialSheet":
Dim SheetToFile As Worksheet
Set SheetToFile = MySheet
End Select
Next MySheet

Set MySheet = Nothing

Dim FileName As String
FileName = "C:\test.txt"

FileNum = FreeFile

Open FileName For Output As #FileNum
Print #FileNum, "start"

SheetToFile.Range(SaveTheseCells).Select

Dim cell As Range

'This does not save the values of M4, N4, O4, etc.
'What is wrong with this?

For Each cell in Selection.Cells
Print #FileNum, cell.Text
Next cell

Print #FileNum, "end"
Close #FileNum

Set SheetToFile = Nothing
End Sub

Jim Cone

Problems with saving cell values to file
 
Mika,

See if this helps...

Dim Cell as Range
Dim CellArea as Range

For Each CellArea in Selection.Areas
For Each Cell in CellArea
Print #FileNum, Cell.Text
Next 'Cell
Next 'CellArea

Regards,
Jim Cone
San Francisco, USA

"Mika" wrote in message
...
(Sorry, if my message arrives twice. I can't see the first one.)
Const SaveTheseCells As String = "M4:O4;M6:O6;M8:O8;M10:O10;M12:O12"
Sub SaveToFile()
Dim MySheet As Worksheet
For Each MySheet In ActiveWorkbook.Worksheets
Select Case MySheet.CodeName
Case "SpecialSheet":
Dim SheetToFile As Worksheet
Set SheetToFile = MySheet
End Select
Next MySheet
Set MySheet = Nothing
Dim FileName As String
FileName = "C:\test.txt"
FileNum = FreeFile
Open FileName For Output As #FileNum
Print #FileNum, "start"
SheetToFile.Range(SaveTheseCells).Select
Dim cell As Range
'This does not save the values of M4, N4, O4, etc.
'What is wrong with this?

For Each cell in Selection.Cells
Print #FileNum, cell.Text
Next cell

Print #FileNum, "end"
Close #FileNum
Set SheetToFile = Nothing
End Sub


Dave Peterson[_5_]

Problems with saving cell values to file
 
I responded to the first one. (I didn't see the second one until later <bg.)

Mika wrote:

(Sorry, if my message arrives twice. I can't see the first one.)

Const SaveTheseCells As String = "M4:O4;M6:O6;M8:O8;M10:O10;M12:O12"

Sub SaveToFile()
Dim MySheet As Worksheet

For Each MySheet In ActiveWorkbook.Worksheets
Select Case MySheet.CodeName
Case "SpecialSheet":
Dim SheetToFile As Worksheet
Set SheetToFile = MySheet
End Select
Next MySheet

Set MySheet = Nothing

Dim FileName As String
FileName = "C:\test.txt"

FileNum = FreeFile

Open FileName For Output As #FileNum
Print #FileNum, "start"

SheetToFile.Range(SaveTheseCells).Select

Dim cell As Range

'This does not save the values of M4, N4, O4, etc.
'What is wrong with this?

For Each cell in Selection.Cells
Print #FileNum, cell.Text
Next cell

Print #FileNum, "end"
Close #FileNum

Set SheetToFile = Nothing
End Sub


--

Dave Peterson


All times are GMT +1. The time now is 12:17 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com