Solved

Input box prompt to specify column letter using VBA

Posted on 2016-11-24
4
83 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
[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
  • 2
  • 2
4 Comments
 
LVL 34

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 34

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

617 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