Excel VBA - Make Textbox input optional - vba

My Problem is the following:
Userform visualization for understanding
(1)
I have a combobx "CGselectionStrategies" that should be the basis for the Input textboxes below. When the userform is started, I would like it to show the previous input for these boxes, depending on the Combobox selection.
The Input is saved in the worksheet "Commodity Groups" with the following code:
Private Sub SaveCGStrategies_Click()
'Just general stuff
Dim outputBook As Workbook
Set outputBook = ActiveWorkbook
'Note-fields for PU Strategies, incl. Authors
Dim CGselectionStrategies As String
Dim NoteTargetMarket As String
Dim AuthorTargetMarket As String
Dim NotePUMStrategy As String
Dim AuthorPUMStrategy As String
Dim NotePUSStrategy As String
Dim AuthorPUSStrategy As String
Dim NotePULStrategy As String
Dim AuthorPULStrategy As String
CGselectionStrategies = Me.CGselectionStrategies
NoteTargetMarket = Me.NoteTargetMarket
AuthorTargetMarket = Me.NoteAuthorMarketInfo
NotePUMStrategy = Me.NotePUMStrat
AuthorPUMStrategy = Me.NoteAuthorPUMStratInfo
NotePUSStrategy = Me.NotePUSStrat
AuthorPUSStrategy = Me.NoteAuthorPUSStratInfo
NotePULStrategy = Me.NotePULStrat
AuthorPULStrategy = Me.NoteAuthorPULStratInfo
'Save CG Strategies behind them in the List on CG Worksheet
outputBook.Activate
outputBook.Worksheets("Commodity Groups").Select
With Me.CGselectionStrategies
If Me.CGselectionStrategies.value = "Halbzeuge (und Rohstoffe)" Then
Range("K2").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Mechanische Konstruktionsteile" Then
Range("K62").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Norm- und Katalogteile (ausser Elektro)" Then
Range("K87").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Elektrische, elektronische und optische Komponenten und Baugruppen" Then
Range("K127").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Hilfs-, Betriebs- und Produktionshifsmittel" Then
Range("K180").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Subsysteme und Anlagen" Then
Range("K256").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Handelsware" Then
Range("K299").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Dienstleistungen" Then
Range("K310").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Allgemeines und Administration" Then
Range("K360").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
End With
End Sub
My Approach was the following...
'Show old Strategies when selecting a combobox-item
'Start with short Text "Please choose a Commodity Group"
If Me.CGselectionStrategies.value = "" Then
Me.NoteTargetMarket.Text = CStr(ThisWorkbook.Sheets("Commodity Groups").Range("K445").value)
Me.Next Variable
Me.Next Variable
Me.Next Variable
End If
If Me.CGselectionStrategies.value = "Halbzeuge (und Rohstoffe)" Then
Me.NoteTargetMarket.Text = CStr(ThisWorkbook.Sheets("Commodity Groups").Range("K2").value)
Me.Next Variable
Me.Next Variable
Me.Next Variable
End If
...and so on. Needless to say it does not work. I found the following online and tried to adapt it to the best of my abilities, but without success.
'Change Textbot Content based on Combobox selection
Dim wks As Excel.Worksheet
Dim selectedString As Variant
Dim row As Long
Dim value As Variant
Set wks = Worksheets("Commodity Groups")
If CGselectionStrategies.ListIndex <> -1 Then
selectedString = CGselectionStrategies.value
On Error Resume Next
row = Application.WorksheetFunction.Match(selectedString, wks.Columns(1), 0)
On Error GoTo 0
If row Then
value = wks.Cells(row, 2)
DomainOwnerTestBox.value = value
Else
'Value not found in the worksheet 'test'
End If
End If
End Sub
One Problem was also that there are multiple Input values, not only in column 2, which are also separated by many other rows.
I hope my poblem is explained in an understandable manner.
(2)
My second poblem, which is way shorter, is regarding how to avoid having to fill in all textboxes in a userform. The one is question has over 200 Inputs to fill out and whenever I want to test i.e. the positioning of the Input in the database, I get a runtime 13 mistake "Type mismatch." However, if I put an Input in every box, it runs through smoothly. Here a code excerpt how I save my data from the userform Input:
Dim Datum As Date
Dim SName As String
Dim PotentialS As String
Dim SuppNr As Long
Dim Active As String
Datum = Me.TextBox117
SName = Me.SuppName
PotentialS = Me.PotentialS
SuppNr = Me.SuppNo
Active = Me.Active
'Go to the first empty line on the output sheet (Meta DB) in this workbook
outputBook.Activate
outputBook.Worksheets("Meta DB").Range("A3").Select
If outputBook.Worksheets("Meta DB").Range("A3").Offset(1, 0) <> "" Then
outputBook.Worksheets("Meta DB").Range("A3").End(xlDown).Select
End If
'Go to A4 and from there always one below the last filled cell in A
ActiveCell.Offset(1, 0).Select
DatabaseRow = ActiveCell.row
'Post Values for new Entry
'Add a New Supplier Tab - Supplier Profile
ActiveCell.value = Datum
ActiveCell.Offset(0, 1).Select
ActiveCell.value = SName
ActiveCell.Offset(0, 1).Select
ActiveCell.value = PotentialS
ActiveCell.Offset(0, 1).Select
ActiveCell.value = SuppNr
ActiveCell.Offset(0, 1).Select
Any help and tips are welcome.

