Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Find a cell value then read the next row as a record of columns

I am trying to do the following: Search for the cell containing "Hours".
Once I find it, drop down 2 more rows and read 48 columns of data, then drop
down another row and read another 48 columns of data. I think I am mixing
Cell and Range stuff together but I am not getting any where. I get datatype
mismatch errors on the For loop

Thanks


'Assumes data starts in column C
Set rRng = ActiveSheet.UsedRange.Columns("C:C")

For i = 1 To rRng.Rows.Count
If ActiveSheet.Cells(i, 3).Value = "Hours" Then
Set rngTemp = ActiveSheet.Cells(i, 3)

Exit For
End If
Next

If Not rngTemp Is Nothing Then
tmpStr = "C" & rngTemp.Row + 2 & ":AY" & (rngTemp.Row + 2)
Set rRng = wsh.Range(tmpStr)
i = 1
For Each c In wsh.Range(tmpStr).Cells
sMonth = "Month" & i & "="
If i < 48 Then
sSQL = sSQL & sMonth & c.Value & ", "
Else
sSQL = sSQL & sMonth & c.Value & ", "
End If
i = i + 1
Next
MsgBox sSQL
End If

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Find a cell value then read the next row as a record of columns

You had a few little probelms. I modified the code to make it easier to
understand.

Sub findhours()

With ActiveSheet
Set HourCell = .Columns("C").Find(what:="Hours", LookIn:=xlValues, _
lookat:=xlWhole)
If Not HourCell Is Nothing Then
tmpStr = "C" & HourCell.Row + 2 & ":AY" & (HourCell.Row + 2)
Set rRng = .Range(tmpStr)

i = 1
sSQL = ""
For Each c In rRng
sMonth = "Month" & i & "="
If i < 48 Then
sSQL = sSQL & sMonth & c.Value & ", "
Else
sSQL = sSQL & sMonth & c.Value & ", "
End If
i = i + 1
Next
MsgBox sSQL
End If


End Sub


"chemicals" wrote:

I am trying to do the following: Search for the cell containing "Hours".
Once I find it, drop down 2 more rows and read 48 columns of data, then drop
down another row and read another 48 columns of data. I think I am mixing
Cell and Range stuff together but I am not getting any where. I get datatype
mismatch errors on the For loop

Thanks


'Assumes data starts in column C
Set rRng = ActiveSheet.UsedRange.Columns("C:C")

For i = 1 To rRng.Rows.Count
If ActiveSheet.Cells(i, 3).Value = "Hours" Then
Set rngTemp = ActiveSheet.Cells(i, 3)

Exit For
End If
Next

If Not rngTemp Is Nothing Then
tmpStr = "C" & rngTemp.Row + 2 & ":AY" & (rngTemp.Row + 2)
Set rRng = wsh.Range(tmpStr)
i = 1
For Each c In wsh.Range(tmpStr).Cells
sMonth = "Month" & i & "="
If i < 48 Then
sSQL = sSQL & sMonth & c.Value & ", "
Else
sSQL = sSQL & sMonth & c.Value & ", "
End If
i = i + 1
Next
MsgBox sSQL
End If

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Find a cell value then read the next row as a record of column

Thanks Joel it works great...!

"Joel" wrote:

You had a few little probelms. I modified the code to make it easier to
understand.

Sub findhours()

With ActiveSheet
Set HourCell = .Columns("C").Find(what:="Hours", LookIn:=xlValues, _
lookat:=xlWhole)
If Not HourCell Is Nothing Then
tmpStr = "C" & HourCell.Row + 2 & ":AY" & (HourCell.Row + 2)
Set rRng = .Range(tmpStr)

i = 1
sSQL = ""
For Each c In rRng
sMonth = "Month" & i & "="
If i < 48 Then
sSQL = sSQL & sMonth & c.Value & ", "
Else
sSQL = sSQL & sMonth & c.Value & ", "
End If
i = i + 1
Next
MsgBox sSQL
End If


End Sub


"chemicals" wrote:

I am trying to do the following: Search for the cell containing "Hours".
Once I find it, drop down 2 more rows and read 48 columns of data, then drop
down another row and read another 48 columns of data. I think I am mixing
Cell and Range stuff together but I am not getting any where. I get datatype
mismatch errors on the For loop

Thanks


'Assumes data starts in column C
Set rRng = ActiveSheet.UsedRange.Columns("C:C")

For i = 1 To rRng.Rows.Count
If ActiveSheet.Cells(i, 3).Value = "Hours" Then
Set rngTemp = ActiveSheet.Cells(i, 3)

Exit For
End If
Next

If Not rngTemp Is Nothing Then
tmpStr = "C" & rngTemp.Row + 2 & ":AY" & (rngTemp.Row + 2)
Set rRng = wsh.Range(tmpStr)
i = 1
For Each c In wsh.Range(tmpStr).Cells
sMonth = "Month" & i & "="
If i < 48 Then
sSQL = sSQL & sMonth & c.Value & ", "
Else
sSQL = sSQL & sMonth & c.Value & ", "
End If
i = i + 1
Next
MsgBox sSQL
End If

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
READ ONLY RECORD SETS James McDowell[_2_] Excel Programming 2 October 19th 07 05:19 PM
Find record MarkN Excel Worksheet Functions 3 November 9th 06 07:42 AM
Find match between 2 columns and then record data that is in colum Karl Excel Discussion (Misc queries) 3 December 8th 05 09:56 PM
Find the Record Syed Haider Ali[_11_] Excel Programming 2 August 21st 05 11:03 PM
A question on read the record from excel weizi Excel Programming 0 July 16th 03 04:42 AM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"