Solved

Excel VBA code assist

Posted on 2014-12-06
9
237 Views
Last Modified: 2014-12-06
I have a procedure that "closes" a section...

I call this procedure via the line below:

zzEndSection wksReport1.Range("A5")...

The procedure starts as follows:

Private Sub zzEndSection(theRange As Range, theSection As String, theOffset As Integer, theStartRow As Integer)

theRange.Offset(theOffset, 0).EntireRow.Insert             <<<<<<<<<<<<<<< Line 1
theRange.Offset(theOffset, 1) = theSection & " Total"   <<<<<<<<<<<<<<< Line 2

If I check the value of "theRange" in line 1 and in line 2 "theRange" is "A5".

I have a starting procedure that is called via:

zzStartSection wksReport1.Range("A5")...

The procedure starts as follows:

Private Sub zzStartSection(theRange As Range, theOffset As Integer, theVerbiage As String)

theRange.Offset(theOffset, 0).EntireRow.Insert   <<<<<<<<<<<<<<< Line 1a
theRange.Offset(theOffset, 0).EntireRow.Select  <<<<<<<<<<<<<<<  Line 2a

When checking values, "theRange" in Line 1a is "A5" yet in Line 2a it is "A6"

Any idea why the starting and ending procedures don't treat the range the same?
0
Comment
Question by:classnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40484828
Are you sure? Because when I do this and run Sub x, where A5 on my Sheet1 is "test", 'theRange' in both statements in the sub shows "test"

Sub x()
zzStartSection Sheets("Sheet1").Range("A5")
End Sub
Private Sub zzStartSection(theRange As Range)
Const theOffset = 1
theRange.Offset(theOffset, 0).EntireRow.Insert '  <<<<<<<<<<<<<<< Line 1a
theRange.Offset(theOffset, 0).EntireRow.Select ' <<<<<<<<<<<<<<<  Line 2a
End Sub

Open in new window

0
 
LVL 33

Expert Comment

by:Norie
ID: 40484830
What is the theOffset when you are calling each sub?
0
 

Author Comment

by:classnet
ID: 40484832
I'm going to post both calling statements and procedures:

Calling segment:
        'End the Status
        zzEndSection wksReport1.Range("A5"), currentStatus, currentRowOffset, startSubTotalRowCategory
        
        currentRowOffset = currentRowOffset + 1
        currentStatus = wksReport1.Range("A5").Offset(currentRowOffset, statusColumn - 1)

      End If
      currentStatus = wksReport1.Range("A5").Offset(currentRowOffset, statusColumn - 1)
      startSubTotalRowStatus = wksReport1.Range("A5").Offset(currentRowOffset, 0).Row
      
      zzStartSection wksReport1.Range("A5"), currentRowOffset, "Status: " & currentStatus

Open in new window


End session
Private Sub zzEndSection(theRange As Range, theSection As String, theOffset As Integer, theStartRow As Integer)

theRange.Offset(theOffset, 0).EntireRow.Insert
theRange.Offset(theOffset, 1) = theSection & " Total"
theRange.Offset(theOffset, 1).HorizontalAlignment = xlRight
theRange.Offset(theOffset, 1).Font.Bold = True
theRange.Offset(theOffset, 2) = "=SUBTOTAL(9,C" & theStartRow & ":C" & theRange.Offset(theOffset, 1).Row - 1 & ")"

Open in new window


Start session:

Private Sub zzStartSection(theRange As Range, theOffset As Integer, theVerbiage As String)

theRange.Offset(theOffset, 0).EntireRow.Insert
theRange.Offset(theOffset, 0).EntireRow.Select

Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone

Open in new window


Am I overlooking something?
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 45

Expert Comment

by:aikimark
ID: 40484833
since the insert creates a new line before the current line, Excel moves everything down.  If your parameter was on A5 (row 5), the data in the cells on that row are now located on row 5
0
 

Author Comment

by:classnet
ID: 40484836
aikimark... why doesn't the range change in both procedures?
0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 40484837
I can only replicate the behaviour you describe if theOffset is 0, and it happens in both subs.
0
 

Author Comment

by:classnet
ID: 40484842
It appears that "theRange" does NOT change unless the offset = 0...  I'll put some code in place to catch this...
0
 

Author Closing Comment

by:classnet
ID: 40484843
Thanks for the catch...
0
 

Author Comment

by:classnet
ID: 40484847
I simply changed all occurrences of A5 to A4 and started the offset at 1... all is well.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question