Firstly I think shortening the SaveCGStrategies_Click code will help understand VBA a little better, what you have done is one by one check every option to save the values, but consider the first option was selected, then you would never need to check the others as you would have found your match, the code is also duplicated each time, the below checks selection and runs a single instance of the code once but against the relevant cells.
Private Sub SaveCGStrategies_Click()
Dim LngRow As Long
Dim outputBook As Workbook
Dim outputSheet As Worksheet
Set outputBook = ActiveWorkbook
Set outputSheet = outputBook.Worksheets("Commodity Groups")
'With Me.CGselectionStrategies
Select Case Me.CGselectionStrategies.Value
Case "Halbzeuge (und Rohstoffe)"
LngRow = 2
Case "Mechanische Konstruktionsteile"
LngRow = 62
Case "Norm- und Katalogteile (ausser Elektro)"
LngRow = 87
Case "Elektrische, elektronische und optische Komponenten und Baugruppen"
LngRow = 127
Case "Hilfs-, Betriebs- und Produktionshifsmittel"
LngRow = 180
Case "Subsysteme und Anlagen"
LngRow = 256
Case "Handelsware"
LngRow = 299
Case "Dienstleistungen"
LngRow = 310
Case "Allgemeines und Administration"
LngRow = 360
End Select
outputSheet.Cells(LngRow, 11) = Me.NoteTargetMarket
outputSheet.Cells(LngRow, 13) = Me.NoteAuthorMarketInfo
outputSheet.Cells(LngRow, 14) = Me.NotePUMStrat
outputSheet.Cells(LngRow, 15) = Me.NoteAuthorPUMStratInfo
outputSheet.Cells(LngRow, 16) = Me.NotePUSStrat
outputSheet.Cells(LngRow, 17) = Me.NoteAuthorPUSStratInfo
outputSheet.Cells(LngRow, 18) = Me.NotePULStrat
outputSheet.Cells(LngRow, 19) = Me.NoteAuthorPULStratInfo
Set outputSheet = Nothing
Set outputBook = Nothing
End Sub
In the same way you referenced the workbook, it also references the worksheet to enable us to write into the ranges of the worksheet that we want to with less code. I have not used the .Select and Activate functions that you had as these can have performance issues. I have also referenced the values directly and not placed them in a variable first, if you were planning to manipulate them prior to writing them to a cell then a variable may be of use but if it is a straight insert from textbox to cell, we can pass it straight through.
Your second issue needs more input to be certain but I suspect relates data types.
Dim Datum As Date
Datum = Me.TextBox117
Is Me.TextBox117 a date in a valid date format? this could be checked as below: -
If IsDate(Me.TextBox117) then Datum = CDate(Me.TextBox117)
The functionCDate ensure the value is passed into the variable as a date.
Dim SuppNr As Long
SuppNr = Me.SuppNo
Is Me.SuppNo a valid number? this could be checked as below: -
If IsNumeric(Me.SuppNo) then SuppNr = CLng(Me.SuppNo)
My recommendation would be while getting it work you set them all to String or pass them through as is.

Hi one thing is for certain - you need to get rid of all of these .Select's
They make it incredibly hard to read. I myself have only just started learning VBA too (about 3 weeks ago). Please look at the following link - How to avoid using Select in Excel VBA macros . It should help you with your VBA readability. It will also make your code about a gazillion times faster.
This will also help you from having to use ActiveCell every other command.
Another tip is instead of you having
Dim *StringVariable* as string
as a line of code that you start off with at the beginning -
if you want to name a cell something, just give its range and then make it = to "String" e.g.
Range("A1") = "This is a String"
I am not experienced enough with VBA to know what is the matter with your TextBox, but i hope this is a good start to aid your general VBA writing.

Related

Transfer information to a different sheet in workbook and allow for continous updating and formatting

