Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default insert column macro will incorrectly add another value

I have a macro that will prompt the user for number of columns to
insert. Each of the inserted columns needs to keep the same formulas,
but delete the constants. When the macro is run the formulas remain
correct however if the original column had a 5 in it the new column
will show a 6 in the same row on the new column. Example:
Column A Added column B Added Column C
=sum(A2:A5) =sum(A2:A5) =sum(A2:A5)
1 2 3
2 3 4
3 4 5

If anyone could determine why it is showing the constants + 1 and
revise the code I would appreciate it, or has a better way to achieve
the same objective.

Sub InsertColumnsAndFillFormulas(Optional vColumn As Long = 0)
Dim x As Long
ActiveCell.EntireColumn.Select 'So you do not have to preselect
entire row
If vColumns = 0 Then
vColumns = Application.InputBox(prompt:= _
"How many columns do you want to add?", Title:="Add Columns", _
Default:=1, Type:=1) 'Default for 1 row, type 1 is number
If vColumns = False Then Exit Sub
End If

Dim sht As Worksheet, shts() As String, i As Long
ReDim shts(1 To Worksheets.Application.ActiveWorkbook. _
Windows(1).SelectedSheets.Count)
i = 0
For Each sht In _
Application.ActiveWorkbook.Windows(1).SelectedShee ts
Sheets(sht.Name).Select
i = i + 1
shts(i) = sht.Name

x = Sheets(sht.Name).UsedRange.Columns.Count 'lastcell fixup

Selection.Resize(columnsize:=2).Columns(2).EntireC olumn. _
Resize(columnsize:=vColumns).Insert Shift:=xlToRight

Next sht
Worksheets(shts).Select
End Sub

Thanks for all help!

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default insert column macro will incorrectly add another value

First, when I ran your code, the inserted columns were empty.

I tried toggling tools|Options|edit tab|Extend data range formats and formulas,
but that didn't change anything for me. The inserted columns were still empty.

But....

If you record a macro when you do this:

Select the inserted columns
edit|goto|special|check constants
then hit the delete key on the keyboard

You'll have code that clears those constants.

wrote:

I have a macro that will prompt the user for number of columns to
insert. Each of the inserted columns needs to keep the same formulas,
but delete the constants. When the macro is run the formulas remain
correct however if the original column had a 5 in it the new column
will show a 6 in the same row on the new column. Example:
Column A Added column B Added Column C
=sum(A2:A5) =sum(A2:A5) =sum(A2:A5)
1 2 3
2 3 4
3 4 5

If anyone could determine why it is showing the constants + 1 and
revise the code I would appreciate it, or has a better way to achieve
the same objective.

Sub InsertColumnsAndFillFormulas(Optional vColumn As Long = 0)
Dim x As Long
ActiveCell.EntireColumn.Select 'So you do not have to preselect
entire row
If vColumns = 0 Then
vColumns = Application.InputBox(prompt:= _
"How many columns do you want to add?", Title:="Add Columns", _
Default:=1, Type:=1) 'Default for 1 row, type 1 is number
If vColumns = False Then Exit Sub
End If

Dim sht As Worksheet, shts() As String, i As Long
ReDim shts(1 To Worksheets.Application.ActiveWorkbook. _
Windows(1).SelectedSheets.Count)
i = 0
For Each sht In _
Application.ActiveWorkbook.Windows(1).SelectedShee ts
Sheets(sht.Name).Select
i = i + 1
shts(i) = sht.Name

x = Sheets(sht.Name).UsedRange.Columns.Count 'lastcell fixup

Selection.Resize(columnsize:=2).Columns(2).EntireC olumn. _
Resize(columnsize:=vColumns).Insert Shift:=xlToRight

Next sht
Worksheets(shts).Select
End Sub

Thanks for all help!


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Macro to Insert the contents of one column to other Dhawal Excel Discussion (Misc queries) 5 October 5th 06 01:22 PM
Trying to insert a column using variable in a Macro MackPetD Excel Discussion (Misc queries) 1 July 25th 06 08:14 AM
Why does a column add incorrectly when usng autosum? cindy sue Excel Worksheet Functions 1 June 7th 06 01:21 AM
Dynamic column insert (macro) Chris Excel Discussion (Misc queries) 2 March 15th 06 11:33 AM
Column sorting incorrectly Melissa Excel Discussion (Misc queries) 18 October 13th 05 08:45 AM


All times are GMT +1. The time now is 08:52 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"