Results 1 to 10 of 75

Thread: vba Copy Paste Conditional to put remark 1 2 3 .. etc

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0

    Macro Correction

    Moderator Notice: This and the next 4 posts were posted here in the wrong place. I have left the copies here since the linked post has been referrenced already. I have also copied the posts to the correct place:
    https://excelfox.com/forum/showthrea...ll=1#post14580





    Code:
    Sub STEP6()
        Dim Ws1 As Worksheet, Ws2 As Worksheet
        Dim Wb1 As Workbook, Wb2 As Workbook
        Dim r2&, lr&, i&
        
        Set Wb1 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1.xls")
        Set Ws1 = Wb1.Worksheets.Item(1)
        Set Wb2 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\Files\AlertCodes.xlsx")
        Set Ws2 = Wb2.Worksheets.Item(4)
        With Ws1
            lr = .Cells(.Rows.Count, "I").End(xlUp).Row
            For i = 2 To lr
                ' Reset r2
                r2 = 0
                ' Avoid error messages
                On Error Resume Next
                ' Try to get r2
                r2 = WorksheetFunction.Match(.Cells(i, "I"), Ws2.[B:B], 0)
                ' Restore error handling
                On Error GoTo 0
                ' Only set column K if r2 is valid
                If r2 > 0 Then
                    If Ws2.Cells(r2, "D") = ">" Then
                        .Cells(i, "K").Value = .Cells(i, "D").Value - 0.01 * .Cells(i, "D").Value
                    Else
                        .Cells(i, "K").Value = .Cells(i, "D").Value + 0.01 * .Cells(i, "D").Value
                    End If
                End If
            Next i
        End With
        Wb1.Save
        Wb1.Close
        Wb2.Close
        
    End Sub

    This codes calculate 1% of column of column D of 1.xls
    but what i wanted is instead of column D, it should calculate the data 1% with column E of AlertCodes.xlsx & add the calculated result with Column E of AlertCodes.xlsx and paste the result to same place where the current macro is putting,rest everything will be same

    Kindly note AlertCodes.xlsx doesn't have headers so keep a eye on the same

    Thnx for the Help

    https://eileenslounge.com/viewtopic....271443#p271443
    https://chandoo.org/forum/threads/vb...rection.44637/
    Last edited by DocAElstein; 07-14-2020 at 01:10 PM.

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Moderator Notice: This post was posted here in the wrong place. I have left the copy here since the linked post has been referrenced already. I have also copied this and the related reply posts to the correct place, starting here:
    https://excelfox.com/forum/showthrea...ll=1#post14580





    You almost had it right here: https://eileenslounge.com/viewtopic....6a1745#p271427
    You were close to correct.

    I try to explain again what Hans tried to explain ( I think you almost understood ):
    __With Ws1
    ………
    ……….
    …………………
    . Cells(i, "E") ……… .Cells(i, "E")
    __End With

    is almost =
    .....Ws1.Cells(i, "E") …… Ws1.Cells(i, "E")

    You want Ws2 ( for Alertcodes.xlsx ) , not Ws1

    Maybe like
    Code:
               Wtih Ws1
                    If Ws2.Cells(r2, "D") = ">" Then 'calculate the data 1% with column E of AlertCodes.xlsx & add the calculated result with Column E of AlertCodes.xlsx
                        .Cells(i, "K").Value = Ws2.Cells(i, "E").Value - 0.01 * Ws2.Cells(i, "E").Value
                    Else
                        .Cells(i, "K").Value = Ws2.Cells(i, "E").Value + 0.01 * Ws2.Cells(i, "E").Value
                    End If
               End With
    This will also be the same
    Code:
                    If Ws2.Cells(r2, "D") = ">" Then 'calculate the data 1% with column E of AlertCodes.xlsx & add the calculated result with Column E of AlertCodes.xlsx
                        Ws1.Cells(i, "K").Value = Ws2.Cells(i, "E").Value - 0.01 * Ws2.Cells(i, "E").Value
                    Else
                        Ws1.Cells(i, "K").Value = Ws2.Cells(i, "E").Value + 0.01 * Ws2.Cells(i, "E").Value
                    End If
    If this does not work, then you must explain again exactly what you want the macro to do.

    Ws2.Cells(i, "E").Value - 0.01 * Ws2.Cells(i, "E").Value
    and
    Ws2.Cells(i, "E").Value + 0.01 * Ws2.Cells(i, "E").Value
    is 'calculate the data 1% with column E of AlertCodes.xlsx & add the calculated result with Column E of AlertCodes.xlsx




    This was wrong:
    Ws2.Cells(i, "E").Value - 0.01 *.Cells(i, "E").Value
    and
    Ws2.Cells(i, "E").Value + 0.01 * .Cells(i, "E").Value
    It is 'calculate the data 1% with column E of AlertCodes.xlsx & add the calculated result with Column E of 1.xls
    It is the same as
    Ws2.Cells(i, "E").Value - 0.01 *Ws1.Cells(i, "E").Value
    and
    Ws2.Cells(i, "E").Value + 0.01 * Ws1.Cells(i, "E").Value



    ( I never use
    __ With
    __ End With

    because it confuses me.
    But lots of other people, like Hans do use it. Everyone writes codes a bit differently, because all VBA codes can be written in many ways. )





    Also
    Have you seen this ? : https://excelfox.com/forum/showthrea...4565#post14565
    Your macro , Sub STEP6() from here
    https://excelfox.com/forum/showthrea...ll=1#post14562
    and here
    https://eileenslounge.com/viewtopic....271385#p271385
    has the wrong Lr2
    Last edited by DocAElstein; 07-14-2020 at 01:12 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    KILL A MODERATOR!!

  3. #3
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Moderator Notice: This post was posted here in the wrong place. I have left the copy here since the linked post has been referrenced already. I have also copied this and the related reply posts to the correct place, starting here:
    https://excelfox.com/forum/showthrea...ll=1#post14580





    Thnx Alot Soc Sir for helping me in solving this problem Sir
    Problem Solved
    Have a Great Day
    Last edited by DocAElstein; 07-14-2020 at 01:12 PM.

Similar Threads

  1. Replies: 26
    Last Post: 09-26-2020, 05:56 PM
  2. VBA -- Copy/Paste across sheets
    By Rasm in forum Excel Help
    Replies: 4
    Last Post: 09-21-2012, 02:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •