Solved

Input box prompt to specify column letter using VBA

Posted on 2016-11-24
4
27 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
excel pivot question 4 42
Excel - find text within text? 1 25
NEED TO UPDATE DATA IN EXCEL 18 32
Excel 2016: auto-number column of merged cells. 47 31
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

863 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

25 Experts available now in Live!

Get 1:1 Help Now