I have a data entry page which feeds into a master using a VBA code & command button. I need to 1. either have the new information feed to the top of the master list without an additional row already being in place or 2. need the information to filter into separate tabs by region with the most recent listed at top. The problem I'm running into is how to make the tabs for each region update automatically from the master sheet. All of this is in one workbook.
VBA Code
Private Sub CommandButton1_Click()
Dim ProjectNumber As String
Dim Region As String
Dim Customer As String
Dim PlantName As String
Dim State As String
Dim PlantNumber As String
Dim Contact As String
Dim PhoneNumber As String
Dim Scope As String
Dim Value As String
Dim Year As String
Dim MW As String
Dim MFG As String
Worksheets("Entry").Select
ProjectNumber = Range("ProjectNumber")
Region = Range("Region")
Customer = Range("Customer")
PlantName = Range("PlantName")
State = Range("State")
PlantNumber = Range("PlantNumber")
Contact = Range("Contact")
PhoneNumber = Range("PhoneNumber")
Scope = Range("Scope")
Value = Range("Value")
Year = Range("Year")
MW = Range("MW")
MFG = Range("MFG")
Worksheets("Experience").Select
Worksheets("Experience").Range("A441").Select
If Worksheets("Experience").Range("A441").Offset(1, 0) <> "" Then
Worksheets("Experience").Range("A441").End(xlDown).Select
End If
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = ProjectNumber
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Region
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Customer
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = PlantName
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = State
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = PlantNumber
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Contact
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = PhoneNumber
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Scope
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Value
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Year
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = MW
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = MFG
Worksheets("Entry").Select
Worksheets("Entry").Range("C6").Select
End Sub
The following inserts a new blank row immediately below row 441 and stuffs the 13 string values into the new row's columns A:M.
Private Sub CommandButton1_Click()
Dim vSTRs As Variant
With Worksheets("Entry")
vSTRs = Array(.Range("ProjectNumber").Value2, .Range("Region").Value2, _
.Range("Customer").Value2, .Range("PlantName").Value2, _
.Range("State").Value2, .Range("PlantNumber").Value2, _
.Range("Contact").Value2, .Range("PhoneNumber").Value2, _
.Range("Scope").Value2, .Range("Value").Value2, _
.Range("Year").Value2, .Range("MW").Value2, .Range("MFG").Value2)
End With
With Worksheets("Experience")
.Cells(442, "A").EntireRow.Insert
.Cells(442, "A").Resize(1, UBound(vSTRs) + 1) = vSTRs
End With
End Sub
This should fulfill your first option.
See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.

Error message if no data entered in text box

Please suggest a vb code, that if the text box is left blank,and during TAB/Enter, the Error message box appears for each text box.
Private Sub CommandButton1_Click()
Sheets("Attendance").Select
Range("a1").Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveCell.Value = Me.d.Value
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Me.N.Value
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Me.Salary.Value
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Me.Remarks.Value
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Me.IT.Value
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Me.Outtime.Value
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Me.Lunch.Value
ActiveCell.Offset(0, 3).Select
ActiveCell.Value = Me.Advance.Value
ActiveCell.Offset(0, 2).Select
ActiveCell.Value = Me.Paid.Value
End Sub
Please see below sub carefully and try to do to your user form.
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Me.TextBox1 = "" Then
MsgBox "You must enter value!"
Cancel = True
Me.TextBox1.SetFocus
End If
End Sub

Check if a cell contains specific text

I have to put jobs from one spreadsheet onto another in their priority order. If a job is listed as completed, then I do not transfer that job over. Below is my code for the top priority, "priority 1". The cell that states it's completion status sometimes has a date before or after it, which is why I put the "*" character.
Do Until IsEmpty(ActiveCell) Or count > 14
If ActiveCell.Value = "Priority I" Then
ActiveCell.Offset(0, 6).Select
If ActiveCell.value = "completed" like "*completed*" Then
ActiveCell.Offset(1, -6).Select
Else
ActiveCell.Offset(0, -1).Select
word0 = ActiveCell.Value
ActiveWindow.ActivateNext
ActiveCell = word0
ActiveWindow.ActivateNext
ActiveCell.Offset(0, -9).Select
word = Left(ActiveCell.Value, 6)
ThisWorkbook.Activate
ActiveCell.Offset(0, 1).Select
ActiveCell = word
ActiveWindow.ActivateNext
ActiveCell.Offset(0, 1).Select
word1 = ActiveCell.Value
ThisWorkbook.Activate
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = word1
ActiveWindow.ActivateNext
ActiveCell.Offset(0, 1).Select
word2 = ActiveCell.Value
ThisWorkbook.Activate
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = word2
ActiveWindow.ActivateNext
ActiveCell.Offset(0, 1).Select
word3 = ActiveCell.Value
ThisWorkbook.Activate
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = word3
ActiveCell.Offset(1, -4).Select
ActiveWindow.ActivateNext
ActiveCell.Offset(1, 1).Select
count = count + 1
End If
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
I have confirmed that it is checking the correct column, it just doesn't catch the word completed. So the problem resides within that line, line 4.
Change
If ActiveCell.value = "completed" like "*completed*" Then
to
If Instr(1, UCase(ActiveCell.Value), "COMPLETED") > 0 Then
or
If UCase(ActiveCell.Value) like "*COMPLETED*" Then

