My couters are not recognized in IF STATEMENT

andreroux

New Member
Joined
Jun 2, 2013
Messages
4
Hi All

I have a macro with 3 counters: STRT_NM1, START_ROW, COUNTER. I run a For...Next loop to execute a sub macro called DoWork. At the end of the DoWork Sub Macro, I display my counters to see if they are incremented, and they are incremented as I expect. Yet, In an IF statement, I want to select a cell by using the statement Range("D" & STRT_NM1).Select where cells D2, D3, D4, etc must be selected. For some strange reason, it keeps selecting D2, as if the counter STRT_NM1 remains at value 2, but my display at the end of the routine shows different. Any Help? Is there another way I can select the cells other than using Range("D" & STRT_NM1).Select?

Code:
Option Explicit
Sub Macro1()
'
' Macro1 Macro
'
' ============================
' Define Variables & Populate them
' ============================
   Dim fname As String
   Dim STRT_NM1 As Long
   Dim START_ROW As Long
   Dim INVCNT As Long
   Dim COUNTER As Long
   Dim ws1 As Worksheet
   STRT_NM1 = 1
   START_ROW = 6
   COUNTER = 1
   
' ==================================================
' Copy First Invoice Detail to Invoice Control Sheet
' ==================================================
        Workbooks.Open Filename:= _
        "C:\Users\E-Machine\Dropbox\Technology Station - VUT\Master Invoice TS.xlsm"
        Sheets("Invoices").Select
        Range("D" & STRT_NM1).Select
        INVCNT = Range("C2").Value
    For COUNTER = 1 To INVCNT
        DoWork START_ROW, STRT_NM1, COUNTER
    Next COUNTER
End Sub
Sub DoWork(ByVal START_ROW As Long, ByVal STRT_NM1 As Long, COUNTER As Long)
    If START_ROW > 0 Then
        START_ROW = START_ROW + 1
        STRT_NM1 = STRT_NM1 + 1
    Else
        START_ROW = 7
        STRT_NM1 = 2
    End If
        Windows("Master Invoice TS.xlsm").Activate
        Sheets("INVOICES").Select
        Range("D" & STRT_NM1).Select
        Dim fname As String
        fname = "C:\Users\E-Machine\Dropbox\Technology Station - VUT\Invoices\" & ActiveCell.Value
        Workbooks.Open fname
        Cells.Select
        Selection.Copy
        ActiveWindow.Close
        Windows("Invoice Control Sheet.xlsm").Activate
'        Sheets.Add After:=ActiveSheet
        Sheets("Sheet2").Select
        ActiveSheet.Paste
        ActiveSheet.Range("I1:J1").Select
        Selection.Copy
        Sheets("Sheet1").Select
        Range("J" & START_ROW).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
           :=False, Transpose:=False
        Sheets("Sheet2").Select
        Range("I2:J2").Select
        Application.CutCopyMode = False
        Selection.Copy
'        Application.WindowState = xlMinimized
        Windows("Master Invoice TS.xlsm").Activate
        Application.WindowState = xlNormal
        Windows("Invoice Control Sheet.xlsm").Activate
        Sheets("Sheet1").Select
        Range("B" & START_ROW).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Sheets("Sheet2").Select
        Range("I3:J3").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet1").Select
        Range("C" & START_ROW).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
           :=False, Transpose:=False
        Sheets("Sheet2").Select
        Range("B5:D5").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet1").Select
        Range("D" & START_ROW).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Sheets("Sheet2").Select
        Range("I4:J4").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet1").Select
        Range("G" & START_ROW).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
           :=False, Transpose:=False
        Sheets("Sheet2").Select
        Range("I21:J21").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet1").Select
        Range("K" & START_ROW).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
             :=False, Transpose:=False
        Range("K" & START_ROW).Select
        Application.CutCopyMode = False
        Selection.Style = "Comma"
        Sheets("Sheet2").Select
'        ActiveWindow.SelectedSheets.Delete
        Cells.Select
        Selection.ClearContents
        Windows("Invoice Control Sheet.xlsm").Activate
        Application.WindowState = xlNormal
        STRT_NM1 = STRT_NM1 + 1
       START_ROW = START_ROW + 1
        Range("E1").Value = START_ROW
        Range("E2").Value = STRT_NM1
        Range("E3").Value = COUNTER
        Sheets("Sheet1").Select
  
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
with Range("D" & STRT_NM1).Select you select only one cell, not a range of cells, you have to use Range("D2:D" & STRT_NM1).Select
 
Upvote 0
Hi All

I just need to say a sincere THANK YOU for all the help I received from the people in this forum. It certainly pointed me in the right direction. I think you guys are AWESOME!

André Roux
 
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,418
Members
449,449
Latest member
Quiet_Nectarine_

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