Find function is only finding part of value in VBA

时间:2019-03-06 11:31:18

标签: excel vba

I have written a macro in VBA which copies values from one worksheet to another using the find function.

My workbook calculates certain variables depending on the span length of a bridge. The span length changes iteratively and I have named the variable "SpanLength". Each time the span length changes the macro copies the maximum values and pastes them to worksheet "MAXIMUM" (shown in the image attached). In MAXIMUM I have named all the top row of span lengths (1, 1.2, 1,4 etc.) "FindSpanLength". The macro then finds the appropriate span length here and paste the maximum values next to it. This macro works except for with span lengths 2, 4, 6 and 8 where instead of finding these span lengths in MAXIMUM, it finds span lengths 1,2, 1.4, 1.6 and 1.8 and copies the results there.

x

I have tried using Lookat:=xlWhole but this exacerbates the problem. Does anyone have any other solutions?

xlWhole

 Private Sub Max()

    ' Chooses correct column: Find the span length column and name this outputcolumn
    Dim spanlengthcell As Range, outputcolumn As Integer
    Set spanlengthcell = Range("FindSpanLength").Find(Range("SpanLength").Value, LookIn:=xlValues, Lookat:=xlPart)
    If Not spanlengthcell Is Nothing Then
     outputcolumn = spanlengthcell.Column
    End If

' Chooses correct row: If the cell to the left of spanlengthcell is empty then this is the first row of output otherwise find next empty cell down
If MAXIMUM.Cells(4, outputcolumn + 1).Value = "" Then
    outputrow = 4
ElseIf MAXIMUM.Cells(5, outputcolumn + 1).Value = "" Then
    outputrow = 5
Else
    outputrow = MAXIMUM.Cells(4, outputcolumn + 1).End(xlDown).Row + 1
End If

' Copy starting point, max bending moment and max shear from calculate to output
MAXIMUM.Cells(outputrow, outputcolumn + 1).Value = Sheets("Calculate").Range("StartPoint").Value
MAXIMUM.Cells(outputrow, outputcolumn + 2).Value = Sheets("Calculate").Range("[enter image description here][1]MaxBM").Value
MAXIMUM.Cells(outputrow, outputcolumn + 3).Value = Sheets("Calculate").Range("MaxSF").Value

End Sub

0 个答案:

没有答案