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
 
Thanks a bunch.
I had to make some minor modification but I got it to work.
The file now works like a charm.

The final script ended up like this.

Code:
Sub Fixa_data()


    Sheets("rawdata").Select
    
    Rows("1:2").Select
    Selection.Delete Shift:=xlUp
        
        Columns("A:A").Select
        
        Dim i


        For i = 1 To 10000


        If Cells(i, 1) = "Agent total" And Cells(i + 0, 6) > "00:00:00" Then
        Cells(i, 1) = Cells(i - 1, 1) & "."
       
End If


Next i


Dim ws As Worksheet
    Dim lRow As Long


    Set ws = ThisWorkbook.Sheets("rawdata")


    With ws
        .AutoFilterMode = False


        lRow = .Range("A" & .Rows.Count).End(xlUp).Row


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


        .AutoFilterMode = False
        
    End With
    
    
    Columns("A:A").Select
    Selection.Replace What:=".", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    
    
    Sheets("Pivotable").Select
    


    MsgBox "Done"
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Thanks a bunch.
I had to make some minor modification but I got it to work.
The file now works like a charm.

The final script ended up like this.

Code:
Sub Fixa_data()


    Sheets("rawdata").Select
    
    Rows("1:2").Select
    Selection.Delete Shift:=xlUp
        
        Columns("A:A").Select
        
        Dim i


        For i = 1 To 10000


        If Cells(i, 1) = "Agent total" And Cells(i + 0, 6) > "00:00:00" Then
        Cells(i, 1) = Cells(i - 1, 1) & "."
       
End If


Next i


Dim ws As Worksheet
    Dim lRow As Long


    Set ws = ThisWorkbook.Sheets("rawdata")


    With ws
        .AutoFilterMode = False


        lRow = .Range("A" & .Rows.Count).End(xlUp).Row


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


        .AutoFilterMode = False
        
    End With
    
    
    Columns("A:A").Select
    Selection.Replace What:=".", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    
    
    Sheets("Pivotable").Select
    


    MsgBox "Done"
Glad to help mate. I am teaching myself most of this as I go along so I am happy to to keep trying to solve new problems. I am pleased we eventually got there in the end :)
 
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,005
Members
449,203
Latest member
Daymo66

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