How to combine 2 codes?(limiting rows in column to X value)

masofras

New Member
Joined
Jun 30, 2016
Messages
8
Dear Excel Experts

Trying to solve problem with macro. However, my VBA coding skills are basic. Any help or hint is welcome

Here is code written correctly(code is not written by me)

Code:
Private Sub Worksheet_Calculate()'Check if values haven't changed
    If Range("F13").Value = Cells(Rows.Count, 1).End(xlUp).Value And _
       Range("K13").Value = Cells(Rows.Count, 2).End(xlUp).Value Then Exit Sub
       
'Store the new values
    Application.EnableEvents = False
    
    If Range("F13").Value <> Cells(Rows.Count, 1).End(xlUp).Value And _
       Range("F13").Value > 0 Then
        Cells(Rows.Count, 1).End(xlUp)(2).Value = Range("F13").Value
    End If
    
    If Range("K13").Value <> Cells(Rows.Count, 2).End(xlUp).Value And _
       Range("K13").Value > 0 Then
        Cells(Rows.Count, 2).End(xlUp)(2).Value = Range("K13").Value
    End If
    
    Application.EnableEvents = True
    
End Sub


What I need exactly? Lets say we limit/cant exceed rows in column to 48 (make range from A2 to A49) and IF any value reaches A49 then A2 delete any value, A49 is filled with new value and whole A2:A48 row moves up. It is cycle.

Something like code below :
Code:
Dim NR As Long    NR = Range("A" & Cells(Rows.Count).Row).End(xlUp).Row + 1
   Range("A" & NR).Value = Range("F13").Value
   If NR > 48 Then
    Range("A2:A48").Value = Range("A3:A49").Value
    Range("A49").ClearContents
    End If


I do not know how to combine those two codes. If you have any question just ask
Thanks for your willingness to help others

Have a nice day :cool:
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi masofras,

Try to copy and paste the following modified code. I have just created a "Combining" procedure to call it from "Worksheet_Calculate". Let me know if this is what you want.

Private Sub Worksheet_Calculate() 'Check if values haven't changed
If Range("F13").Value = Cells(Rows.Count, 1).End(xlUp).Value And _
Range("K13").Value = Cells(Rows.Count, 2).End(xlUp).Value Then Exit Sub

'Store the new values
Application.EnableEvents = False

If Range("F13").Value <> Cells(Rows.Count, 1).End(xlUp).Value And _
Range("F13").Value > 0 Then
Combining
End If

If Range("K13").Value <> Cells(Rows.Count, 2).End(xlUp).Value And _
Range("K13").Value > 0 Then
Combining
End If

Application.EnableEvents = True

End Sub


Sub Combining()
Dim NR As Long
NR = Range("A" & Cells(Rows.Count).Row).End(xlUp).Row + 1
Range("A" & NR).Value = Range("F13").Value
If NR > 48 Then
Range("A2:A48").Value = Range("A3:A49").Value
Range("A49").ClearContents
End If
End Sub
 
Upvote 0
Hello badeea

At first thank you for reply and your spent time, much appreciated. It is working like a charm but one problem shows.
If I set higher NR value than 64(I mean 48 in code) in "Combining()" macro, it doesnt continue. It is stucked. If I set value lower NR value than 64 it is working perfect.

I found on other website solution. Code below:
Code:
[COLOR=#333333]Range("A" & Cells(Rows.Count, 1).Row).End(xlUp).Offset(1, 0).Value = Range("F13").Value[/COLOR]

Sorry, I didnt know that problem will show. If you can combine those 3 codes somehow, it will be perfect and final code.


Have a nice day :cool:
 
Upvote 0
Hi,

The following "Combining()" code should make it. Try and let me know what you get.

Sub Combining()
Dim NR As Long
NR = Range("A" & Cells(Rows.Count).Row).End(xlDown).Row + 1
Range("A" & Cells(Rows.Count, 1).Row).End(xlUp).Offset(1, 0).Value = Range("F13").Value
If NR > NR - 1 Then
Range("A2:A" & NR - 1).Value = Range("A3:A" & NR).Value
Range("A" & NR).ClearContents
End If
End Sub
 
Upvote 0
Code showing me error:
Run-time error 1004
Method Range of object_Worksheet failed

Here is how my code looks like (NR = 70)
Code:
[COLOR=#333333]Sub Combining()[/COLOR]
[COLOR=#333333]Dim NR As Long[/COLOR]
[COLOR=#333333]NR = Range("A" & Cells(Rows.Count).Row).End(xlDown).Row + 1[/COLOR]
[COLOR=#333333]Range("A" & Cells(Rows.Count, 1).Row).End(xlUp).Offset(1, 0).Value = Range("F13").Value[/COLOR]
[COLOR=#333333]If NR > NR - 1 Then[/COLOR]
[COLOR=#ff8c00]Range("A2:A[B]70[/B]" & NR - 1).Value = Range("A3:A[B]71[/B]" & NR).Value[/COLOR]
[COLOR=#333333]Range("A[B]71[/B]" & NR).ClearContents[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End Sub[/COLOR]

If I click debug, excel shows me that error is in dark orange row.

Thanks
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,393
Members
449,446
Latest member
CodeCybear

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