VBA not copying cell value when running it on certain files

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
I have this code that counts the number of "CHAIN & EYEBOLT" in some excel files, then adds one row at the end of the file to show the total number of this item.
The new row should always have: column A (value from cell above), C (total number of the item), B, D, I and K with the static values shown below

1673055575090.png


This code works............on 90% of the files, but on a few test files, it is not copying the value in column A, like the example below, cell A returned as blank
I am not sure exactly what is causing this issue, any help is appreciated
1673055751549.png


dropbox link below for 2 test files, this one is working

and this one is not

full code below, any criticism on the way this is coded is also welcome.
I am always looking for ways to improve these VBA codes..

VBA Code:
Sub EyeboltsAndChains()
    Dim lrNew As Long
    lrNew = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    sr = 2
    With Range("M1", Cells(Rows.Count, "M").End(3))
            .Replace What:="@ 18""", Replacement:=vbNullString, LookAt:=xlPart
            .Replace What:="@ 24""", Replacement:=vbNullString, LookAt:=xlPart
            .Replace What:="@ 30""", Replacement:=vbNullString, LookAt:=xlPart
            .Replace What:="@ 42""", Replacement:=vbNullString, LookAt:=xlPart
            .Replace What:="@ 48""", Replacement:=vbNullString, LookAt:=xlPart
            .Replace What:="@ 60""", Replacement:=vbNullString, LookAt:=xlPart
            .Replace What:="@ 72""", Replacement:=vbNullString, LookAt:=xlPart
            .Replace What:="&", Replacement:=vbNullString, LookAt:=xlPart
            .Replace What:="2 1", Replacement:="3", LookAt:=xlPart
    End With
    
    n = WorksheetFunction.SumIfs(Range("M" & sr & ":M" & lr), Range("K" & sr & ":K" & lr), "*CHAIN & EYEBOLT*")
    lrNew = lrNew + 1

    Cells(lrNew, "A") = Cells(lr, "A")
    Cells(lrNew, "B") = "."
    Cells(lrNew, "C") = n
    Cells(lrNew, "D") = "F09720"
    Cells(lrNew, "I") = "Purchased"
    Cells(lrNew, "K") = "CHAIN & EYEBOLT"

    If Cells(lrNew, "C").Value Like "*0*" Then
        Rows(lrNew).Delete
    End If
End Sub
 

Attachments

  • 1673055522867.png
    1673055522867.png
    11 KB · Views: 8

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
The issue is because actually you have mixed up use of lr and lrNew so much. If I were you, at first, I would declare both properly:
VBA Code:
Dim lrNew As Long, lr As Long
Then use only lr as last row until the end of the code:
VBA Code:
lr = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
At this point I would do a move like this:
VBA Code:
lrNew = lr + 1

I think the code must run fine after all these fixes.
 
Upvote 0
Solution
The issue is because actually you have mixed up use of lr and lrNew so much. If I were you, at first, I would declare both properly:
VBA Code:
Dim lrNew As Long, lr As Long
Then use only lr as last row until the end of the code:
VBA Code:
lr = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
At this point I would do a move like this:
VBA Code:
lrNew = lr + 1

I think the code must run fine after all these fixes.
thanks, this indeed fixed the issue. :)
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top