Run two codes in same macro without error

Kyle M

Board Regular
Joined
Apr 13, 2010
Messages
65
The 1st block of code below is meant to start in row 6, and fill every blank row in Column A with data from the row above if the same row in Column B has data. The same is true for the 2nd block of code except the Columns are D and B. If I try to run both codes in one macro and row 6 is the only row with data I receive an error message that says ‘Error: application defined or object defined error’ and this line of code
Code:
M.Value = M.Offset(-1).Value
gets highlighted in yellow. Do you know why that is happening and how I can fix it so I can run both codes in the same macro with one row of data and not get an error message?


Code:
Dim I As Range<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Dim J As Range<o:p></o:p>
Dim K As Long<o:p></o:p>
On Error GoTo L<o:p></o:p>
With Worksheets("Sheet1")<o:p></o:p>
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row<o:p></o:p>
Set J = .Range("A6:A" & LastRow).SpecialCells(xlCellTypeBlanks)<o:p></o:p>
For Each I In J<o:p></o:p>
I.Value = I.Offset(-1).Value<o:p></o:p>
Next<o:p></o:p>
End With<o:p></o:p>
L:<o:p></o:p>
<o:p> </o:p>
Dim M As Range<o:p></o:p>
Dim N As Range<o:p></o:p>
Dim O As Long<o:p></o:p>
On Error GoTo P<o:p></o:p>
With Worksheets("Sheet1")<o:p></o:p>
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row<o:p></o:p>
Set N = .Range("D6:D" & LastRow).SpecialCells(xlCellTypeBlanks)<o:p></o:p>
For Each M In N<o:p></o:p>
M.Value = M.Offset(-1).Value<o:p></o:p>
Next<o:p></o:p>
End With<o:p></o:p>
P:
<o:p></o:p>
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try a slightly more steam driven approach
Sub Fixdata()

Dim I As Integer
Dim Test, Test2, Test3, Fix_, Fix_2 As String

With Worksheets("Sheet1")
Lastrow = Range("B65000").End(xlUp).Row
For I = 6 To Lastrow
Test = "A" & I
Fix_ = "A" & I - 1
Test2 = "B" & I
Test3 = "D" & I
Fix_2 = "D" & I - 1
If (Range(Test) = "") And (Range(Test2) <> "") Then Range(Test) = Range(Fix_).Value
If (Range(Test3) = "") And (Range(Test2) <> "") Then Range(Test3) = Range(Fix_2).Value
Next I
End With
End Sub
 
Last edited:
Upvote 0
SOLVED: Run two codes in same macro without error

Choo Choo Airwolf! This code rules. Thank you!
 
Last edited:
Upvote 0
SOLVED: Run two codes in same macro without error

Thank you for the tip Andrew!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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