简体   繁体   中英

How do I remove date and place it in a column using Excel VBA

So I have an export of data that comes out in the following form (dates continue for a month):

9/1/2014  
Team        Quantity  
Tom            1  
Jim            2

9/2/2014  
Team        Quantity  
Tom            1  
Jim            2

I have to manually remove the date from the top of each line and put it in a column so that I can create a pivot table from it. Is there any VBA code that could take the date from the top row and put it in a column like this:

Team       Quantity     Date  
Tom           1          9/1/2014  
Jim           2          9/1/2014 etc...

Team       Quantity     Date  
Tom           1          9/2/2014  
Jim           2          9/2/2014 etc...

The only problem is the number of lines that can be under a team is variable every month. Any suggestions?

Here Is what I have for you based off the little Information. I have created you a template and this is how it looks. 在此处输入图片说明

You will have to tabs on is to run the program and the other one is where you put you're data in. 在此处输入图片说明

So if you take a look at the data I put in all I would have to do is go push the run button and this is what it will look like. 在此处输入图片说明

So here is the code for the run button.

Sub btnRun()
'declare variables
Dim ToBeFound, strVar1, strVar2 As String
Dim rng As Range

ToBeFound = "Team"

'Activate and selete sheet1 than select A1
Sheets("DataTab").Activate
Range("A1").Activate

For x = 1 To 1000
Set rng = Cells.Find(What:=ToBeFound, LookIn:=xlValues, lookat:=xlWhole)

If IIf(rng Is Nothing, "", rng) = "" Then
MsgBox "Complete"
Exit Sub

Else
rng.Activate
ActiveCell.Value = "Team:"
ActiveCell.Offset(0, 2).Value = "Date"
strVar1 = ActiveCell.Offset(-1, 0).Value
ActiveCell.Offset(-1, 0).EntireRow.Delete

For i = 1 To 100
ActiveCell.Offset(1, 0).Activate
If ActiveCell.Value = "" Then
Exit For

Else
ActiveCell.Offset(0, 2).Value = strVar1
End If
Next i
End If
Next x

End Sub

And here is the template with everything already complete and done all you do is have to load in the data into the data tab and push run. http://speedy.sh/twvt3/Template.xlsm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM