Avatar billede zulaff Nybegynder
11. marts 2009 - 22:32 Der er 1 kommentar og
1 løsning

FormulaR1C1 problem

Jeg er ved at lave en makro i excel. Jeg har lavet følgende lykke som skal udregne den maksimale af adskellige gennemsnit:

formulaString = "=MAKS("
    For i = 0 To timeStop - timeStart - 1
        formulaString = formulaString + "AVERAGE(R[" + Trim(Str(i - 2)) + _
        "]C[-7]:R[" + Trim(Str(i + 3)) + "]C[-7])"
        i = i + 4
        If i + 1 < timeStop - timeStart Then formulaString = formulaString + ","
    Next i
    Range("H" + Trim(Str(timeStart + 2))).Select
    ActiveCell.FormulaR1C1 = formulaString + ")"

Først fik jeg en fejl i nederste linje af koden, så jeg kiggede koden igennem og fandt ud af at sidste gang den igennem lykken, bliver der ikke sat nogen tekst på enden af formulaString. Hvorfor gør der ikke det? Den går ind i lykken og den kører koden, men der kommer ikke noget på strengen. Lykken kører maksimalt igennem 30 gange og kørte igennem 6 gange da jeg testede det.

Er der nogen der kan hjælpe?
Avatar billede zulaff Nybegynder
11. marts 2009 - 22:33 #1
Hele kildekoden ser således ud:

Sub EMGandEvents3()
'Base Path of Source Files
basePath = SelectFolder("Select Folder Containing Source Files", "") + "\"

'File Name Without Initials
baseFileName = "-Interface001.xls"

'Create Folder for New Files
On Error Resume Next
MkDir (basePath + "EMGandEventsData")
On Error GoTo 0

'Open EMG and Events
eventsWindow = "EMG and Events 4.xlsx"
Workbooks.Open Filename:=basePath + eventsWindow

