Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / December 2007

Tip: Looking for answers? Try searching our database.

Need some VBA help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ST Jude - 06 Dec 2007 16:47 GMT
I need some coding for the following:
I have a workbook titled "ALL Trend Log" that does trend calculations for
me. I manually enter the data to calculate from another workbook and want to
make this automated.

Here's how the process works: people in my department make a new workbook
from a template and enter their data into it, using the exact same cells
every time. They then save these workbooks into a Windows folder titled
"E2APBX" in the format of Testname,run#,initials,date so it looks like this
"E2APBX1897DV6-13-07". The data I want from this sheet is located in cells
B1,D1,F4,D4,E4,K4,I4,J4 and K1. I then take this data and put it into another
workbook titled "ALL Trend Log" (found in Windows folder "Quant. Assay Trend
Logs")  under the worksheet titled "E2APBX" into the respective cells A6
through I6. I do this every couple days by finding the new workbooks and
entering the data into the "ALL Trend Log." How can I get this to perform
automatically?

Thanks in advance to everyone for their help and insight. I have come to
really like this board and its professionalism.
Barrett
ilia - 06 Dec 2007 17:41 GMT
Hi Barrett,

I'd like to try this, but some questions first.

Is run# always four digits?

What worksheet in the E2APBX1897DV6-13-07 workbook contains the data
you want (or is there only one sheet)?  Is the date format always m-d-
yy?

How do you determine, when doing this manually, which workbooks are
new?  Does one of the cells "B1,D1,F4,D4,E4,K4,I4,J4 and K1" contain
the run number?

> I need some coding for the following:
> I have a workbook titled "ALL Trend Log" that does trend calculations for
[quoted text clipped - 16 lines]
> really like this board and its professionalism.
> Barrett
ST Jude - 06 Dec 2007 18:26 GMT
Hi ilia,
Thanks for responding so quickly!
> Is run# always four digits?

Run# changes as the number of runs assigned increases. At this point in the
year we're usually in the 1,000 range. Whichever is easier to use is the one
to go with. I prefer 4 digits personally, but whichever is easier to program
is the one to use.

> What worksheet in the E2APBX1897DV6-13-07 workbook contains the data
> you want (or is there only one sheet)?  

There is only one sheet and its named "Sheet 1." E2APBX1897DV6-13-07 was
what I used as a name reference so you could see how its broken down. The
workbook's are actually called (by us) TPQ forms.

>Is the date format always m-d-yy?

Yes it is.

> How do you determine, when doing this manually, which workbooks are
> new?  

Basically I look at the workbook title. The (date being at the end,
E2APBX1897DV[B]6-13-07 [/B]) is what I look for. But this date is also found
in cell D1 of the worksheet.

>Does one of the cells "B1,D1,F4,D4,E4,K4,I4,J4 and K1" contain
> the run number?

Yes. The cells signify the following in this format (TPQ form
cell#)=Signifance=(Trend Log cell#):
B1=Run#=A6, D1=Date=B6, F4=RSQ=C6, D4=Slope=D6, E4=Y-Int=E6, K4=RSQ=F6,
I4=Slope=G6, J4=Y-Int=H6, and K1=Instrument name=I6. Now you'll notice that I
have two sets of R squared, slope and Y-intercept values. The first set is
for our chromosome breakpoint test, the other set is for the GAP DH test.

Just so you know, what we do here at the childrens hospital is test our
patients genes for errors and translocations that result in cancer.  We track
EVERYTHING and keep detailed logs on our instruments and tests to make sure
these kids get the best possible treatment. For my own part I greatly
appreciate the time and effort you're putting into this, I know my fellow
employees (who use these sheets daily) will appreciate this, but the ones who
benefit the most are the children. Thank you for all your help.
Sincerely,
Barrett  



> Hi Barrett,
>
[quoted text clipped - 30 lines]
> > really like this board and its professionalism.
> > Barrett
ilia - 06 Dec 2007 23:08 GMT
Hi Barrett,

I pretty much have the code that does what you want, but I'm still
unclear about a few things.

One: suppose there is more than one E2APBX file in the E2APBX
directory.  Does that mean that you want to replace whatever the
content of A6:I6 with the latest file, or do you want to continue
adding rows (A7:B7, A8:B8, etc)?  Right now i have code looping
through any file matching the criteria, but if you're just looking for
one file then it doesn't make a difference.  I'm sure I'm
demonstrating a lack of knowledge about what you're actually trying to
accomplish, but hey, I'm an accountant not a scientist. =]

Two: what is the basis for the date comparison?  I have a function
that will extract the date portion of the file name, but what am i
comparing it to?  Am i looking at the last date you have in B6?  Or
the most recent date in column B?  Today's date?

Let me know and I'll post the finished code some time tomorrow.

> Hi ilia,
> Thanks for responding so quickly!
[quoted text clipped - 80 lines]
>
> - Show quoted text -
ST Jude - 07 Dec 2007 12:10 GMT
Thanks again for doing this. I REALLY appreciate it!
One: There will be many more E2APBX files in that folder. Their names are
how we distinguish them. The "E2APBX" part of the name will always be the
same, but the run number, initials and date will always change. I will want
to keep adding rows (A7:B7, A8:B8, etc) up to 15 rows. Then I have the trend
log calculations and I believe they take up 3 rows so the grouping will need
to start again with the E2APBX data at A24 to I24, A25 to I25, et cetera. I'm
at home currently with a sick toddler so I'm going to have my supervisor send
me a copy of the worksheet so I can give you exact cell locations for the
trend log calculations. Accountant... scientist... we're number people any
way you slice it ;-P
Two: The date comparison is just to tell me where I left off last time I
updated the Trend Log workbook. I don't do It every day so when I et back
into it I just look at the trend log workbook and see what the last date I
entered was. Then I go back to the folder with all the E2APBX workbooks, find
the next date after I left off and bring it up to current date.

> Hi Barrett,
>
[quoted text clipped - 101 lines]
> >
> > - Show quoted text -
ilia - 07 Dec 2007 19:36 GMT
OK,

Here's the first version.  Please try this on a copy of your
workbooks, if you have a chance.  Paste the into a standard module
(does not have to be ALL Trend Log), and adjust the paths
accordingly.  I'll come up with some test data that meet your criteria
and post back a final version with any corrections and error handling
code.

-Ilia

'begin code
Option Explicit

Public Sub lookForFiles()
 Const strRunsPath As String = "C:\E2APBBX\"
 Const strTrendPath As String = "C:\Quant. Assay Trend Logs\"
 Const strTrendFileName As String = "ALL Trend Log.xls"
 Const strWshName As String = "E2APBX"
 Const strDataRange As String = "B1,D1,F4,D4,E4,K4,I4,J4,K1"

 Dim currFileName As String

 Dim wshTemp As Excel.Worksheet
 Dim wkbTrend As Excel.Workbook
 Dim wshTrend As Excel.Worksheet
 Dim iFileCount As Long

 Dim wkbData As Excel.Workbook
 Dim wshData As Excel.Worksheet
 Dim rngData As Excel.Range

 Dim iCellCount As Long

 Dim firstNewRow As Long
 Dim iCurrFile As Long

 Dim lastDate As Date

 Application.ScreenUpdating = False

 If ThisWorkbook.Name = strTrendFileName Then
   Set wkbTrend = ThisWorkbook
 Else
   Set wkbTrend = _
     Application.Workbooks.Open(strTrendPath & _
                                 strTrendFileName)
 End If

 ' add a temporary sheet to keep track of new data files
 Set wshTrend = wkbTrend.Worksheets(strWshName)
 Set wshTemp = wkbTrend.Worksheets.Add
 wshTemp.Visible = xlSheetVeryHidden

 ' get the latest date of last run entered
 lastDate = getLastDate(wshTrend)

 ' look for new files
 currFileName = Dir(strRunsPath)
 With wshTemp
   Do While currFileName <> ""
     If (fileNameMatches(currFileName) And _
         extractRunDate(currFileName) > lastDate) Then
       iFileCount = iFileCount + 1
       .Cells(iFileCount, 1).Value = currFileName
     End If
     currFileName = Dir()
   Loop

   firstNewRow = getFirstNewRow(wshTrend)
   For iCurrFile = 1 To iFileCount
     ' open each new file
     Set wkbData = _
         Application.Workbooks.Open(strRunsPath & _
                                   .Cells(iCurrFile, 1))
     Set wshData = wkbData.Worksheets("Sheet 1")

     ' process all cells in range
     iCellCount = 0
     For Each rngData In wshData.Range(strDataRange)
       iCellCount = iCellCount + 1
       wshTrend.Cells(firstNewRow, _
                   iCellCount).Value = rngData.Value
       firstNewRow = firstNewRow + 1
     Next rngData
     wkbData.Close (False)
   Next iCurrFile

   Application.DisplayAlerts = False
   .Delete
   Application.DisplayAlerts = True
 End With

 Application.ScreenUpdating = True
End Sub

Private Function getFirstNewRow(ByRef wsh As _
                         Excel.Worksheet) As Long
 Dim iCount As Long
 Dim firstNewRow As Long

 firstNewRow = wsh.UsedRange.Rows.Count + 1

 ' leave 3 rows blank every 15 rows
 For iCount = 21 To firstNewRow Step 15
   If firstNewRow = iCount Then
     firstNewRow = firstNewRow + 3
   ElseIf (firstNewRow = iCount + 1) Then
     firstNewRow = firstNewRow + 2
   ElseIf (firstNewRow = iCount + 2) Then
     firstNewRow = firstNewRow + 1
   End If
 Next iCount

 getFirstNewRow = firstNewRow
End Function

Private Function getLastDate(ByRef wsh As _
                       Excel.Worksheet) As Date
 ' using a separate function for this
 ' in case we need to use a cell-by-cell
 ' algorithm to find latest date, instead of Max
 getLastDate = _
     Application.WorksheetFunction.Max( _
                           wsh.Range("$B:$B"))
End Function

Private Function fileNameMatches(fileName As String) _
                                         As Boolean
 ' file name must start with E2APBX
 ' run # may be anywhere from 1 to 9999999
 ' initials may be 2 or 3 characters
 ' date must be m-d-yy format
 ' 01-01-07 is invalid because of leading zeroes
 ' file extension must be .xls, .xlsx, or .xlsm
 Const strPattern As String = _
   "^E2APBX" & _
   "[1-9][0-9]{0,6}" & _
   "[A-Z]{2,3}" & _
   "[1-9][012]{0,1}-[1-9][0-9]{0,1}-[0-9][0-9]" & _
   "[.]xls[xm]{0,1}$"

 'VBScript_RegExp_10.RegExp
 Dim objRegExp As Object

 'New VBScript_RegExp_10.RegExp
 Set objRegExp = CreateObject("vbscript.RegExp")

 With objRegExp
   .Pattern = strPattern
   .IgnoreCase = True
   fileNameMatches = .test(fileName)
 End With
End Function

Private Function extractRunDate(fileName As String) _
                                           As Date
 ' sample file name: "E2APBX1897DV6-13-07.xls"

 Dim iDateBegins As Integer
 Dim iDateEnds As Integer

 Dim iPointer As Integer

 iDateEnds = InStrRev(fileName, ".") - 1
 iDateBegins = InStr(fileName, "-") - 2
 If Not isDigit(Mid(fileName, iDateBegins, 1)) Then
   iDateBegins = iDateBegins + 1
 End If

 extractRunDate = Mid(fileName, iDateBegins, _
                             iDateEnds - iDateBegins)
End Function

Private Function isDigit(dig As String) As Boolean
 isDigit = (Asc(dig) >= Asc(0) And Asc(dig) <= Asc(9))
End Function
' end code

> Thanks again for doing this. I REALLY appreciate it!
> One: There will be many more E2APBX files in that folder. Their names are
[quoted text clipped - 120 lines]
>
> - Show quoted text -
drose - 12 Dec 2007 13:28 GMT
Hi Ilia,
I work with Barrett and have been trying the code.  It is working but needs
some added code.  When it skips 3 rows that is because in those three rose we
have formulas built in that is calculating the mean, standard deviation and
%CV of the data.  When i run the macro it starts putting the data below these
formulas instead of the first 15 rows above them.,

Thanks for all the help,
drose

> OK,
>
[quoted text clipped - 287 lines]
> > > > > > B1,D1,F4,D4,E4,K4,I4,J4 and K1. I then take this data and put it into another
> > > > > > workbook titled "ALL Trend Log" (found in Windows folder "Quant. Assay Trend
ilia - 12 Dec 2007 16:47 GMT
This is the part I don't quite understand.  What if there are more
files than will fit in the 15 rows of data?  I don't understand how to
pick among them which will be above the summary rows.  From what I
understood earlier, the data skips 3 rows every 15 rows and
continues.  This is what Barrett wrote:

>>I will want to keep adding rows (A7:B7, A8:B8, etc) up to 15 rows. Then I have the trend log calculations and I believe they take up 3 >>rows so the grouping will need to start again with the E2APBX data at A24 to I24, A25 to I25, et cetera.

Feel free to e-mail me regarding this.

> Hi Ilia,
> I work with Barrett and have been trying the code.  It is working but needs
[quoted text clipped - 266 lines]
>
> read more >>
drose - 12 Dec 2007 17:39 GMT
First I appolagize that we are both very inexperienced with VBA but I will
try to explain.
Each file fills a row, so after 15 files we would be ready to calculate the
standard deviation, mean and %CV of the 15 files, we then repeat for the next
15 files to see if they have changed and so on.  A continuous monitor to
adjust for 2 standard deviations.  Please let me know if this still doesn't
make sense... it is hard to explain.
drose

> This is the part I don't quite understand.  What if there are more
> files than will fit in the 15 rows of data?  I don't understand how to
[quoted text clipped - 276 lines]
> >
> > read more >>
ilia - 12 Dec 2007 19:15 GMT
I understand that part, what I'm not understanding is what the
spreadsheet needs to do once it's "ready to calculate".  Do you want
to hault everything and wait for user input?  Maybe a command button
named "View Next 15" or something?  Message box saying "click OK to
continue?"

Or, do you want to save the average, STDEV, %CV, etc for each set of
15, and then compare?  I can make it create or update a separate
summary sheet, we just need to agree on the format.

> First I appolagize that we are both very inexperienced with VBA but I will
> try to explain.
[quoted text clipped - 243 lines]
>
> read more >>
drose - 12 Dec 2007 22:24 GMT
I gotcha,
We would like to save the average, STDEV, %CV, etc for each set of
> 15, and then compare.  we are currently just saving the three lines where they are and then adding the formula after the next 15 and so on.  It takes several months to get 15 so we were just going down the spreadsheet.  I think it would be better if it created a separte summar sheet with all the average, STDEV, %CV for the groups of 15.  (If the STDEV start to vary we will update or template and the assays will only be accepted if they are within 3 STDEV.  We can do this manually)
Thanks, ilia
drose

> I understand that part, what I'm not understanding is what the
> spreadsheet needs to do once it's "ready to calculate".  Do you want
[quoted text clipped - 253 lines]
> >
> > read more >>
Dan R. - 06 Dec 2007 18:07 GMT
Barrett,

Put this in a module in your "All Trend Log" file and run it.

Sub test()
 Dim src As Variant, fName As String, wb As Workbook

 src = Array("B1","D1","F4","D4","E4","K4","I4","J4","K1")
 fName = Application.GetOpenFilename()

 If fName <> "False" Then
   Set wb = Workbooks.Open(fName)
 Else
   Exit Sub
 End If

 For i = 0 To 8
   wb.Sheets(1).Range(src(i)).Copy _
     ThisWorkbook.Sheets("E2APBX").Cells(6, i + 1)
 Next

 wb.Close savechanges:=False
End Sub

--
Dan

> I need some coding for the following:
> I have a workbook titled "ALL Trend Log" that does trend calculations for
[quoted text clipped - 16 lines]
> really like this board and its professionalism.
> Barrett
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.