+ Reply to Thread
Results 1 to 7 of 7

Amend macro in excel

  1. #1
    Registered User
    Join Date
    02-22-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    3

    Amend macro in excel

    Hi,

    I am very new to macros and have been assigned an issue wherein I need to amend the existing macro as per the new requirement as shown below.

    Whenever the VGM Wgt Flg is ‘Y’ the value in column VGM Wgt Kgs should be taken instead of the column Gross Weight, while when the VGM Wgt Flg is ‘N’ the calculation should take value on column Gross Weight. (CBFData.xls)

    There are 2 excel spreadsheets involved which I have attached as well.

    I have amended the code as shown below but it comes back with an error on the very first row- 'Variable not defined.'
    --------------------------------------------------------------------------------------------------------------------
    If WBin.Worksheets(1).Range("O" & inRow) = n Then
    If .Range("G" & inRow) = 0 Then
    If .Range("E" & inRow) = 20 Then
    lngWeight = (WBin.Worksheets(1).Range("L" & inRow) + 2000)
    ElseIf .Range("E" & inRow) = 40 Then
    lngWeight = (WBin.Worksheets(1).Range("L" & inRow) + 4000)
    ElseIf .Range("E" & inRow) = 45 Then
    lngWeight = (WBin.Worksheets(1).Range("L" & inRow) + 4000)
    Else
    lngWeight = 0
    End If
    Else
    lngWeight = WBin.Worksheets(1).Range("G" & inRow)
    End If


    If WBin.Worksheets(1).Range("O" & inRow) = Y Then
    If .Range("G" & inRow) = 0 Then
    If .Range("E" & inRow) = 20 Then
    lngWeight = (WBin.Worksheets(1).Range("N" & inRow))
    ElseIf .Range("E" & inRow) = 40 Then
    lngWeight = (WBin.Worksheets(1).Range("N" & inRow))
    ElseIf .Range("E" & inRow) = 45 Then
    lngWeight = (WBin.Worksheets(1).Range("N" & inRow))
    Else
    lngWeight = 0
    End If
    Else
    lngWeight = WBin.Worksheets(1).Range("N" & inRow)
    End If
    ------------------------------------------------------------------------------------------------------------------

    Can someone please give some suggestions in this regard ? Thanks in advance.
    Attached Files Attached Files
    Last edited by lahsiv2004; 02-22-2017 at 11:57 AM.

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Amend macro in excel

    Hi lashiv,

    Two things....

    Please Login or Register  to view this content.
    are you sure it will never be a capital N instead of lowercase n? I assume it is a letter in the cell....

    n by itself is 0 (but also not accepted since it is not a declared variable) since without quotes VBA will think it is a variable. So to fix your code:

    Please Login or Register  to view this content.
    But if the cell could have n or N then:


    Please Login or Register  to view this content.
    That one will check if the uppercase value of the cell is "N" so both n and N will be a match.

    Same solution for Y, you need quotes around it.

    I would also ask that you check the forum rules, especially rule # 3 about code tags? Thanks.
    Last edited by Arkadi; 02-22-2017 at 01:04 PM.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Amend macro in excel

    A couple of things:

    If you are getting that error, more than likely somebody as set the "OPtion Explicit" option....Usually (that's where I set it) at the top of the code you'll see Option Explicit -- that means every variable MUST be dimensioned...see DIM Statements...

    Also, you have password protected your macro so I can't see everything that is going on. I assume you have set inRow to a value but not DIM'd it. that would be the first place to start...

    HTH......have fun
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  4. #4
    Registered User
    Join Date
    02-22-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    3

    Re: Amend macro in excel

    Hi Arkadi,

    Thanks very much for your solution. It helped a lot. However, I am now getting a different error - Loop without Do

    on the below code-

    If UCase(WBin.Worksheets(1).Range("O" & inRow).Value) = "N" Then 'V07 21/02/2017
    If .Range("G" & inRow) = 0 Then
    If .Range("E" & inRow) = 20 Then '
    lngWeight = (WBin.Worksheets(1).Range("L" & inRow) + 2000) '
    ElseIf .Range("E" & inRow) = 40 Then '
    lngWeight = (WBin.Worksheets(1).Range("L" & inRow) + 4000) '
    ElseIf .Range("E" & inRow) = 45 Then '
    lngWeight = (WBin.Worksheets(1).Range("L" & inRow) + 4000)
    Else '
    lngWeight = 0 '
    End If '
    Else '
    lngWeight = WBin.Worksheets(1).Range("G" & inRow)
    End If


    If UCase(WBin.Worksheets(1).Range("O" & inRow).Value) = "Y" Then 'V07 21/02/2017
    If .Range("G" & inRow) = 0 Then
    If .Range("E" & inRow) = 20 Then '
    lngWeight = (WBin.Worksheets(1).Range("N" & inRow)) '
    ElseIf .Range("E" & inRow) = 40 Then '
    lngWeight = (WBin.Worksheets(1).Range("N" & inRow)) '
    ElseIf .Range("E" & inRow) = 45 Then '
    lngWeight = (WBin.Worksheets(1).Range("N" & inRow))
    Else '
    lngWeight = 0 '
    End If '
    Else '
    lngWeight = WBin.Worksheets(1).Range("N" & inRow)
    End If




    If .Range("F" & inRow) = 96 Then
    If .Range("E" & inRow) = 40 Then
    CBF.POD(i).intWeight(3) = CBF.POD(i).intWeight(3) + lngWeight 'POD TotalWeight (E) 'V07 13/01/2004
    ElseIf .Range("E" & inRow) = 45 Then
    CBF.POD(i).intWeight(4) = CBF.POD(i).intWeight(4) + lngWeight 'POD TotalWeight (E) 'V07.25 27/08/2010
    End If
    ElseIf .Range("E" & inRow) = 20 Then
    CBF.POD(i).intWeight(1) = CBF.POD(i).intWeight(1) + lngWeight 'POD TotalWeight (C) 'V07
    ElseIf .Range("E" & inRow) = 40 Then
    CBF.POD(i).intWeight(2) = CBF.POD(i).intWeight(2) + lngWeight 'POD TotalWeight (D) 'V07
    End If

    inRow = inRow + 1
    Loop
    End With


    Quote Originally Posted by Arkadi View Post
    Hi lashiv,

    Two things....

    Please Login or Register  to view this content.
    are you sure it will never be a capital N instead of lowercase n? I assume it is a letter in the cell....

    n by itself is 0 (but also not accepted since it is not a declared variable) since without quotes VBA will think it is a variable. So to fix your code:

    Please Login or Register  to view this content.
    But if the cell could have n or N then:


    Please Login or Register  to view this content.
    That one will check if the uppercase value of the cell is "N" so both n and N will be a match.

    Same solution for Y, you need quotes around it.

    I would also ask that you check the forum rules, especially rule # 3 about code tags? Thanks.

  5. #5
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Amend macro in excel

    A blindingly obvious error - You have a LOOP statement without a corresponding DO statement. They always come in pairs.

    You could, perhaps, remove the LOOP statement but without knowing the logic, or other code you may not have posted, it's possible you need to add a DO statement.

    You have already been asked to correct the code tags...
    I would also ask that you check the forum rules, especially rule # 3 about code tags? Thanks.


    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

  6. #6
    Registered User
    Join Date
    02-22-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    3

    Re: Amend macro in excel

    Thanks a lot for your help.

    The Loop error has been rectified now, however, I get this error (screenshot attached) while compiling the code - Compile error in hidden module. The error commonly occurs when the code is incompatible with the version, platform or architecture of the application.

    Thanks.
    Attached Images Attached Images

  7. #7
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Amend macro in excel

    Since the code is password protected, there is not a lot we can see in your workbook. It is possible you have some 32-bit code and 64-bit office, or vice-versa.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro to analyze and amend Data
    By Tao_ in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-10-2012, 11:16 AM
  2. Amend my worksheets to one sheet macro
    By thedon_1 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-20-2011, 11:47 AM
  3. Problem with Amend Macro
    By just_some_guy in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-03-2010, 02:07 AM
  4. Amend macro to run through all sheets except two
    By rajsa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2009, 11:30 AM
  5. Macro to amend formulas
    By wmccomber in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-27-2008, 03:58 PM
  6. Amend This Loop Macro
    By davehunter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-28-2007, 09:52 AM
  7. Can Someone Please Amend This Macro
    By davehunter in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-30-2007, 12:13 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1