excel vba after calculation comma is wron

Posted on 2012-09-17
Last Modified: 2012-10-03
Dear experts,

I made a little bit of vba code to import some data from another excel file. In my code i divide 1 plant into two plants,
Function DividePowerplants()

Dim GSRN As String
Dim GSRN1 As String
Dim GSRN2 As String
Dim Factor1 As Double
Dim Factor2 As Double
Dim Name1 As String
Dim Name2 As String
Dim uRng As Range
Dim lCols As Long

    x = Workbooks(WorkbookName).Sheets("DividedPD's").Cells(Rows.Count, "B").End(xlUp).Row

For i = 2 To x

With Workbooks(WorkbookName).Sheets("DividedPD's")

    GSRN = .Range("B" & i).Value
    Name1 = .Range("C" & i).Value
    GSRN1 = .Range("D" & i).Value
    Factor1 = .Range("E" & i).Value / (.Range("E" & i).Value + .Range("H" & i).Value)
    Factor2 = 1 - Factor1
    GSRN2 = .Range("G" & i).Value
    Name2 = .Range("F" & i).Value
End With

    With Workbooks(WorkbookName).Sheets("Expected Production")

        Set rangeFind = .Range("3:3").Find(What:=GSRN, After:=Range("A3"), SearchOrder:=xlByColumns)
    If Not rangeFind Is Nothing Then

            lCols = rangeFind.Column + 1
            .Columns(lCols).EntireColumn.Insert Shift:=xlToLeft

            .Cells(1, lCols) = .Cells(1, rangeFind.Column).Value
            .Cells(2, lCols) = .Cells(2, rangeFind.Column).Value
            .Cells(3, lCols) = GSRN1
            .Cells(4, lCols) = Name1

            For j = 5 To 64
                     .Cells(j, lCols).Value = .Cells(j, rangeFind.Column).Value * Factor1
            Next j
            .Cells(3, rangeFind.Column).Value = GSRN2
            .Cells(4, rangeFind.Column).Value = Name2

            For k = 5 To 64
                .Cells(k, rangeFind.Column).Value = .Cells(k, rangeFind.Column).Value * Factor2

            Next k
End If
End With

Next i

Open in new window

The code works fine but the results in my sheet aren't correct. Because when i have a powerplant A and with the code i divide it into Plant B and Plant C
Plant A has the results: 2177 and plant B has 22
                                      1113                          11
Can anyone tell me how this is possible?

Question by:MarkVrenken
    LVL 11

    Accepted Solution

    can you please attach some sample workbooks that produce the errors.
    LVL 1

    Author Comment

    never mind i'm sorry for the late response. Haven't been at work for some time. I fixed it. After trying to make an example file i discovered i had some numbers stored as text what produced the mistake.
    thanks for making me do it;)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    Excel 2010 - Select category per Max values 5 17
    Excel to create SQL insert 7 28
    VBA to sum a column 13 30
    Excel Averageifs 2 14
    What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
    Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
    This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
    This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now