Problem with macro for replacing cell data with content 1 cell above active cell.

holmen

New Member
Joined
Sep 29, 2015
Messages
4
Hi!

I'm having some trouble converting raw-data to a format I can put to use in a pivotable.
At my work we pull data a couple of times a day from a external program, the formatting of that data makes for allot of manual labour before we can present it.
I'm trying to write a VBA scripts that cycles through column A of the data and replace all instaces of "agent total" with the name of the agent.
The name of the agent is always one cell above the active cell.

The only way I have worked with macros before is with the "record macro" function so i'm basically new to this.

I'm using Excel2013
and Windows Xp

I have tried to modify a search an replace recorded macro by editing the replacement value with all kinds of things but nothing remotely works.
I tried yo use a ActiveCell.Offset(-1, 0) to get the content of the cell above

This is how far I come:
Code:
Columns("A:A").Select
Selection.Replace What:="agent total", Replacement:= ActiveCell.Offset(-1, 0), LookAt:= _ 
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

The actual data look something like this:

AgentAgentIDQueueCalls InboundAbandonsAHT IB
Andersson Christoffer114110OSD_Q_Viasat_Teknik_IPTV46000:12:41
Andersson Christoffer114110OSD_TA_Viasat_Teknik_IPTV9000:23:17
agent total159100:13:15
Andersson Henrik130080OSD_TA_Viasat_Sales_Teknik3000:08:40
Andersson Henrik130080OSD_Q_Viasat_Teknik_IPTV0000:00:00
agent total68100:11:39
Andersson Johan120017OSD_TA_Viasat_Teknik15000:07:54
Andersson Johan120017OSD_TA_Viasat_Sales_Teknik3000:24:38
agent total104000:10:15
Andersson Simon130561OSD_Q_Viasat_Teknik_IPTV90100:09:22
Andersson Simon130561OSD_TA_Viasat_Teknik_IPTV26000:14:10
agent total299100:09:15

<tbody>
</tbody>

I know I must be making some basic mistake but I have no idea where to start correcting.
If anyone of you know how to make this work It would be very appreciated.

//Niklas
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi Niklas, welcome to the boards.

This is untested as yet, but try this:

Rich (BB code):
Columns("A:A").Select
Selection.Replace What:="agent total", Replacement:= ActiveCell.Offset(-1, 0).Value, LookAt:= _ 
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
 
Upvote 0
Hi Niklas, welcome to the boards.

This is untested as yet, but try this:

Rich (BB code):
Columns("A:A").Select
Selection.Replace What:="agent total", Replacement:= ActiveCell.Offset(-1, 0).Value, LookAt:= _ 
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

Hi and thank you.

I tried it but i still won't work.
in debug it says
"ActiveCell.Offset(-1, 0).Value= application-defined of object defined error"<application-defined or="" object-defined="" error="">

I'm thinking I can't modify the "Selection.Replace" command like this and I need to write a script that moves down one one cell at at time.
Then if it finds "agent total" it should set it as active cell, copy data from activecell with offset (-1, 0) and paste it in active cell.
then resume the walk down the column.

I know what I want to do but how to write the function is a mystery.
I have been trying to google similar functions but when I try to make sense of the code I'm missing the basic knowledge to make sense of it.

Any more ideas would be great :)
Until then I keep googling away.

//Niklas</application-defined>
 
Upvote 0
11qHNDI4.png




will this work my brother ?

Code:
Sub TESTING()

Dim i


For i = 5 To 16


If Cells(i, 4) = "Total" Then
   Cells(i, 4) = Cells(i - 1, 4)
End If


Next i


End Sub


basically for the name range i am asking Excel to look for the word Total if he/she finds it he/she replace it with the name from the cell above it ... not a brilliant loop but it works.

however i wonder how can we add a feature to unmerge those nasty total cells when found.


may the force be with you (y)
 
Last edited:
Upvote 0
Oh la la la just add unmerge command:

Code:
Sub TESTING()

Dim i


For i = 5 To 16


If Cells(i, 4) = "Total" Then
   Cells(i, 4) = Cells(i - 1, 4)
[COLOR=#ff0000]   Cells(i, 4).UnMerge[/COLOR]
   
End If


Next i


End Sub
 
Upvote 0
Hi again Niklas,

Have done some testing this time and have something which I believe should help you.

Code:
Sub Test_Replace()
Set MyRng = Range(Cells(1, "A"), Cells(Rows.Count, "A").End(xlUp))
For Each cell In MyRng
    If cell.Value = "Agent Total" Then
    cell.UnMerge
    cell.Value = cell.Offset(-1, 0).Value
        End If
    Next
End Sub
 
Upvote 0
Updated to also copy the correct cell alignment from the cell above after the cell is unmerged:

Code:
Sub Test_Replace()
Set MyRng = Range(Cells(1, "A"), Cells(Rows.Count, "A").End(xlUp))
For Each cell In MyRng
    If cell.Value = "Agent Total" Then
    cell.UnMerge
    cell.Value = cell.Offset(-1, 0).Value
    cell.HorizontalAlignment = cell.Offset(-1, 0).HorizontalAlignment
        End If
    Next
End Sub
 
Upvote 0
Awesome, thank for al the help.
It's now working and I nearly have the whole thing done.
There is just some small fixing left to.

The last problem I have is with this script.

Code:
With .Range("A1:A" & lRow)
            .AutoFilter Field:=1, Criteria1:="<>*.*"
            .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With

I am deleting all rows that don't ends with "." but I need to keep the last row that says "total" (and don't have a ".")
I'm trying to figure out how to exclude "total" from deleting.

any input?
 
Upvote 0
Awesome, thank for al the help.
It's now working and I nearly have the whole thing done.
There is just some small fixing left to.

The last problem I have is with this script.

Code:
With .Range("A1:A" & lRow)
            .AutoFilter Field:=1, Criteria1:="<>*.*"
            .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With

I am deleting all rows that don't ends with "." but I need to keep the last row that says "total" (and don't have a ".")
I'm trying to figure out how to exclude "total" from deleting.

any input?

Try Changing:

Code:
With .Range("A1:A" & lRow)
            [COLOR=#ff0000][B].AutoFilter Field:=1, Criteria1:="<>*.*"[/B][/COLOR]
            .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With

To:

Code:
With .Range("A1:A" & lRow)
[B][COLOR=#ff0000]            .AutoFilter Field:=1, Criteria1:="<>*.", _[/COLOR][/B]
[B][COLOR=#ff0000]        Operator:=xlAnd, Criteria2:="<>total"[/COLOR][/B]
            .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,792
Members
449,468
Latest member
AGreen17

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