issue with inserting Columns

bhandari

Active Member
Joined
Oct 17, 2017
Messages
359
Code:
Sub AddAColumn()
 Dim varUserInput As Variant
 Dim ColNum As Variant
 varUserInput = InputBox("Enter Column Number where you want to add a Column:", _
  "What Column?")
 If varUserInput = "" Then Exit Sub

ColNum = varUserInput
    Rows(ColNum & ":" & ColNum).Insert Shift:=xlDown
    Rows(ColNum - 1 & ":" & ColNum - 1).Copy Range("AV" & ColNum)
    Range(ColNum & ":" & ColNum).ClearContents
End Sub
Command Button 1
user need to give value in input box ,how many columns need to insert
if user give 2 in input box
2 columns need to insert after AV Column
Command Button 2
Delete Columns Through Input Box
if user give 1 column
one Column need to delete after "AV"
if user give more than 5 Columns i need to give restriction "Msg box need to pop up"
"we cant delete more than 5 columns" or any Alternative ?


Tnx In Advance
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
The code you posted is inserting rows, do you want to insert columns or rows?
 
Upvote 0
Is this what you want?
Code:
Sub addcolumns()
Dim numcol As String
numcol = InputBox("Enter number of columns to insert:", _
  "Number of Column?")
If numcol = "" Then
    MsgBox ("No value entered")
    Exit Sub
End If
Range("AW1").Resize(, numcol).EntireColumn.Insert
End Sub

Code:
Sub delcolumns()
Dim numcol As String
numcol = InputBox("Enter number of columns to deleat:", _
  "Number of Column?")
If numcol = "" Then
    MsgBox ("No value entered")
    Exit Sub
End If
If numcol + 0 > 5 Then
    MsgBox ("we cant delete more than 5 columns")
    Exit Sub
End If
Range("AW1").Resize(, numcol).EntireColumn.Delete
End Sub
 
Upvote 0
Working very Well..little bit missing in code

can you please Copy The data of "AV" Column to new inserted columns..Those are Inserting with Blank cells by using your code..

This thing is missing in code..
 
Upvote 0
Try
Code:
Sub addcolumns()
Dim numcol As String
numcol = InputBox("Enter number of columns to insert:", _
  "Number of Column?")
If numcol = "" Then
    MsgBox ("No value entered")
    Exit Sub
End If
Range("AW1").Resize(, numcol).EntireColumn.Insert
Columns(48).EntireColumn.Copy Range("AW1").Resize(, numcol)

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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