Solved

Input box prompt to specify column letter using VBA

Posted on 2016-11-24
4
37 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
ID: 41901211
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
ID: 41901246
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
ID: 41901259
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
ID: 41901642
Hi Noorie,

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

Regards, Andreas
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

831 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