Solved

Input box prompt to specify column letter using VBA

Posted on 2016-11-24
4
13 Views
Last Modified: 2016-11-25
Dear Experts:

below code (courtesy of an EE expert) replicates data records based on numbers located in Column E, e.g. ...

the data record  where the column E contains the number '5' is replicated five times, etc.

Could somebody please tweak this macro so that the Column in which the replication factors are located ...
... is not hard coded (i.e. Column E) .

Instead, the user should be prompted via inputbox to specify that 'replication factor column'.


Help is very much appreciated.

Thank you very much in advance.


Sub y_Replicate_Data()
Dim i As Variant
Dim J As Variant
Dim k As Variant



If MsgBox("Would you like to replicate the data records based on the number of units in Column E?" & vbCrLf & vbCrLf & _
"Please make SURE ..." & vbCrLf & vbCrLf & _
"(1)... that the number of units are located in Column E and ..." & vbCrLf & _
"(2)... the article numbers are sorted in ascending orders!", vbYesNo + vbQuestion, "Replicate Data") = vbNo Then
Exit Sub
End If

UsedRange = ActiveSheet.UsedRange.Rows.Count

For i = UsedRange To 2 Step -1
    J = ActiveSheet.Cells(i, 5)
    For k = J - 1 To 1 Step -1
        Set Rng = ActiveSheet.Range("E" & i).EntireRow
        Rng.Copy
        Rng.Offset(1).Insert Shift:=xlDown
    Next
Next


End Sub

Open in new window

0
Comment
Question by:AndreasHermle
  • 2
  • 2
4 Comments
 
LVL 33

Expert Comment

by:Norie
Comment Utility
Try this.
Sub y_Replicate_Data()
Dim colLetter As String
Dim i As Variant
Dim J As Variant
Dim k As Variant



If MsgBox("Would you like to replicate the data records based on the number of units in Column E?" & vbCrLf & vbCrLf & _
"Please make SURE ..." & vbCrLf & vbCrLf & _
"(1)... that the number of units are located in Column E and ..." & vbCrLf & _
"(2)... the article numbers are sorted in ascending orders!", vbYesNo + vbQuestion, "Replicate Data") = vbNo Then
Exit Sub
End If

UsedRange = ActiveSheet.UsedRange.Rows.Count

colLetter = InputBox("Please enter replication factor column:", "Factor column","E"))

If colLetter = "" Then
    MsgBox "Operation cancelled."
End If

For i = UsedRange To 2 Step -1
    J = ActiveSheet.Cells(i, colLetter
    For k = J - 1 To 1 Step -1
        Set Rng = ActiveSheet.Range(colLetter & i).EntireRow
        Rng.Copy
        Rng.Offset(1).Insert Shift:=xlDown
    Next
Next


End Sub

Open in new window

0
 

Author Comment

by:AndreasHermle
Comment Utility
Hi Norie,

great, it works great.

Is it possible to accommodate yet another feature, ie, the user is only allowed to enter single letters (A,B,C etc.) and no numbers into the input box.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
Comment Utility
Here you are, I've fixed some errors/types that were in the original code too.
Sub y_Replicate_Data()
Dim rng As Range
Dim colLetter As String
Dim i As Long
Dim J As Long
Dim k As Long

    If MsgBox("Would you like to replicate the data records based on the number of units in Column E?" & vbCrLf & vbCrLf & _
              "Please make SURE ..." & vbCrLf & vbCrLf & _
              "(1)... that the number of units are located in Column E and ..." & vbCrLf & _
              "(2)... the article numbers are sorted in ascending orders!", vbYesNo + vbQuestion, "Replicate Data") = vbNo Then
        Exit Sub
    End If

    UsedRange = ActiveSheet.UsedRange.Rows.Count

    colLetter = InputBox("Please enter replication factor column:", "Factor column", "E")

    If colLetter = "" Then
        MsgBox "Operation cancelled."
        Exit Sub
    End If
    
     If Not UCase(colLetter) Like "[A-Z]" Then
        MsgBox "Please enter a column letter between A-Z."
        Exit Sub
     End If

    For i = UsedRange To 2 Step -1
        J = ActiveSheet.Cells(i, colLetter)
        For k = J - 1 To 1 Step -1
            Set rng = ActiveSheet.Range(colLetter & i).EntireRow
            rng.Copy
            rng.Offset(1).Insert Shift:=xlDown
        Next k
    Next i

End Sub

Open in new window

0
 

Author Closing Comment

by:AndreasHermle
Comment Utility
Hi Noorie,

great this did the trick. Thank you very much for your valuable help

Regards, Andreas
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now