autofill data from a sheet to a sheet named after a current month

I want to track the daily, monthly records of my shop.
My first sheet is where I record all the earnings for the day. I made an update button that transfers all of the data collected at the end of the day to the next worksheets, which are named after the months.
I successfully made it on the month of July because I just typed in "July" to select the worksheet where the data will be transferred.
But what I want to get is automatically selecting the worksheet based on the current month, where the next worksheets are named after.
I tried
worksheets(format(now,"mmm")).select
but I'm getting an error stating that I am out of range
here's my whole code:
Private Sub CommandButton1_Click()
If MsgBox("Are you sure?", vbYesNo, "Title") = vbYes Then
Dim CtrlCDate As String, CtrlCPSQty As Integer, CtrlCPSAmt As Integer, CtrlCPLQty As Integer, CtrlCPLAmt As Integer, CtrlCPQty As Integer, CtrlCPamt As Integer, CtrlCSQty As Integer, CtrlCSAmt As Integer, TC As Integer, TL As Integer, TS As Integer
Worksheets("CTRLC Ops").Select
CtrlCDate = Range("N3")
CtrlCPSQty = Range("N6")
CtrlCPSAmt = Range("O6")
CtrlCPLQty = Range("N7")
CtrlCPLAmt = Range("O7")
CtrlCPQty = Range("N8")
CtrlCPamt = Range("O8")
CtrlCSQty = Range("N9")
CtrlCSAmt = Range("O9")
TC = Range("O10")
TL = Range("O11")
TS = Range("N13")
Worksheets("July").Select
Worksheets("July").Range("A3").Select
If Worksheets("July").Range("A3").Offset(1, 0) <> "" Then
Worksheets("July").Range("A3").End(xlDown).Select
End If
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = CtrlCDate
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = CtrlCPSQty
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = CtrlCPSAmt
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = CtrlCPLQty
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = CtrlCPLAmt
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = CtrlCPQty
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = CtrlCPamt
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = CtrlCSQty
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = CtrlCSAmt
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = TC
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = TL
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = TS
Worksheets("CTRLC Ops").Select
Worksheets("CTRLC Ops").Range("D1").Select
End If
End Sub
Worksheets(Format(now,"mmm")).Select will select a worksheet called "Jul". Is that the name of the sheet, or is it July?
"mmm" will return the first three letters of the month, "mmmm" will return the full month name.
So if you want to select worksheet "July", then use Worksheets(Format(now,"mmmm")).Select

Run a macro on a selection of cells

I wrote the following macro to help me on a VLOOKUP repetitive action.
It works, but I can't manage to run it on several cells at the same time.
I guess there's a code to write at the beginning of the macro.
Help much appreciated ;-)
Sub Croisement_ZANOX_BO()
'
' Croisement_ZANOX_BO Macro
'
'
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC3,BO!C[-18]:C[-11],1,FALSE)"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC3,BO!C[-19]:C[-12],2,FALSE)"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC3,BO!C[-20]:C[-13],3,FALSE)"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC3,BO!C[-21]:C[-14],4,FALSE)"
Selection.NumberFormat = "dd/mm/yy;#"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC3,BO!C[-22]:C[-15],5,FALSE)"
Selection.NumberFormat = "dd/mm/yy;#"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC3,BO!C[-23]:C[-16],6,FALSE)"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC3,BO!C[-24]:C[-17],7,FALSE)"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC3,BO!C[-25]:C[-18],8,FALSE)"
Selection.NumberFormat = "# ##0,00 €"
End Sub
You should avoid the use of .Select/ActiveCell etc as #Makah suggested.
INTERESTING READ
If the formula that you want to use is say =VLOOKUP($C1,BO!D:XFA,N,FALSE) where n is the column number in the formula (based on your above code) and you want to put that from say D1 then use a simple loop like this
Sub Sample()
Dim ws As Worksheet
Dim n As Long, col As Long
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
col = 4 '<~~ For COl D
With ws
For n = 1 To 8
.Cells(1, col).Formula = "=VLOOKUP($C1,BO!D:XFA," & n & ",FALSE)"
col = col + 1
Next n
End With
End Sub