Sure! Here's a sample. Also see my comments after the code below...
First, I created a worksheet.
Then on sheet1, I typed in some sample data from A1 to B4. I put the first names in column A and the last names in column B:
john smith
james johnson
susan jones
mary hanson
Next, in VBA, I inserted a module.
In the module I placed the following code:
Option Explicit
Private Const strPath As String = "C:\Windows\Desktop\"
Private Const strFileName As String = "File1.txt"
Public Sub LoopThroughCells()
Dim objWorksheet As Excel.Worksheet
Dim objRange As Excel.Range
Dim lRowCount As Long
Dim lColumnCount As Long
Dim lCurrentRow As Long
Open strPath + strFileName For Output As #1
Set objWorksheet = Application.Sheets("Sheet1")
With objWorksheet
.Activate
.Range("A1").Activate
Set objRange = .Cells.CurrentRegion
lRowCount = objRange.Rows.Count
lColumnCount = objRange.Columns.Count
lCurrentRow = 1
For lCurrentRow = 1 To lRowCount
Write #1, .Cells(lCurrentRow, 1),'a comma = continued record
Write #1, .Cells(lCurrentRow, 2)
Next
End With
Close #1
Set objRange = Nothing
Set objWorksheet = Nothing
End Sub
Comments:
There are many ways to select cells and define ranges. Some people just create a range and iterate through the cells from left to right, row by row, such as A1, B1, A2, B2, etc.
The main thing to remember is that a range simply refers to a collection of cells, rows, columns, or anything else you wish to define. Once you give the parameters of the range, you can select any element you wish from it.
For example, my range had 4 contact names over 8 cells. I could have selected the 7th item in the range if I wished to pull this out.
Also, I chose CurrentRegion which selects all non-blank cells in a group of cells. But if you know explicity that you only want to return data from a particular starting and ending row, you don't need to use CurrentRegion.
Oftentimes I've also used the starting and ending arguments in the range command or used the offset method.
It may pay to use generic starting and ending points to keep things simple. I don't use the word "relative" because that refers to cell addresses
For example, your range may go from A20:A40. If you wanted the first row, you don't get A1. You get A20 because as far as the range is concerned, that is the first row. That is why setting up your variables and loops carefully ensures you don't pull data from the wrong location.
Now if you wanted data from only certain cells, all you need do is to test the value of the cell for whatever property you are testing for and if it satisfies your criteria, you could choose to include that data in the file you are writing out.
One last gotcha to watch for: If you do anything that changes the current range (such as deleting a row on the worksheet, or using an offset from your current position) be mindful that your range may not be the same - it may have collapsed or expanded. You may want to redefine your range by setting it again.
Let me know if you have any other questions. Happy Holiday!
Joe G