If text, then select entire row + run makro

raphagcwill

New Member
Joined
Jan 12, 2016
Messages
41
Hi there,

would really appreciateyour help with the below.

I have found and changed to my needs the VBA i found here (excel - Filling any empty cells with the value above - Stack Overflow).

What it does is that i search on column N for the text "PPO". If true it copies the data on cells above.

The problem is that the macro only works when i select the row i want it to run... if I run the macro without selecting the row nothing happens

Is there any "easy" vba for selecting an entire row based on a text found on column N?


Sub copy_row_above()
Dim columnValues As Range, i As Long

Set columnValues = Selection

i = 1

For i = 1 To columnValues.rows.Count
If columnValues.Cells(i, 14).Value = "PPO" Then
columnValues.Cells(i, 3).Value = columnValues.Cells(i - 1, 3).Value
columnValues.Cells(i, 9).Value = columnValues.Cells(i - 1, 9).Value
columnValues.Cells(i, 10).Value = columnValues.Cells(i - 1, 10).Value
columnValues.Cells(i, 11).Value = columnValues.Cells(i - 1, 11).Value
columnValues.Cells(i, 12).Value = columnValues.Cells(i - 1, 12).Value

End If
Next

End Sub



Thank you so much in advance

Regards,

Raphael
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi there,

would really appreciateyour help with the below.

I have found and changed to my needs the VBA i found here (excel - Filling any empty cells with the value above - Stack Overflow).

What it does is that i search on column N for the text "PPO". If true it copies the data on cells above.

The problem is that the macro only works when i select the row i want it to run... if I run the macro without selecting the row nothing happens

Is there any "easy" vba for selecting an entire row based on a text found on column N?


Sub copy_row_above()
Dim columnValues As Range, i As Long

Set columnValues = Selection

i = 1

For i = 1 To columnValues.rows.Count
If columnValues.Cells(i, 14).Value = "PPO" Then
columnValues.Cells(i, 3).Value = columnValues.Cells(i - 1, 3).Value
columnValues.Cells(i, 9).Value = columnValues.Cells(i - 1, 9).Value
columnValues.Cells(i, 10).Value = columnValues.Cells(i - 1, 10).Value
columnValues.Cells(i, 11).Value = columnValues.Cells(i - 1, 11).Value
columnValues.Cells(i, 12).Value = columnValues.Cells(i - 1, 12).Value

End If
Next

End Sub



Thank you so much in advance

Regards,

Raphael

try something like

Code:
Cells(i, 1).EntireRow.Select
 
Upvote 0
Hi,

Thanks for the suggestion...unfortunately it did not work.

i tried the following


Sub copy_row_above()

Dim columnValues As Range, i As Long

Set columnValues = Selection

i = 1


For i = 1 To columnValues.rows.Count

If columnValues.Cells(i, 14).Value = "PPO" Then
Cells(i, 1).EntireRow.Select
columnValues.Cells(i, 3).Value = columnValues.Cells(i - 1, 3).Value
columnValues.Cells(i, 9).Value = columnValues.Cells(i - 1, 9).Value
columnValues.Cells(i, 10).Value = columnValues.Cells(i - 1, 10).Value
columnValues.Cells(i, 11).Value = columnValues.Cells(i - 1, 11).Value
columnValues.Cells(i, 12).Value = columnValues.Cells(i - 1, 12).Value

End If
Next

End Sub
 
Upvote 0
Hi,

Thanks for the suggestion...unfortunately it did not work.

i tried the following


Sub copy_row_above()

Dim columnValues As Range, i As Long

Set columnValues = Selection

i = 1


For i = 1 To columnValues.rows.Count

If columnValues.Cells(i, 14).Value = "PPO" Then
Cells(i, 1).EntireRow.Select
columnValues.Cells(i, 3).Value = columnValues.Cells(i - 1, 3).Value
columnValues.Cells(i, 9).Value = columnValues.Cells(i - 1, 9).Value
columnValues.Cells(i, 10).Value = columnValues.Cells(i - 1, 10).Value
columnValues.Cells(i, 11).Value = columnValues.Cells(i - 1, 11).Value
columnValues.Cells(i, 12).Value = columnValues.Cells(i - 1, 12).Value

End If
Next

End Sub

what did not work. Are you saying that it did not select the entire row? Did it throw an error? if so, which error?
 
Upvote 0
Sorry, should have been clearer.

No error...the macro runs but no cell is selected. And since no cell is selected the orginal code does not work..

The first cell is selcted only when i change to

Cells(i, 1).EntireRow.Select
If columnValues.Cells(i, 14).Value = "PPO" Then

But if I have it on the other way around is does not work.
 
Upvote 0
Sorry, should have been clearer.

No error...the macro runs but no cell is selected. And since no cell is selected the orginal code does not work..

The first cell is selcted only when i change to

Cells(i, 1).EntireRow.Select
If columnValues.Cells(i, 14).Value = "PPO" Then

But if I have it on the other way around is does not work.

ok I used your code and the select works, the problem is you didn't change you code to do anything with the select.

What is it you want your code to do AFTER selecting the entire row?
 
Upvote 0
I would like it to copy the the data from the cells above, but only on columns C, I, J, K, and L.
The cells where i want the data to be copied is always blank.

The code i posted previouslly works flawlessly, men only when I select the row where my tex "PPO" is.

So that is why i thought that by finding and selecting the row the other code would run normally

Once agains, thanks for you time and effort

Mvh,
Raphael
 
Upvote 0
This line looks suspicious:
Code:
If columnValues.Cells(i, 14).Value = "PPO" Then
        Cells(i, 1).EntireRow.Select
columnValues is a range, so for example if columnValues is (A5:A10) then columnValues.Cells(1, 1) means A5 not A1.
 
Upvote 0
I would like it to copy the the data from the cells above, but only on columns C, I, J, K, and L.
The cells where i want the data to be copied is always blank.

The code i posted previouslly works flawlessly, men only when I select the row where my tex "PPO" is.

So that is why i thought that by finding and selecting the row the other code would run normally

Once agains, thanks for you time and effort

Mvh,
Raphael

ok I don't think selecting the entire row is needed.

this will select the whole sheet and run your code till it reaches the last row that houses data. Most of this is your code. I cleaned it up a bit.

Code:
Sub copy_row_above()

 Dim columnValues As Range, i As Long
 
    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    Set columnValues = ActiveSheet.Cells
    i = 1
    For i = 1 To columnValues.ROWS.Count
       If Cells(i, 14).Value = "PPO" Then
          Cells(i, 3).Value = Cells(i - 1, 3).Value
          Range(ActiveSheet.Cells(i, 9), ActiveSheet.Cells(i, 12)).Value = _
            Range(ActiveSheet.Cells(i - 1, 9), ActiveSheet.Cells(i - 1, 12)).Value
       End If
    Next
    With Application
        .DisplayAlerts = True
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
 End Sub
 
Upvote 0
Solution
OMG, OMG

it did work without any glicth. You are the man!!!!

Thanks a million for all the effort and commitment

Many Regards,
Raphael
 
Upvote 0

Forum statistics

Threads
1,225,214
Messages
6,183,625
Members
453,177
Latest member
GregL65

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