+ Reply to Thread
Results 1 to 4 of 4

No errors, but not receiving desired result

  1. #1
    Registered User
    Join Date
    08-17-2006
    Posts
    19

    No errors, but not receiving desired result

    I am quite new to VB but have been able to learn a lot from this forum along the way.

    Here is my code:

    Private Sub CommandButton6_Click()

    'Select the entire Capitla View Forecast sheet and delete all contents to start empty

    Sheets("Capital View Forecast").Select
    Sheets("Capital View Forecast").Cells.ClearContents

    'Select the range of values from the Master List tab that is needed on the Capital View Forecast tab and paste special the values onto the Capital View Forecast Tab.


    Sheets("Master List").Select
    Sheets("Master List").Range("A2:K1000").Select
    Selection.Copy
    Sheets("Capital View Forecast").Select
    Sheets("Capital View Forecast").Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    ActiveSheet.Paste
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

    'Delete unused columns on the Capital View Forecast Tab

    Sheets("Capital View Forecast").Select
    Sheets("Capital View Forecast").Range("C:C,D:D,F:F,G:G,H:H,I:I,J:J").Select
    Selection.Delete

    'Rearrange columns on Capital View Forecast tab to match desired format

    Sheets("Capital View Forecast").Select
    Sheets("Capital View Forecast").Columns("D:D").Select
    Selection.Cut
    Sheets("Capital View Forecast").Columns("A:A").Select
    Selection.Insert Shift:=xlToRight

    Sheets("Capital View Forecast").Select
    Sheets("Capital View Forecast").Columns("C:C").Select
    Selection.Cut
    Sheets("Capital View Forecast").Columns("B:B").Select
    Selection.Insert Shift:=xlToRight

    'Set active cell as D2 before starting Loop

    Sheets("Capital View Forecast").Range("D2").Select


    'Run loop that deletes the entire row if the value in the Capitalized Licenses column is blank

    Do

    If ActiveCell.Value = 0 Then
    Selection.EntireRow.Delete
    End If

    'If there is a value in the Capitalized Licenses column then leave unchanged and move to next row below

    If ActiveCell.Value <> 0 Then
    ActiveCell.Offset(1, 0).Activate
    End If

    'Loop until there is no value in the Supplier column

    Loop Until IsEmpty(ActiveCell.Offset(0, -1))


    'Move back to cell A2

    Sheets("Capital View Forecast").Select
    Sheets("Capital View Forecast").Range("A2").Activate

    'Copy and Paste the columns for quarter values from other tab

    Sheets("OLD Capital View Forecast").Select
    Sheets("OLD Capital View Forecast").Range("E1:O1").Select
    Selection.Copy

    Sheets("Capital View Forecast").Select
    Sheets("Capital View Forecast").Range("E1").Select

    Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    ActiveSheet.Paste
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

    'Move to cell A2 and Begin 2nd Loop to move Capitalized Licenses Value to respective Quarter column

    Sheets("Capital View Forecast").Select
    Sheets("Capital View Forecast").Range("A2").Activate

    Do

    If ActiveCell.Value = Q406 Then
    ActiveCell.Offset(0, 4).Select
    ActiveCell.FormulaR1C1 = "=RC[-1]"
    ActiveCell.Offset(0, -4).Activate
    End If

    If ActiveCell.Value = Q107 Then
    ActiveCell.Offset(0, 5).Select
    ActiveCell.FormulaR1C1 = "=RC[-2]"
    ActiveCell.Offset(0, -5).Activate
    End If

    If ActiveCell.Value = Q207 Then
    ActiveCell.Offset(0, 6).Select
    ActiveCell.FormulaR1C1 = "=RC[-3]"
    ActiveCell.Offset(0, -6).Activate
    End If



    ActiveCell.Offset(1, 0).Select

    Loop Until IsEmpty(ActiveCell.Offset(0, 0))

    End Sub
    Everything works as I expected except towards the end (Red Font) where I am trying to enter dollar amounts from the same tab to their respective fiscal quarter. It seems pretty straightforward but for some reason I end up with no dollar amounts in any of the fiscal quarter columns.

    Is there a problem with having a 2nd "Do Loop" in the same macro?

  2. #2
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    The problem may be caused by your assignments of which I have plucked one:
    ActiveCell.Value = Q406 should be modified as
    ActiveCell.Value = "Q406". Note the use of double quotation marks enclosing the string Q406.

    On a different note, be informed that in vba, you do not need to select an item before copying. The Macro Recorder's (MR) does not observe this principle. You can therefore improve upon the Recorder's output by learning the proper coding habits-using appropriate syntax other than the verbose heap churned out by the MR. Your code, thus trimmed is bound to work more efficiently.
    Last edited by Myles; 08-25-2006 at 04:17 PM.

  3. #3
    Registered User
    Join Date
    08-17-2006
    Posts
    19
    We can close the case on this one. I figured it out after a lot of failed attempts.

  4. #4
    Registered User
    Join Date
    08-17-2006
    Posts
    19
    Quote Originally Posted by Myles
    The problem may be caused by your assignments of which I have plucked one:
    ActiveCell.Value = Q406 should be modified as
    ActiveCell.Value = "Q406". Note the use of double quotation marks enclosing the string Q406.

    On a different note, be informed that in vba, you do not need to select and item before copying -as the Macro Recorder's (MR) does. You can improve upon the Recorder's output by learning the proper coding habits-using appropriate syntax other than the verbose heap churned out by the MR. Your code, thus trimmed is bound to work more efficiently.

    Thank you for the feedback. I know there is a lot of room to trim down my code and I keep getting better at that each time I try a new function. Input from this forum has been much better than the VBA help menus...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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