Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Input box prompt to specify column letter using VBA

Posted on 2016-11-24
4
Medium Priority
?
145 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 35

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 35

Accepted Solution

by:
Norie earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

916 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