I have a working code to ask user for:
- a product
- what column to put it in
- quantity of product
All this happens based on customers region and checkboxes. (Checked regions will get data from user input)
Code is working fine in columns from C all the way to Z. But when I try to change the code to go all the way to "BU" it will always tell me that i have chosen a column not available to use.
Is there a way I can change "Z" to "BU" without making the macro fail?
here is my code: (from C to Z working fine...)
- a product
- what column to put it in
- quantity of product
All this happens based on customers region and checkboxes. (Checked regions will get data from user input)
Code is working fine in columns from C all the way to Z. But when I try to change the code to go all the way to "BU" it will always tell me that i have chosen a column not available to use.
Is there a way I can change "Z" to "BU" without making the macro fail?
here is my code: (from C to Z working fine...)
VBA Code:
Sub MyPersonalNightmare()
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
' Variables to store values
Dim Vareslag As String
Dim Kolonne As String
Dim Antall As String
' Spør brukeren om hva de skal lage manko på
Vareslag = InputBox("Name on product", "Product Information", "Posters")
' Sjekk om brukeren har angitt tekst
If Vareslag = "" Then
MsgBox "No Product Information provided", vbExclamation, "Product Info"
Exit Sub
End If
Plassering:
' Ask user what column to place product in
Kolonne = InputBox("Choose column (C to BU)", "Product Information")
' Validate that it is a valid column
If Kolonne = "" Or (UCase(Kolonne) < "C" Or UCase(Kolonne) > "BU") Then
MsgBox "Unavailable Column choosen, it needs to be between C and BU.", vbExclamation, "Product Information"
GoTo Plassering
End If
' Find column based on user choice
Dim targetCol As Integer
targetCol = Columns(Kolonne).Column
' check if column is in use already
If Not IsEmpty(ws.Cells(9, targetCol)) Then
Dim alternativKolonne As String
alternativKolonne = InputBox("This Column is currently unavailable (Already in use) Choose another Column (C til BU):", "Product Information")
' Validate that new user defined column is valid
If alternativKolonne = "" Or (UCase(alternativKolonne) < "C" Or UCase(alternativKolonne) > "BU") Then
MsgBox "Unavailable Column chosen (Needs to be between C and BU.", vbExclamation, "Product Information"
GoTo Plassering
End If
targetCol = Columns(alternativKolonne).Column
End If
' Ask user of product quantity
Antall = InputBox("How many units to chossen customers?", "Product Information", "1")
' Validate that user makes an input
If Antall = "" Then
MsgBox "Input is not valid, Exiting procedure", vbExclamation, "Product Information"
Exit Sub
End If
' Make headline for product in list
ws.Cells(9, targetCol).Value = Vareslag
' Find quantity of product from user value
Dim antallManglendeVarer As Integer
antallManglendeVarer = CInt(Antall)
' Loop through list an put quantity on chosen customers in area BW12:BW308
Dim cell As Range
For Each cell In ws.Range("BW12:BW308")
If cell.Value = "VISES" Then
' Find right column to put quantity in
ws.Cells(cell.Row, targetCol).Value = antallManglendeVarer
End If
Next cell
MsgBox "Routine completed succesfully", vbInformation
End Sub