'Loop Until All Time Periods are Tranferred
initials = Range("A2").Value
initialsIndex = 2
fileIsOpen = False
Do While initials <> ""
   
    'Open Interface File
    If Not fileIsOpen Then
        interfaceFile = basePath + initials + baseFileName
        Workbooks.Open Filename:=interfaceFile
        interfaceWindow = initials + baseFileName
        fileIsOpen = True
    End If

    'Get Start and Stop Times
    Windows(eventsWindow).Activate
    timeStart = Range("C" + Trim(Str(initialsIndex))).Value
    timeStop = Range("D" + Trim(Str(initialsIndex))).Value

    'Prepare Event File
    For i = initialsIndex + 1 To initialsIndex + 4
        Rows(Trim(Str(i)) + ":" + Trim(Str(i))).Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Next i
    Rows(Trim(Str(initialsIndex)) + ":" + Trim(Str(initialsIndex))).Select
    Selection.Copy
    Rows(Trim(Str(initialsIndex + 1)) + ":" + Trim(Str(initialsIndex + 7))).Select
    ActiveSheet.Paste
    Range("E" + Trim(Str(initialsIndex + 1))).Select
    ActiveCell = "Max"
    Range("E" + Trim(Str(initialsIndex + 2))).Select
    ActiveCell = "HighestAverage"
   
    'Calculate  EMG Activity for Zygomaticus
    Windows(interfaceWindow).Activate
    startTimeString = Trim(Str(timeStart))
   
    'Average
    Range("H" + startTimeString).Select
    ActiveCell.FormulaR1C1 = "=AVERAGE(R[0]C[-7]:R[" + Trim(Str(timeStop - timeStart)) + "]C[-7])"
   
    'Max
    Range("H" + Trim(Str(timeStart + 1))).Select
    ActiveCell.FormulaR1C1 = "=MAX(R[-1]C[-7]:R[" + Trim(Str(timeStop - timeStart - 1)) + "]C[-7])"
       
    'HighestAverage
    formulaString = "=MAKS("
    For i = 0 To timeStop - timeStart - 1
        formulaString = formulaString + "AVERAGE(R[" + Trim(Str(i - 2)) + _
        "]C[-7]:R[" + Trim(Str(i + 3)) + "]C[-7])"
        i = i + 4
        If i + 1 < timeStop - timeStart Then formulaString = formulaString + ","
    Next i
    Range("H" + Trim(Str(timeStart + 2))).Select
    Range("H" + Trim(Str(timeStart + 2))) = formulaString
    ActiveCell.FormulaR1C1 = formulaString + ")"
    'Median
    'Range("H" + Trim(Str(timeStart + 3))).Select
    'ActiveCell.FormulaR1C1 = "=MEDIAN(R[-3]C[-7]:R[" + Trim(Str(timeStop - timeStart - 4)) + "]C[-7])"
   
    'Calculate EMG Activity Before Event
    'Average
        'ActiveCell.FormulaR1C1 = "=AVERAGE(R[-8]C[-7]:R[-5]C[-7])"
    Range("H" + Trim(Str(timeStart + 2))).Select
    ActiveCell.FormulaR1C1 = "=AVERAGE(R[" + Trim(Str(timeStart - timeStop - 12)) + "]C[-7]:R[-12]C[-7])"
   
    'Max
    Range("H" + Trim(Str(timeStart + 3))).Select
    ActiveCell.FormulaR1C1 = "=MAX(R[" + Trim(Str(timeStart - timeStop - 13)) + "]C[-7]:R[-13]C[-7])"
   
    'Min
    'Range("H" + Trim(Str(timeStart + 6))).Select
    'ActiveCell.FormulaR1C1 = "=MIN(R[" + Trim(Str(timeStart - timeStop - 16)) + "]C[-7]:R[-16]C[-7])"
   
    'Median
    'Range("H" + Trim(Str(timeStart + 7))).Select
    'ActiveCell.FormulaR1C1 = "=MEDIAN(R[" + Trim(Str(timeStart - timeStop - 17)) + "]C[-7]:R[-17]C[-7])"
   
    'Calculate EMG Activity for the Rest
    Range("H" + startTimeString + ":H" + Trim(Str(timeStart + 7))).Select
    Selection.Copy
    Range("H" + startTimeString + ":M" + Trim(Str(timeStart + 7))).Select
    ActiveSheet.Paste
   
    'Copy Problem Events to Event File
    Range("H" + startTimeString + ":M" + Trim(Str(timeStart + 1))).Select
    Selection.Copy
    Windows(eventsWindow).Activate
    Range("F" + Trim(Str(initialsIndex)) + ":K" + Trim(Str(initialsIndex + 1))).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    'Copy Before Events to Event File
    Windows(interfaceWindow).Activate
    Range("H" + Trim(Str(timeStart + 2)) + ":M" + Trim(Str(timeStart + 3))).Select
    Selection.Copy
    Windows(eventsWindow).Activate
    Range("L" + Trim(Str(initialsIndex)) + ":Q" + Trim(Str(initialsIndex + 1))).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
   
    'Prepare Next Loop
    initialsIndex = initialsIndex + 8
    newInitials = Range("A" + Trim(Str(initialsIndex))).Value
    'MsgBox (newInitials)
    If newInitials <> initials Then
        initials = newInitials
        Windows(interfaceWindow).Close False
        fileIsOpen = False
    End If
   
Loop
Windows(eventsWindow).Activate
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=basePath + "EMGandEventsData\EMG and Events 4.xlsx" _
        , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Application.DisplayAlerts = True
End Sub

'Both arguements are optional. The first is the dialog caption and
'the second is is to specify the top-most visible folder in the
'hierarchy. The default is "My Computer."

Function SelectFolder(Optional Title As String, Optional TopFolder _
                        As String) As String
    Dim objShell As New Shell32.Shell
    Dim objFolder As Shell32.Folder

'If you use 16384 instead of 1 on the next line,
'files are also displayed
    Set objFolder = objShell.BrowseForFolder _
                            (0, Title, 1, TopFolder)
    If Not objFolder Is Nothing Then
        SelectFolder = objFolder.Items.Item.Path
    End If
End Function
Avatar billede zulaff Nybegynder
02. oktober 2009 - 13:04 #2
Der er ikke kommet nogle kommentarer, så jeg lukker hermed denne tråd.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester