databaseuniversity
New Member
- Joined
- Jul 19, 2012
- Messages
- 4
I'm trying to make a code that would copy cells down a single column until the cell before a keyword, copy these cells, and paste them (transpose them) into a row on a new page. Then it would find the next instance of the keyword and copy from the keyword to the cell before the next keyword and transpose into the next row on the new page.
I would start with this (assuming "flower" is keyword):
flower
rose
$1.79
in stock
flower
hydrangea
$5.00
flower
tulip
flower
and end up with this:
flower rose $1.79 in stock
flower hydrangea $5.00
flower tulip
flower
I tried to modify this code: http://www.ozgrid.com/VBA/transpose-rows.htm
but I'm lost.... Here is what I have:
(excel 2007, windows xp)
---------------------------------------------------------------
Any help is much appreciated. I'm obviously a complete novice at this!
I would start with this (assuming "flower" is keyword):
flower
rose
$1.79
in stock
flower
hydrangea
$5.00
flower
tulip
flower
and end up with this:
flower rose $1.79 in stock
flower hydrangea $5.00
flower tulip
flower
I tried to modify this code: http://www.ozgrid.com/VBA/transpose-rows.htm
but I'm lost.... Here is what I have:
(excel 2007, windows xp)
---------------------------------------------------------------
Code:
Option Explicit
Sub TransposetoKeyword()
Dim lRows As Long, lCol As Long
Dim rCol As Range
Dim lLoop As Long
Dim wsStart As Worksheet, wsTrans As Worksheet
On Error Resume Next
'Get single column range
Set rCol = Application.InputBox(Prompt:="Select single column", _
Title:="TRANSPOSE ROWS", Type:=8)
'Cancelled or non valid range
If rCol Is Nothing Then Exit Sub
'I modified this... not sure if I did it right
'Get keyword. type 2 is for text
Dim keyword As Long
keyword = Application.InputBox(Prompt:="Select keyword", _
Title:="KEYWORD", Type:=2)
'Cancelled or non valid keyword
If keyword = 0 Then Exit Sub
'Make sure the transpositions will fit
If lRows > ActiveSheet.Columns.Count Then
MsgBox "Your 'transpose every x rows' exceeds the columns available"
Exit Sub
End If
'Limit range to used cells
lCol = rCol.Column
Set rCol = Range(rCol(1, 1), Cells(Rows.Count, lCol).End(xlUp))
'Set Worksheet variables
Set wsStart = ActiveSheet
Set wsTrans = Sheets.Add()
wsStart.Select
'Loop with step of x and transpose
'This is what I need to fix... so it searches for the keyword and changes the number
'accordingly. Not a fixed number....
For lLoop = rCol(1, 1).Row To Cells(Rows.Count, lCol).End(xlUp).Row Step lRows
Cells(lLoop, lCol).Resize(lRows, 1).Copy
wsTrans.Cells(Rows.Count, "A").End(xlUp)(2, 1).PasteSpecial Transpose:=True
Application.CutCopyMode = False
Next lLoop
On Error GoTo 0
End Sub
Any help is much appreciated. I'm obviously a complete novice at this!