Off-topic Talk Where overpaid, underworked S2000 owners waste the worst part of their days before the drive home. This forum is for general chit chat and discussions not covered by the other off-topic forums.

Excel VB basic macro help on HW....

Thread Tools
 
Old Dec 10, 2010 | 06:27 AM
  #1  
s4play's Avatar
Thread Starter
Registered User
Member (Premium)
 
Joined: Mar 2004
Posts: 9,447
Likes: 16
From: West Coast baby
Default Excel VB basic macro help on HW....

I've waiting til the last minute to do some HW and can't figure this out in time before it's due....

Does anyone here know any Visual Basic and can help me write some basic macro code to search certain values in Excel and then copy them to a new spreadsheet?


here's an example:

I attached a sample problem that we were working on last night. There are a total of 6 problems on each spreadsheet and we need to extract 2 rows of data in each sample problem. Most of the data is in EXCEL spreadsheet and we are writing the code in Microsoft VB.

The actual problem:
BACKWARD DIGIT SPAN EXERCISE
Fri Sep 17 09:07:04 2010 : Time and date of exercise
: Subject identification
1 : Session number
600000 : Test time (milliseconds)
1000 : Stimulus time (milliseconds)
1000 : Inter-stimulus time (milliseconds)
pair # String Answer Response Result
1 642 246 642 Incorrect
2 871 178 178 Correct
3 4651 1564 165 Incorrect
4 3864 4683 4863 Incorrect
4 : Total number of digit spans
1 : Total number of spans correct
3 : Total number of spans incorrect
0 : Total number of spans unanswered
3 : Maximum span correct




For sample problem one, we need to extract the fields:

A13 (4 : Total number of digit spans)
A17 (3 : Maximum span correct)

Then do a simple math equation to divide 3/4 = 75%


The one unusual factor is that in each problem the data location is not always in a pre-determined row (such as A13, A17) but varies with each problem (see the master sheet . Is there a way we can run the search for specific wording (such as "total number of digit spans)?

So far the code we have been trying to use is not working:


--------------------------------------------
Sub Search()

Dim rngSearch As Range
Dim rngFirst As Range

With ThisWorkbook.Worksheets("Sheet1")
With .Columns(1) 'Can put .Range("A1:A30")
Set rngSearch = .Find(What:="Maximum span correct", LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not rngSearch Is Nothing Then
Set rngFirst = rngSearch
Do
rngSearch.EntireRow.Copy Destination:= _
ThisWorkbook.Worksheets("Sheet2").Cells(rngSearch. Row, rngSearch.Column)
Set rngSearch = .FindNext(After:=rngSearch)
Loop While Not rngSearch Is Nothing And rngSearch.Address <> rngFirst.Address
End If
End With
End With

End Sub
Reply
Old Dec 10, 2010 | 11:19 AM
  #2  
magician's Avatar
Registered User
 
Joined: Jul 2001
Posts: 6,592
Likes: 0
From: Yorba Linda, CA
Default

What do you think it's supposed to be doing that it isn't doing?

I tried running it and it appears to find "Maximum span correct" in column 1 of Sheet1 and copy each row in which it appears to the corresponding row in Sheet2.

It's hard to suggest how to make it right if we don't know why you think it's wrong.
Reply
Old Dec 10, 2010 | 12:29 PM
  #3  
s4play's Avatar
Thread Starter
Registered User
Member (Premium)
 
Joined: Mar 2004
Posts: 9,447
Likes: 16
From: West Coast baby
Default

thanks for looking at this...

We want the output to extract two lines of data (ones I bolded in the example)

When I run the program, I get an error in line 9? you don't get this?


rick
Reply
Old Dec 10, 2010 | 01:34 PM
  #4  
magician's Avatar
Registered User
 
Joined: Jul 2001
Posts: 6,592
Likes: 0
From: Yorba Linda, CA
Default

No errors at all.

I added a second loop to scan for "Total number of digit spans", and that worked fine as well (not that I thought it wouldn't).

As you haven't numbered the lines I'm not sure which one is line 9. What sort of error do you get?

If I were writing the code I probably wouldn't copy row X in Sheet1 to row X in Sheet2: I'd have a counter and copy the first instance to row 1, the second to row 2, and so on, but that's just me. (I'd rather have Sheet2 always organized the same way: why continue to make my life difficult?)

I'm in Paris right now and it's quite late, so I won't be able to look at it much longer today, but I'll pop in tomorrow morning to see what's happening.
Reply
Old Dec 30, 2010 | 10:26 AM
  #5  
s4play's Avatar
Thread Starter
Registered User
Member (Premium)
 
Joined: Mar 2004
Posts: 9,447
Likes: 16
From: West Coast baby
Default

How was Paris? thanks for the suggestions and help....

Sorry for delayed reply, we tried to pull the data based on highlighted strings:


Sub ExtractData()
Dim u As Long
Dim i As Long
Dim j As Long
Dim Rng As Range
Dim m As Long
Dim k As Long
Dim a As Long
Dim b As Long

u = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
Set Rng = Range("B1:B" & u) 'ActiveSheet.UsedRange.SpecialCells(xlCellTypeCons tants)
Sheets("Sheet1").Cells.Clear
k = 1
For i = 1 To u
a = 0
b = 0
If Cells(i, 2).Font.ColorIndex <> -4105 Then
a = i
For j = i + 1 To u
If Cells(j, 2).Font.ColorIndex <> -4105 Then
b = j
Exit For
End If
Next j
If b = 0 Then
b = u
End If
Range("A" & i & ":G" & i).Copy Sheets("Sheet1").Cells(k, 1)
k = k + 1
For m = a + 1 To b
If Cells(m, 2).Interior.ColorIndex <> -4105 Then
Range("A" & m & ":G" & m).Copy Sheets("Sheet1").Cells(k, 1)
k = k + 1
End If
Next m
i = b - 1

End If
Next i


Sheets("Sheet1").Range("AA:AB").ClearContents
MsgBox "Done! " & Chr(10) & k - 1 & " rows have been extracted and copied to Sheet1"
End Sub
Reply
Old Dec 30, 2010 | 02:07 PM
  #6  
magician's Avatar
Registered User
 
Joined: Jul 2001
Posts: 6,592
Likes: 0
From: Yorba Linda, CA
Default

Paris was cold, snowy, filled with Christmas lights: pretty nice.

It would have been nicer if my wife had been there with me.

I met a couple of magicians for dinner Saturday night (the 11th): one from Paris, one from Newcastle. That was fun.
Reply
Old Dec 31, 2010 | 08:11 AM
  #7  
i_heart_my_DB8's Avatar
Registered User
 
Joined: Apr 2005
Posts: 8,586
Likes: 0
From: Scatterbrainia
Default

[QUOTE=magician,Dec 30 2010, 04:07 PM]Paris was cold, snowy, filled with Christmas lights: pretty nice.

It would have been nicer if my wife had been there with me.

I met a couple of magicians for dinner Saturday night (the 11th): one from Paris, one from Newcastle.
Reply
Old Dec 31, 2010 | 09:11 AM
  #8  
magician's Avatar
Registered User
 
Joined: Jul 2001
Posts: 6,592
Likes: 0
From: Yorba Linda, CA
Default

Originally Posted by i_heart_my_DB8,Dec 31 2010, 09:11 AM
I CAUGHT HIM, WHAT DO I WIN?!?!?

Fixed.

Reply
Old Dec 31, 2010 | 06:41 PM
  #9  
i_heart_my_DB8's Avatar
Registered User
 
Joined: Apr 2005
Posts: 8,586
Likes: 0
From: Scatterbrainia
Default

Originally Posted by magician,Dec 31 2010, 11:11 AM
Fixed.

Yeah yeah... just be glad only 5 people in total have viewed this thread. You've got a reputation to uphold!

Reply
Old Jan 5, 2011 | 10:23 AM
  #10  
Kyushin's Avatar
Banned
 
Joined: Dec 2003
Posts: 7,662
Likes: 1
From: Long Beach, CA
Default

ohh
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
AlX Boi
Off-topic Talk
3
Jan 20, 2011 05:45 AM
DangerZone9K
Off-topic Talk
20
Dec 2, 2009 12:30 PM
Austblue
Off-topic Talk
3
Feb 14, 2005 04:27 AM
X Factor
Off-topic Talk
7
Oct 19, 2003 11:06 AM




All times are GMT -8. The time now is 10:09 PM.