Full Version: Looping through columns
UtterAccess Discussion Forums > Microsoft® Office > Microsoft Excel
PaulBrand
I've written this code which works but now I want to move to the next column and loop again.

j is the column position and the bit I need help with is in red...

I've attached the book too!!

CODE

Dim cn As Object, rs As ADODB.Recordset
Dim strMessageType As String
Dim datMessageTime As Date
Dim intMessageCount As Integer
Dim strTime As String
Dim i As Integer
Dim j As Integer
Dim ID As Integer


Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= C:\db1.mdb"
Set rs = New ADODB.Recordset
rs.Open "Table1", cn, adOpenKeyset, adLockOptimistic

Sheets("sheet1").Select
Range("A1").Select
Selection.End(xlToRight).Select

j = ActiveCell.Column

[color="red"]  strMessageType = Range("C1").Value[/color]  The range "C1" I want to increment until 'j'
i = 1

strTime = "A1"
ID = 2
With rs
    Do Until i = 3626
        .AddNew
        !ID = ID
        !MessageType = strMessageType
        !MessageTime = Range(strTime).Offset(i, 0)
        !MessageCount = Range(strTime).Offset(i, 1)
        .Update
        i = i + 1
    Loop
End With


End Sub
PaulBrand
CODE

Dim cn As Object, rs As ADODB.Recordset
Dim strMessageType As String
Dim datMessageTime As Date
Dim intMessageCount As Integer
Dim strRange As String
Dim i, j, k, l As Integer
Dim ID As Integer
Dim intCol As Integer
Dim strHeader As String
Dim wks As String

'Setup a db connection
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= C:\db1.mdb"
Set rs = New ADODB.Recordset
rs.Open "Table1", cn, adOpenKeyset, adLockOptimistic
wks = InputBox("What's the name of the worksheet?")
'Find last column

Sheets(wks).Select
Range("A1").Select
Selection.End(xlToRight).Select
j = ActiveCell.Column

Range("A1").Select
Selection.End(xlDown).Select
k = ActiveCell.Row

'Loop columns
strHeader = Range("A1").Address(False, False)

For intCol = 2 To j
strHeader = Range(strHeader).Offset(0, 1).Address

strMessageType = Range(strHeader).Value
i = 1

strRange = "A1"
ID = intCol
'Loop rows
With rs
    Do Until i = k
    'Write data
        .AddNew
        !ID = ID
        !MessageType = strMessageType
        !MessageTime = Range(strRange).Offset(i, 0)
        !MessageCount = Range(strRange).Offset(i, 1)
        .Update
        i = i + 1
    Loop
End With

Next intCol
KingMartin
Hi Paul,

you don't need this:

Range("A1").Select
Selection.End(xlToRight).Select
j = ActiveCell.Column

This will do (for activesheet and Excel 2003 and below)

j = [iv1].End(xlToLeft).Column

which will determine the last column (W in your example) but is that what you want to do?

It seems to me that you want to "normalize" your sheet, import A,B,C first, then A,B,D, then A,B,E, etc. until W. Correct?

Also, you are replacing the header in the first row with column number? (starting with 3,4 etc. until 23?)

Martin
PaulBrand
Hi Martin,

Thanks for the tip, I didn't think I would need to 'select' the column and row for 'End' but it works.

I do want to normalize, yes, but I wanted B1, A2, B2 then B1 A3, B3 and to loop to last row then move to next column and so forth till the last column. The actual file I'm working with has 93 columns and about 20,000 rows.

I'm not replacing the header though, I want this to be used for a number of uses against different data - so I've left the cell references as is (text "A1"...).

Cheers
Paul
KingMartin
QUOTE
but I wanted B1, A2, B2 then B1 A3, B3 and to loop to last row then move to next column and so forth till the last column.


Ok, it seemed to me that you wanted to import A,B,C (all the rows), then A,B, D (all the rows) etc. dazed.gif

Did you get it sorted Paul?

Martin
PaulBrand
Yeah,

Works a treat!!

o!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.