Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Output to a file?
This is my first attempt at programming Excel so please bear with me.
I have a sheet (Sheet3 named "Columns"). On this sheet there are three columns that I am interested in Table, CodeColumn, Column. Where ever the value in CodeColumn is not blank I want to form: SELECT "Column" FROM "Table" WHERE "Column" NOT IN (SELECT "Column" FROM "CodeColumn") The quoted names would be the values in the spread sheet. In the end I would end up with a text file that has the above formated text for each line that CodeColumn is not blank. Is this hard? First I am not sure how to specify the sheet, next I don't know how to iterate through each row in the sheet, and I am not sure how to output the data to a file (I would be happy with the lines in the clipboard if that was easiest). Thank you in advance for your help even if it is just to get me started. Kevin Burton |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Output to a file?
Give this code a try. To use ti you must reference it to "Microsoft Scripting
Runtime" (Tools - References...) Sub CreateSelects() Dim fso As FileSystemObject Dim fsoFile As Variant Dim wks As Worksheet Dim rngToTraverse As Range Dim rngCurrent As Range Set wks = Sheets("Sheet1") Set rngToTraverse = wks.Range(wks.Range("A2"), _ wks.Cells(Rows.Count, "A").End(xlUp)) Set fso = New FileSystemObject Set fsoFile = fso.CreateTextFile("C:\Output.txt") For Each rngCurrent In rngToTraverse If rngCurrent.Value < Empty Then _ fsoFile.writeline "SELECT " & rngCurrent.Value & _ " FROM " & rngCurrent.Offset(0, 1).Value & " ..." Next rngCurrent fsoFile.Close Set fsoFile = Nothing Set fso = Nothing End Sub Since I do not know which columns to use you will have to modify the code to suit your needs... -- HTH... Jim Thomlinson "Kevin Burton" wrote: This is my first attempt at programming Excel so please bear with me. I have a sheet (Sheet3 named "Columns"). On this sheet there are three columns that I am interested in Table, CodeColumn, Column. Where ever the value in CodeColumn is not blank I want to form: SELECT "Column" FROM "Table" WHERE "Column" NOT IN (SELECT "Column" FROM "CodeColumn") The quoted names would be the values in the spread sheet. In the end I would end up with a text file that has the above formated text for each line that CodeColumn is not blank. Is this hard? First I am not sure how to specify the sheet, next I don't know how to iterate through each row in the sheet, and I am not sure how to output the data to a file (I would be happy with the lines in the clipboard if that was easiest). Thank you in advance for your help even if it is just to get me started. Kevin Burton |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Output to a file?
Thank you. This definitely got me started.
This line is causing some problem: Set wks = Sheets("Sheet1") When I try to run this I get an error "Method 'Sheets' of object _Global failed." For my spread sheet (.xls file) this is Sheet3 with a name of "Columns" I am guessing that I should rename this to "Sheet3". Finally I am not sure what the syntax around rngCurrent.Offset(0, 1).Value is. I am interested in columns A, I, and J. You set rngToTraverse to column 'A' starting at row 2. I am interested in the non blank column at 'I'. If I change the 'A' to 'I' so it looks like: Set rngToTraverse = wks.Range(wks.Range("I2"), _ wks.Cells(Rows.Count, "I").End(xlUp)) As I understand it when the test for a non empty cell to reference column 'A' I would input an Offset(0,-8) right? Thanks again. Kevin "Jim Thomlinson" wrote: Give this code a try. To use ti you must reference it to "Microsoft Scripting Runtime" (Tools - References...) Sub CreateSelects() Dim fso As FileSystemObject Dim fsoFile As Variant Dim wks As Worksheet Dim rngToTraverse As Range Dim rngCurrent As Range Set wks = Sheets("Sheet1") Set rngToTraverse = wks.Range(wks.Range("A2"), _ wks.Cells(Rows.Count, "A").End(xlUp)) Set fso = New FileSystemObject Set fsoFile = fso.CreateTextFile("C:\Output.txt") For Each rngCurrent In rngToTraverse If rngCurrent.Value < Empty Then _ fsoFile.writeline "SELECT " & rngCurrent.Value & _ " FROM " & rngCurrent.Offset(0, 1).Value & " ..." Next rngCurrent fsoFile.Close Set fsoFile = Nothing Set fso = Nothing End Sub Since I do not know which columns to use you will have to modify the code to suit your needs... -- HTH... Jim Thomlinson "Kevin Burton" wrote: This is my first attempt at programming Excel so please bear with me. I have a sheet (Sheet3 named "Columns"). On this sheet there are three columns that I am interested in Table, CodeColumn, Column. Where ever the value in CodeColumn is not blank I want to form: SELECT "Column" FROM "Table" WHERE "Column" NOT IN (SELECT "Column" FROM "CodeColumn") The quoted names would be the values in the spread sheet. In the end I would end up with a text file that has the above formated text for each line that CodeColumn is not blank. Is this hard? First I am not sure how to specify the sheet, next I don't know how to iterate through each row in the sheet, and I am not sure how to output the data to a file (I would be happy with the lines in the clipboard if that was easiest). Thank you in advance for your help even if it is just to get me started. Kevin Burton |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Output to a file?
For the most part you have it...
set wks = sheets("Sheet1") Since I did not know which sheet it is that your list is on I made it variable. "Sheet1" is the tab name, so if your tab is "Columns" then you want set wks = Columns("Sheet1") You could also just use set wks = sheet3 'where sheet3 is the code name rngCurrent is a range object that is traversing A2 throught A??. Change it to I if that is what you need. The offset is just a way of looking outside the current cell. The format is row, column. The values can be + or -, just be sure not to offset right off of the worksheet or you will receive a run time error. It looks like you have this correct in your assesment. -- HTH... Jim Thomlinson "Kevin Burton" wrote: Thank you. This definitely got me started. This line is causing some problem: Set wks = Sheets("Sheet1") When I try to run this I get an error "Method 'Sheets' of object _Global failed." For my spread sheet (.xls file) this is Sheet3 with a name of "Columns" I am guessing that I should rename this to "Sheet3". Finally I am not sure what the syntax around rngCurrent.Offset(0, 1).Value is. I am interested in columns A, I, and J. You set rngToTraverse to column 'A' starting at row 2. I am interested in the non blank column at 'I'. If I change the 'A' to 'I' so it looks like: Set rngToTraverse = wks.Range(wks.Range("I2"), _ wks.Cells(Rows.Count, "I").End(xlUp)) As I understand it when the test for a non empty cell to reference column 'A' I would input an Offset(0,-8) right? Thanks again. Kevin "Jim Thomlinson" wrote: Give this code a try. To use ti you must reference it to "Microsoft Scripting Runtime" (Tools - References...) Sub CreateSelects() Dim fso As FileSystemObject Dim fsoFile As Variant Dim wks As Worksheet Dim rngToTraverse As Range Dim rngCurrent As Range Set wks = Sheets("Sheet1") Set rngToTraverse = wks.Range(wks.Range("A2"), _ wks.Cells(Rows.Count, "A").End(xlUp)) Set fso = New FileSystemObject Set fsoFile = fso.CreateTextFile("C:\Output.txt") For Each rngCurrent In rngToTraverse If rngCurrent.Value < Empty Then _ fsoFile.writeline "SELECT " & rngCurrent.Value & _ " FROM " & rngCurrent.Offset(0, 1).Value & " ..." Next rngCurrent fsoFile.Close Set fsoFile = Nothing Set fso = Nothing End Sub Since I do not know which columns to use you will have to modify the code to suit your needs... -- HTH... Jim Thomlinson "Kevin Burton" wrote: This is my first attempt at programming Excel so please bear with me. I have a sheet (Sheet3 named "Columns"). On this sheet there are three columns that I am interested in Table, CodeColumn, Column. Where ever the value in CodeColumn is not blank I want to form: SELECT "Column" FROM "Table" WHERE "Column" NOT IN (SELECT "Column" FROM "CodeColumn") The quoted names would be the values in the spread sheet. In the end I would end up with a text file that has the above formated text for each line that CodeColumn is not blank. Is this hard? First I am not sure how to specify the sheet, next I don't know how to iterate through each row in the sheet, and I am not sure how to output the data to a file (I would be happy with the lines in the clipboard if that was easiest). Thank you in advance for your help even if it is just to get me started. Kevin Burton |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Output to a file?
Now I have:
Dim fso As FileSystemObject Dim fsoFile As Variant Dim wks As Worksheet Dim rngToTraverse As Range Dim rngCurrent As Range Set wks = Columns("Sheet1") Set rngToTraverse = wks.Range(wks.Range("I2"), _ wks.Cells(Rows.Count, "I").End(xlUp)) Set fso = New FileSystemObject Set fsoFile = fso.CreateTextFile("C:\Output.txt") For Each rngCurrent In rngToTraverse If rngCurrent.Value < Empty Then _ fsoFile.writeline "SELECT " & rngCurrent.Offset(0, 1).Value & _ " FROM " & rngCurrent.Offset(0, -8).Value & _ " WHERE " & rngCurrent.Offset(0, 1).Value & " NOT IN " & _ "(SELECT " & rngCurrent.Offset(0, 1).Value & " FROM " & rngCurrent.Value & ")" Next rngCurrent fsoFile.Close Set fsoFile = Nothing Set fso = Nothing But the line Set wks = Columns("Sheet1") Gives me an error "Runtime error '13': Type mismatch I think I have not communicated the structure of the Excel document correctly. I have a file (call it Changes.xls). In that file there are four tabs (sheets?) at the bottom the sheet that I am interested in is named "Columns" it is sheet3. Thank you. Kevin "Jim Thomlinson" wrote: For the most part you have it... set wks = sheets("Sheet1") Since I did not know which sheet it is that your list is on I made it variable. "Sheet1" is the tab name, so if your tab is "Columns" then you want set wks = Columns("Sheet1") You could also just use set wks = sheet3 'where sheet3 is the code name rngCurrent is a range object that is traversing A2 throught A??. Change it to I if that is what you need. The offset is just a way of looking outside the current cell. The format is row, column. The values can be + or -, just be sure not to offset right off of the worksheet or you will receive a run time error. It looks like you have this correct in your assesment. -- HTH... Jim Thomlinson "Kevin Burton" wrote: Thank you. This definitely got me started. This line is causing some problem: Set wks = Sheets("Sheet1") When I try to run this I get an error "Method 'Sheets' of object _Global failed." For my spread sheet (.xls file) this is Sheet3 with a name of "Columns" I am guessing that I should rename this to "Sheet3". Finally I am not sure what the syntax around rngCurrent.Offset(0, 1).Value is. I am interested in columns A, I, and J. You set rngToTraverse to column 'A' starting at row 2. I am interested in the non blank column at 'I'. If I change the 'A' to 'I' so it looks like: Set rngToTraverse = wks.Range(wks.Range("I2"), _ wks.Cells(Rows.Count, "I").End(xlUp)) As I understand it when the test for a non empty cell to reference column 'A' I would input an Offset(0,-8) right? Thanks again. Kevin "Jim Thomlinson" wrote: Give this code a try. To use ti you must reference it to "Microsoft Scripting Runtime" (Tools - References...) Sub CreateSelects() Dim fso As FileSystemObject Dim fsoFile As Variant Dim wks As Worksheet Dim rngToTraverse As Range Dim rngCurrent As Range Set wks = Sheets("Sheet1") Set rngToTraverse = wks.Range(wks.Range("A2"), _ wks.Cells(Rows.Count, "A").End(xlUp)) Set fso = New FileSystemObject Set fsoFile = fso.CreateTextFile("C:\Output.txt") For Each rngCurrent In rngToTraverse If rngCurrent.Value < Empty Then _ fsoFile.writeline "SELECT " & rngCurrent.Value & _ " FROM " & rngCurrent.Offset(0, 1).Value & " ..." Next rngCurrent fsoFile.Close Set fsoFile = Nothing Set fso = Nothing End Sub Since I do not know which columns to use you will have to modify the code to suit your needs... -- HTH... Jim Thomlinson "Kevin Burton" wrote: This is my first attempt at programming Excel so please bear with me. I have a sheet (Sheet3 named "Columns"). On this sheet there are three columns that I am interested in Table, CodeColumn, Column. Where ever the value in CodeColumn is not blank I want to form: SELECT "Column" FROM "Table" WHERE "Column" NOT IN (SELECT "Column" FROM "CodeColumn") The quoted names would be the values in the spread sheet. In the end I would end up with a text file that has the above formated text for each line that CodeColumn is not blank. Is this hard? First I am not sure how to specify the sheet, next I don't know how to iterate through each row in the sheet, and I am not sure how to output the data to a file (I would be happy with the lines in the clipboard if that was easiest). Thank you in advance for your help even if it is just to get me started. Kevin Burton |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Open a print output sent to file | Excel Discussion (Misc queries) | |||
Cannot open file for output | Excel Programming | |||
CSV output file | Excel Programming | |||
VBA help to output a file | Excel Programming | |||
Trying to protect output file with Macro | Excel Programming |