+ Reply to Thread
Results 1 to 3 of 3

IF statements

Hybrid View

stanton17 IF statements 03-30-2016, 12:47 PM
Leith Ross Re: IF statements 03-30-2016, 01:28 PM
stanton17 I would need the condition to... 03-30-2016, 01:56 PM
  1. #1
    Registered User
    Join Date
    06-28-2015
    Location
    North East, England
    MS-Off Ver
    2010
    Posts
    39

    IF statements

    HI

    i have some VBA code that inputs data from cells into a Word templete bookmarks and then saves the document as a seperate file mutiple times.

    This works well however what i need help with is a way to say if range B2 to B10000 > 0 then dont copy the data to my word templete. The reason for this is if there is no amount in B2 then i dont want the information copied to the doc temeplete.

    beleow is the code so far

    Formula: copy to clipboard
    Private Sub CommandButton1_Click()
    Dim objWord As Object
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    objWord.Documents.Open "C:myfiles\Letter template.docx" ' change as required
    With objWord.ActiveDocument

    .Bookmarks("Text1").Range.Text = ws.Range("A2").Value
    .Bookmarks("Text2").Range.Text = ws.Range("A2").Value
    .Bookmarks("Text3").Range.Text = ws.Range("B2").Value
    .Bookmarks("Text4").Range.Text = ws.Range("C2").Value
    .Bookmarks("Text5").Range.Text = ws.Range("D2").Value
    .Bookmarks("Text6").Range.Text = ws.Range("E2").Value

    End With
    objWord.ActiveDocument.SaveAs FileName:="C:myfiles\Letter1.docx"
    objWord.ActiveDocument.Close
    objWord.Documents.Open "C:myfiles\Letter template.docx" ' change as required
    With objWord.ActiveDocument
    .Bookmarks("Text1").Range.Text = ws.Range("A3").Value
    .Bookmarks("Text2").Range.Text = ws.Range("A3").Value
    .Bookmarks("Text3").Range.Text = ws.Range("B3").Value
    .Bookmarks("Text4").Range.Text = ws.Range("C3").Value
    .Bookmarks("Text5").Range.Text = ws.Range("D3").Value
    .Bookmarks("Text6").Range.Text = ws.Range("E3").Value
    End With
    objWord.ActiveDocument.SaveAs FileName:="C:myfiles\Letter2.docx"
    objWord.ActiveDocument.Close
    objWord.Documents.Open "C:myfiles\Letter template.docx" ' change as required
    With objWord.ActiveDocument
    .Bookmarks("Text1").Range.Text = ws.Range("A4").Value
    .Bookmarks("Text2").Range.Text = ws.Range("A4").Value
    .Bookmarks("Text3").Range.Text = ws.Range("B4").Value
    .Bookmarks("Text4").Range.Text = ws.Range("C4").Value
    .Bookmarks("Text5").Range.Text = ws.Range("D4").Value
    .Bookmarks("Text6").Range.Text = ws.Range("E4").Value
    End With
    objWord.ActiveDocument.SaveAs FileName:="C:myfiles\Letter3.docx"
    objWord.ActiveDocument.Close
    Set objWord = Nothing
    End Sub
    Last edited by stanton17; 03-30-2016 at 12:50 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: IF statements

    Hello stanton17,

    What happens if B3 or B4 is zero?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    06-28-2015
    Location
    North East, England
    MS-Off Ver
    2010
    Posts
    39
    Quote Originally Posted by Leith Ross View Post
    Hello stanton17,

    What happens if B3 or B4 is zero?
    I would need the condition to include every cell of coloum B. If B is 0 or blank then no letter needs to be created and that riw is ignored but if 1 or more is input then the rest if the data will populate the letter
    Last edited by stanton17; 03-30-2016 at 03:51 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Nested if statements containing multiple and statements
    By John M. in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 06-22-2014, 11:48 PM
  2. Formula with multiple IF statements and IF AND statements
    By lottidotti in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-14-2013, 08:03 AM
  3. Reading If statements and formulating values from if statements
    By crnam in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-16-2013, 05:20 AM
  4. questions with if statements and nested if statements
    By Pat Excel in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 02-03-2013, 01:41 PM
  5. Multiple nested IF statements and AND statements
    By TonyGetz in forum Excel General
    Replies: 2
    Last Post: 12-14-2010, 03:07 AM
  6. Replies: 12
    Last Post: 05-15-2009, 08:38 AM
  7. [SOLVED] operator statements, shorting when reusing one of the statements?
    By KR in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-04-2005, 02:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1