Excel macro help

excelnewbie12

New Member
Joined
Jul 17, 2015
Messages
1
Hello, this is my first time posting on here.

I'm trying to write a macro that will string data into a word document (not just copying and pasting a table, but actually putting it in lines). I'm new at Excel so I'm trying to work off of someone else's macro..which is only kind of helping. So far, I've gotten this word documents to open, but nothing will actually go in them.

Here's my code:

Code:
'compiles what is needed for each room into respective strings
'and feeds them into a new word document which is saved by date in the folder "Distro"
Sub Macro()
Sheets("Delivery").Select
Dim iCell As Range
Dim gRange(7) As Range

Set gRange(0) = Worksheets("Delivery").Range("D3:D100")
Set gRange(1) = Worksheets("Delivery").Range("E3:E100")
Set gRange(2) = Worksheets("Delivery").Range("F3:F100")
Set gRange(3) = Worksheets("Delivery").Range("G3:G100")
Set gRange(4) = Worksheets("Delivery").Range("H3:H100")
Set gRange(5) = Worksheets("Delivery").Range("I3:I100")
Set gRange(6) = Worksheets("Delivery").Range("J3:J100")
Set gRange(7) = Worksheets("Delivery").Range("K3:K100")

Dim room(7) As String
room(0) = "4 North : "
room(1) = "4 South: "
room(2) = "4 East: "
room(3) = "4 West: "
room(4) = "5 South: "
room(5) = "5 East: "
room(6) = "5 West: "
room(7) = "Reception: "

Dim FilePath As String

FilePath = ThisWorkbook.Path & "\Distro\distro." & Format(Date, "mm") & "." & Format(Date, "dd") & "." & Format(Date, "yyyy") & ".doc"

Dim objWord
Dim distro
Dim objSelection

Set objWord = CreateObject("Word.Application")
Set distro = objWord.Documents.Add

objWord.Visible = True

Set objSelection = objWord.Selection

For i = 0 To UBound(gRange)
    cost = 0
    For Each iCell In gRange(i)
        room(i) = room(i) + itemString(iCell)
        If iCell.Value > 0 Then
            cost = cost + ((iCell.Value / Range("Y" & iCell.Row)) * Range("Z" & iCell.Row))
        End If
    Next iCell
    objSelection.TypeText (Left(room(i), Len(room(i)) - 2) & vbCrLf & vbCrLf)
    costs(i) = cost
Next i

distro.SaveAs (FilePath)

'---------------------------------------------------------------------------------
'And now we make lists of units to be pulled from what we already have on hand
'and then boxes/packs of units to be ordered, and creates a new word doc for them
'and saves it by date to the folder "Orders"

Sheets("Delivery").Select
Dim rPull As Range

Set rPull = Worksheets("Delivery").Range("A3:A100")

Dim filePath2 As String
filePath2 = ThisWorkbook.Path & "\Orders\order." & Format(Date, "mm") & "." & Format(Date, "dd") & "." & Format(Date, "yyyy") & ".doc"

Dim orderdoc
Dim objSelect

Set orderdoc = objWord.Documents.Add

objWord.Visible = True

Set objSelect = objWord.Selection

Dim PullList As String
PullList = "Pull (by Unit): "

Dim OrderList As String
OrderList = "Order(boxes/packs) to 4 North: "

For Each myCell In rPull
    If myCell.Value > 0 Then
        PullList = PullList & myCell.Value & " " & Range("A" & myCell.Row).Text & ", "
    End If
    If myCell(1, 0).Row <> "22" Then
        If myCell(1, 0).Value > 0 Then
            OrderList = OrderList & myCell(1, 0).Value & " " & Range("A" & myCell.Row).Text & ", "
        End If
    End If
Next myCell

Sheets("MinMaxNeed").Select

Dim Paper(7) As Range
Dim rooms(7) As Range
Dim ordpaper As String

ordpaper = ""

Set Paper(0) = Worksheets("MinMaxNeed").Range("E23")
Set Paper(1) = Worksheets("MinMaxNeed").Range("I23")
Set Paper(2) = Worksheets("MinMaxNeed").Range("M23")
Set Paper(3) = Worksheets("MinMaxNeed").Range("Q23")
Set Paper(4) = Worksheets("MinMaxNeed").Range("U23")
Set Paper(5) = Worksheets("MinMaxNeed").Range("Y23")
Set Paper(6) = Worksheets("MinMaxNeed").Range("AC23")
Set Paper(7) = Worksheets("MinMaxNeed").Range("AG23")

Set rooms(0) = Worksheets("MinMaxNeed").Range("C3")
Set rooms(1) = Worksheets("MinMaxNeed").Range("G3")
Set rooms(2) = Worksheets("MinMaxNeed").Range("K3")
Set rooms(3) = Worksheets("MinMaxNeed").Range("O3")
Set rooms(4) = Worksheets("MinMaxNeed").Range("S3")
Set rooms(5) = Worksheets("MinMaxNeed").Range("W3")
Set rooms(6) = Worksheets("MinMaxNeed").Range("AA3")
Set rooms(7) = Worksheets("MinMaxNeed").Range("AE3")

objSelect.TypeText (Left(PullList, Len(PullList) - 2) & vbCrLf & vbCrLf & Left(OrderList, Len(OrderList) - 2) & vbCrLf & vbCrLf & "Paper:" & vbCrLf & vbCrLf & vbCrLf)

orderdoc.SaveAs (filePath2)

End Sub

Public Function itemString(iCell As Range) As String

Sheets("Delivery").Select

If iCell.Value > 0 Then
    itemString = iCell.Value & " " & Range("B" & iCell.Row).Text & " of " & Range("A" & iCell.Row).Text & ", "
Else
    itemString = vbNullString
End If

End Function

I'm still learning VBA (this macro is my first time ever using it), so don't make fun of me too much, please! There's probably some unnecessary code and whatnot in there. Basically, I'm trying to string inventory into a distribution list and an order list. If anyone can help, I'd really appreciate it. :):confused:
 
Last edited by a moderator:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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