VBA Code doesnt work when pressing the button but working when inside code module.

romanemul

New Member
Joined
Jul 22, 2016
Messages
35
Hi forum members.

Macro is running fine from inside module. It stops on certain line .

Point of this code:
Im trying to change workbook generated by another application by prepending a number to certain lines after inserting a new column.

Problem line :
Code:
cell.Offset(2, -1).Select

Code:
Sub formatter()

Dim rng As Range

Dim cell As Range
Dim daterange As Range
Dim countrange As Range
Dim datacell As Range


Dim wb As Workbook
Dim ws As Worksheet
Dim actwb As Workbook
Dim workbookname As String
Dim xlapp As Application


Set xlapp = GetObject(, "Excel.Application")

workbookname = InputBox("Number of book ?")

Set wb = xlapp.Workbooks("Book" & workbookname)
Set ws = wb.Worksheets("Sheet1")
Set rng = ws.Range("a:a")

rng.Cells.Columns.Insert (1)

Set cell = ws.Range("A1")


For Each cell In ws.Range("b:b")
    
    If cell.Interior.Color = black Then
        Set countrange = cell.Offset(2, -1)
        
        Set daterange = Range(cell.Offset(2, 8), cell.Offset(2, 8).End(xlDown))
        
        cell.Offset(2, -1).Select    ' <------stops working here
                    
            If daterange.Count > 500 Then End
            
                

                counter = 0
                For Each datacell In daterange
                
                    If Left(datacell.Text, 5) = Left(datacell.Offset(1, 0).Text, 5) Or datacell.Offset(1, 0) = "" Then
                        countrange.Value = counter + 1
                        counter = counter + 1
                        Set countrange = countrange.Offset(1, 0)


                    Else
                        countrange.Value = counter + 1
                        counter = 0
                        Set countrange = countrange.Offset(1, 0)
                    
                    End If
                Next datacell
            
    End If
Next cell


Set rng = Nothing
Set wb = Nothing
Set ws = Nothing


End Sub



I read about external referencing of cells and i think i have my cell referenced properly
at
Code:
Set cell = ws.Range("A1")

Can anybody help me ?

Thanks
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
You can only select a cell on the active sheet.
 
Upvote 0
You could just remove that line as it doesn't appear to serve any purpose.
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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