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
Related
I am trying to transfer the data from sheet one to sheet two and combined the information on the second sheet. The code I have listed below works, but it seems very inefficient. I am trying to improve by VBA abilities and would love to here ways to shrink my code down, make it more efficient, and still achieve the same goal. Thanks for any help you can provide.
Sheet 1
Sheet 2
Sub batchorder()
Dim Pname As String
Dim Lplace As String
Dim numsld As Long
Dim rating As Integer
Dim lastrow As Long
Dim i As Long
Dim openc As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("A1").Select
For i = 1 To lastrow
If Cells(i, 1).Value <> "" Then
'Copy name to sheet 2
Cells(i, 1).Select
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("Sheet2").Select
Range("A1").Select
'Find the next open cell to paste to
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Sheets("Sheet1").Select
'Copy place to sheet 2
ActiveCell.Offset(1, 0).Select
Selection.Copy
Sheets("Sheet2").Select
Range("B1").Select
'Find the next open cell to paste to
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Sheets("Sheet1").Select
'Copy sold to sheet 2
ActiveCell.Offset(1, 0).Select
Selection.Copy
Sheets("Sheet2").Select
Range("C1").Select
'Find the next open cell to paste to
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Sheets("Sheet1").Select
'Copy rating to sheet 2
ActiveCell.Offset(1, 0).Select
Selection.Copy
Sheets("Sheet2").Select
Range("D1").Select
'Find the next open cell to paste to
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Sheets("Sheet1").Select
i = i + 3
Else
End If
Next i
End Sub
Sub batchorder()
Dim Row As Long
Dim i As Long
' These two lines speed up evrything ENORMOUSLY.
' But you need the lines at the end too
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Row = Sheet2.UsedRange.Rows.Count ' Row is nr of last row in sheet
While Application.CountA(Sheet2.Rows(Row)) = 0 And Row > 1
Row = Row - 1 ' skip empty rows at the end if present
Wend
For i = 1 To Sheet1.UsedRange.Rows.Count
If Sheet1.Cells(i, 1).Value <> "" Then
Sheet2.Cells(Row, 1).FormulaLocal = Sheet1.Cells(i, 2).FormulaLocal
Sheet2.Cells(Row, 2).FormulaLocal = Sheet1.Cells(i + 1, 2).FormulaLocal
Sheet2.Cells(Row, 3).FormulaLocal = Sheet1.Cells(i + 2, 2).FormulaLocal
Sheet2.Cells(Row, 4).FormulaLocal = Sheet1.Cells(i + 3, 2).FormulaLocal
i = i + 3
Row = Row + 1
End If
Next
' Restore Excel to human state.
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
You should basically never use the select statement, it gets everything really messy quickly. Here's a basic combiner of mine. Just added the If statement to check whether the cell and in this case row is empty.
This should work but more importantly try to understand what it does to learn. I gave it some comments.
Sub batchorder()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
' Just habits, but doing this here means that I won't have to write anything else than ws1 and ws2 in the future
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")
Dim lastrowWs1 As Long
Dim j As Long
' first row after ws2 headers
j = 2
' With statement to make the code nicer also ".something" now means ws1.something
With ws1
' Bob Ulmas method -- just a personal preference to find the last row.
lastrowWs1 = .Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
For i = 1 To lastrowWs1
' Check if the cell is not empty
If Not .Cells(i, 1) = vbNullString Then
'Basically range.value = other_range.value
ws2.Range(ws2.Cells(j, 1), ws2.Cells(j, 4)).Value = WorksheetFunction.Transpose(.Range(.Cells(i, 2), .Cells(i + 3, 2)).Value)
' step 3 forward as the amount of rows per record was 4
i = i + 3
' go to next row for worksheet 2
j = j + 1
End If
Next i
End With
End Sub
My code paste the same formula throughout all of the H2 column. I dont see anywhere in the code where it should do that.
Worksheets("sheet1").Activate
Range("F2").Activate
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value <> "" Then
Pickle = ActiveCell.Address
ActiveCell.Offset(0, 2).Select
ActiveCell.Value = "=IF(" + Pickle + " <TODAY(),""Send Reminder"",""Do not Send Reminder"") "
ActiveCell.Offset(0, -2).Select
End If
ActiveCell.Offset(1, 0).Select
Loop
No loop needed. Use .FormulaR1C1
Do not use Activate and Select, they slow down the code
Dim lastrow As Long
With Worksheets("sheet1")
lastrow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("H2:H" & lastrow).FormulaR1C1 = "=IF(RC[-2] <TODAY(),""Send Reminder"",""Do not Send Reminder"") "
End With
This puts the formula in all the cells at once and the RC[-2] properly refers to the same row in Column F
I have a macro which is part of a few, however this is the first to tidy up sheet before running
To determine the column and table to tidy I am trying to find the last empty value and create a column and table range to use as variable throughout my modules. However failing at an early bit whether I choose cells C4 or C5 or refer to them in R1C1 style it breaks as if cells were empty however they are not.
It breaks at
LRow = ws.Cells(Rows.Count, C5).End(xlUp).Row
Unsure how to get it to proceed.
Sub Tidy()
'
' Tidy Macro
'
'
Dim table_1 As Long
Dim table_2 As Long
Dim col_len, table_len As Range
Dim LRow As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
LRow = ws.Cells(Rows.Count, C5).End(xlUp).Row
Set col_len = ws.Range("C4:C" & Cells(LRow).Address(False, False))
Set table_len = ws.Range("A4:F" & Cells(LRow).Address(False, False))
table_2 = Worksheets("DumpSheet").Cells(Row.Count, R5C10).End(xlUp).Row
Range("A5").Select
ActiveCell.FormulaR1C1 = "=R1C1"
Range("A5").Select
Selection.AutoFill Destination:=Range("A5:A" & col_len)
Range(table_len).Select
Selection.Copy
Range("H5").Select
ActiveSheet.Paste
Range("B5").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=R[-4]C"
Range("B5").Select
ActiveCell.FormulaR1C1 = "=R1C2"
Range("B5").Select
Selection.AutoFill Destination:=Range("B5:B" & col_len)
Range("B5:B" & table_1).Select
Range("I5").Select
ActiveCell.FormulaR1C1 = "=R1C9"
Range("I5").Select
Selection.AutoFill Destination:=Range("I5:I29")
Range("I5:I" & table_2).Select
End Sub
C5 is being treated as a variable that hasn't been assigned. It is not a cell reference. You're looking for something more like:
LRow = ws.Cells(ws.Rows.Count, 3).End(xlUp).Row
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.
good afternoon,
I have a worksheet where I need a macro to copy the value from D1 and paste it to the next 5 cells (paste it to E1:I1), then if the next cell has data (J1) copy it and paste it to the next five cells etc. until the next cell is blank (the problem is that every time this spreadsheet has a different number of columns). I did try to do this with macro recorder but I have to set every time the cells that I want to copy the data from and the cells that I will paste them to. There must be an easier way than this, any help would be appreciated.
Range("D1").Select
Selection.Copy
Range("E1:I1").Select
ActiveSheet.Paste
Range("J1").Select
Application.CutCopyMode = False
Selection.Copy
Range("K1").Select
ActiveWindow.SmallScroll ToRight:=10
Range("K1:O1").Select
ActiveSheet.Paste
Range("P1").Select
Application.CutCopyMode = False
Selection.Copy
Range("Q1:U1").Select
ActiveSheet.Paste
Range("V1").Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll ToRight:=12
Range("W1").Select
ActiveSheet.Paste
Range("X1:AA1").Select
ActiveSheet.Paste
Range("AB1").Select
Application.CutCopyMode = False
Selection.Copy
Range("AC1:AG1").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll ToRight:=8
Range("AH1").Select
Application.CutCopyMode = False
Selection.Copy
Range("AI1:AM1").Select
ActiveSheet.Paste
Range("AN1").Select
Application.CutCopyMode = False
Selection.Copy
Range("AO1:AS1").Select
ActiveSheet.Paste
Consider the following to first find the last column in spreadsheet and iterate every 5 columns using Cells(r, c) reference for numbering:
Sub CopyNextFive()
LastColumn = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Column
For i = 4 To LastColumn Step 6
Cells(1, i).Copy
Range(Cells(1, i + 1), Cells(1, i + 5)).PasteSpecial xlPasteAll
Next i
Application.CutCopyMode = False
End Sub
I would do this by using RC notation and looping something like this:
dim myValue
dim c as integer
dim x as integer
c=4 'Start in column D
myValue = cells(1,c).value 'Row 1 of column D
while myValue <> ""
for x = 1 to 5
cells(1,c+x).value=myValue
next x
c=c+x+1 'To give us the 10th column: J
myValue = cells(1,c).value
wend
Sub mySub()
Dim src As Range: Set src = ActiveSheet.Range("D1")
Dim dest As Range: Set dest = ActiveSheet.Range("E1:I1")
Do Until Trim(src.Text) = vbNullString
src.Copy dest
Set src = src.Offset(, 6): Set dest = dest.Offset(, 6)
Loop
End Sub
You'd need to run some form of loop. There are several kinds: For ... Next, Do Until ..., etc. Have a read about them (http://www.excelfunctions.net/VBA-Loops.html) and you'll see they give you great versatility.
In your case, one of many solutions could be as follows:
' Adjust these values to suit
Const SHEET_NAME As String = "Sheet1" 'name of sheet
Const START_COLUMN As String = "D" 'column letter where routine starts
Const ROW_NUM As Long = 1 'row number of your data
Const COPY_SIZE As Integer = 5 'number of columns to copy the data
Dim rng As Range
' The looping routine
Set rng = ThisWorkbook.Worksheets(SHEET_NAME).Cells(ROW_NUM, START_COLUMN)
Do Until IsEmpty(rng)
rng.Offset(, 1).Resize(, COPY_SIZE) = rng.Value2
Set rng = rng.Offset(, COPY_SIZE + 1)
Loop