VBA code opening a new workbook and giving out put. how to fix it?

Vigash

New Member
Joined
Aug 26, 2022
Messages
21
Office Version
  1. 2013
Platform
  1. Windows
Hi All,

This is the code I am trying to execute. I am getting the result correctly. But my issue is instead of lastrow sheet in the same workbook macro creates a new workbook with a new sheet named as the last row and giving output there.. Why it's not giving the result in the same worksheet (lastrow) which is available in the existing workbook? Please clarify.

Sub lastrowfinder()
Dim lr As Integer
Worksheets("Stock Out").Activate
ActiveSheet.ListObjects("Stockout").Range.Select
lr = ActiveSheet.ListObjects("Stockout").ListRows.Count
Worksheets("lastrow").Activate
Range("A2").Select
If Range("A2").Value = "" And Range("B2").Value = "" Then
Range("A2").Value = lr

ElseIf Range("A2").Value <> "" And Range("B2").Value = "" Then
Selection.Offset(0, 1).Value = lr

ElseIf Range("A2").Value <> "" And Range("B2").Value <> "" Then
Range("A2").Value = Range("B2").Value
Range("B2").Value = lr

End If
End Sub

Thanks,
Vigash
Screenshot.PNG
 
Hi,

Sorry for the late .. I am trying to execute macro if any new data is added to my table.

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Records As Range
Set Records = Range("Stockout")
If Not Application.Intersect(Records, Range(Target.Address)) Is Nothing Then

Call Module3.lastrow_value

End If

End Sub

I am struggling to work with events especially when it comes to the Table structure. Kindly advice.

Vigash
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,224,820
Messages
6,181,159
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