Excel Macro and Transposing Data

carleo

New Member
Joined
Apr 1, 2004
Messages
13
I have a macro that takes each row of data on a spreadsheet and creates a Word document. The problem I have is that all the rows have data in columns A:H but some may have data up to column Z. So when I transpose the range (H:Q) it caused my reports to have lots of blank fields or I am limiting my options. How can I transpose up to the last field on that row so that if there are only 2 records I will only use up to two rows on the report or if there are 50 records I will have 50 rows of data on the report? Below is a bit of the macro.

Sheets("Data").Select
'Find the last row with data in the database
FINALROW = Range("A9999").End(xlUp).Row
For i = 2 To FINALROW
Sheets("Data").Select
' Copy the name to cell
Range("A" & i).Copy Destination:=Sheets("Template").Range("b6")
Range("B" & i).Copy Destination:=Sheets("Template").Range("b7")
Range("C" & i).Copy Destination:=Sheets("Template").Range("b8")
Range("D" & i).Copy Destination:=Sheets("Template").Range("b10")
Range("E" & i).Copy Destination:=Sheets("Template").Range("b11")
Range("F" & i).Copy Destination:=Sheets("Template").Range("b12")
Range("G" & i).Copy Destination:=Sheets("Template").Range("b5")

Range("H" & i & ":Q" & i).Copy
Sheets("Template").Select
Range("b20").PasteSpecial Transpose:=True
' Copy the data for the new document to the clipboard

Please advise. Thanks in advance!
 
Hello,

Is this code working as expected?

Code:
    Sheets("Data").Select
For i = 2 To Range("A65536").End(xlUp).Row
    Sheets("Data").Select
    Range("A" & i).Copy Destination:=Sheets("Template").Range("b6")
    Range("B" & i).Copy Destination:=Sheets("Template").Range("b7")
    Range("C" & i).Copy Destination:=Sheets("Template").Range("b8")
    Range("D" & i).Copy Destination:=Sheets("Template").Range("b10")
    Range("E" & i).Copy Destination:=Sheets("Template").Range("b11")
    Range("F" & i).Copy Destination:=Sheets("Template").Range("b12")
    Range("G" & i).Copy Destination:=Sheets("Template").Range("b5")

FIRST_CELL = Range("H" & i).Address
SECOND_CELL = Range("A" & i).Offset(0, 6).End(xlToRight).Address
Range(FIRST_CELL, SECOND_CELL).Copy
Sheets("Template").Select
    Sheets("Template").Select
    Range("B20").Select
    Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
        , Transpose:=True
Next i
 
Upvote 0
No, it's not working as expected. I'm getting data for other rows mixed in. It was not doing that before. My entire macro is:

Sub ControlWord()
Dim appWD As Word.Application
Set appWD = CreateObject("Word.Application.8")
appWD.Visible = True

Sheets("Data").Select
For i = 2 To Range("A65536").End(xlUp).Row
Sheets("Data").Select
Range("A" & i).Copy Destination:=Sheets("Template").Range("b6")
Range("B" & i).Copy Destination:=Sheets("Template").Range("b7")
Range("C" & i).Copy Destination:=Sheets("Template").Range("b8")
Range("D" & i).Copy Destination:=Sheets("Template").Range("b10")
Range("E" & i).Copy Destination:=Sheets("Template").Range("b11")
Range("F" & i).Copy Destination:=Sheets("Template").Range("b12")
Range("G" & i).Copy Destination:=Sheets("Template").Range("b5")

FIRST_CELL = Range("H" & i).Address
SECOND_CELL = Range("A" & i).Offset(0, 6).End(xlToRight).Address
Range(FIRST_CELL, SECOND_CELL).Copy

Sheets("Template").Select
Range("B20").Select
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
, Transpose:=True

Range("A1:B34").Copy
appWD.Documents.Add
appWD.Selection.Paste

With appWD.ActiveDocument.PageSetup
.LeftMargin = Application.InchesToPoints(1)
.RightMargin = Application.InchesToPoints(1.5)
.TopMargin = Application.InchesToPoints(0.3)
.BottomMargin = Application.InchesToPoints(0.3)
End With
' Save the new document with a sequential file name
appWD.ActiveDocument.SaveAs Filename:="C:\temp\misc\Monitor Alert Site " & Range("B5") & " Report " & i
appWD.ActiveDocument.Close
Next i
' Close the Word application
appWD.Quit
End Sub
 
Upvote 0
Hello,

Is it because you are not clearing COlumn B?

If so, before the Next i at the end of the macro you should add


Code:
Sheets("Template").Columns(2).ClearContents

Has this solved it?
 
Upvote 0
It worked! Thank you very much!!!!!!!!!!
onlyadrafter said:
Hello,

Is it because you are not clearing COlumn B?

If so, before the Next i at the end of the macro you should add


Code:
Sheets("Template").Columns(2).ClearContents

Has this solved it?
 
Upvote 0

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