By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,478 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,478 IT Pros & Developers. It's quick & easy.

Excel Macro

P: 1
I am trying to write an excel macro that reads cells from an excel sheet sequentially (third column) and writes each to a seperate text file with the value in the adjacent cell in column A (user ID) in the name of the text file.

Please help, Thank you!
Jul 30 '09 #1
Share this Question
Share on Google+
1 Reply


ADezii
Expert 5K+
P: 8,679
@DataMo
Expand|Select|Wrap|Line Numbers
  1. Dim intLastRow As Integer
  2. Dim intRowCounter As Integer
  3. Const conPATH_TO_FILE As String = "C:\Test\"
  4.  
  5. 'Assumes you are working with the 1st Worksheet
  6. Worksheets("Sheet1").Activate
  7.  
  8. 'Retrieve the Last Row with Data
  9. intLastRow = ActiveSheet.Cells.SpecialCells(xlLastCell).Row
  10.  
  11. 'Cells(intRowCounter, 1) = User ID
  12. 'Cells(intRowCounter, 3) = Some predefined Value
  13.  
  14. For intRowCounter = 1 To intLastRow
  15.   'Make sure Data exists in Columns 1 and 3 for each Row
  16.   If (Len(Cells(intRowCounter, 1)) > 0 And Len(Cells(intRowCounter, 3))) > 0 Then
  17.     Open conPATH_TO_FILE & Cells(intRowCounter, 1) & ".txt" For Output As #1
  18.       Print #1, Cells(intRowCounter, 3)   'Print Value in Column 3 to
  19.   End If
  20.       Close #1
  21. Next
Aug 2 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.