I am new to VBA and coding in general. I am currently trying to transfer date from a multitab over to a workbook and keep getting the run time error 424: object required. Please help
Dim sheetname As String
Private Sub CommandButton2_Click()
Dim LastRow As Long
Dim sheetname As String
Select Case MultiPage1.Value
Case 0`sheetname = "Hannah"
Sheets(sheetname).Select
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A1").Select`enter code here`
Selection.End(xlDown).Offset(1, 0).Select
ActiveCell.Value = Date
ActiveCell.Offset(0, 1).Value = Me.RequestorHR.Value
ActiveCell.Offset(0, 2).Value = Me.CaseHR.Value
ActiveCell.Offset(0, 3).Value = Me.TypeHR.Value
ActiveCell.Offset(0, 4).Value = Me.UrgencyHR.Value
ActiveCell.Offset(0, 5).Value = Me.ReasonsHR.Value
ActiveCell.Offset(0, 6).Value = Me.DeadlineHR.Value
Clear_Click
Case 1
sheetname = "John"
Sheets(sheetname).Select
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A1").Select
Selection.End(xlDown).Offset(1, 0).Select
ActiveCell.Value = Date
ActiveCell.Offset(0, 1).Value = Me.RequestorJM.Value
ActiveCell.Offset(0, 2).Value = Me.CaseJM.Value
ActiveCell.Offset(0, 3).Value = Me.TypeJM.Value
ActiveCell.Offset(0, 4).Value = Me.UrgencyJM.Value
ActiveCell.Offset(0, 5).Value = Me.ReasonsJM.Value
ActiveCell.Offset(0, 6).Value = Me.DeadlineJM.Value
Clear_Click
I made some suggestions below so you have less repeating code. I added an array so you do not have to keep retyping all your textbox(Assuming) values each time. I hope that helps. I had to guess so if it needs to be tweaked let me know bud! It may run an error cause i was not sure of the active cell your referring too. If you let me know, i can fix it.
Private Sub CommandButton2_Click()
Dim LastRow As Long
Dim sheetname As String
Dim strList() As Variant
Dim i As Integer
strList = Array(, Me.RequestorHR.Value, Me.CaseHR.Value, Me.TypeHR.Value, _
Me.UrgencyHR.Value, Me.ReasonsHR.Value, Me.DeadlineHR.Value)
Select Case MultiPage1.Value
Case 0
sheetname = "Hannah"
Sheets(sheetname).Select
LastRow = Sheets(sheetname).Range("A" & Rows.Count).End(xlUp).Row
Sheets(sheetname).Range("A1").End(xlDown).Offset(1, 0).Value = Date
For i = 1 To 6
With Sheets(sheetname)
.ActiveCell.Offset(0, i).Value = strList(i)
End With
Next i
Clear_Click
Case 1
sheetname = "John"
Sheets(sheetname).Select
LastRow = Sheets(sheetname).Range("A" & Rows.Count).End(xlUp).Row
Sheets(sheetname).Range("A1").End(xlDown).Offset(1, 0).Value = Date
For i = 1 To 6
With Sheets(sheetname)
.ActiveCell.Offset(0, i).Value = strList(i)
End With
Next i
Clear_Click
In Userform Code:
' StartUpPosition > Manual
Private Sub UserForm_Initialize()
With Me
.Top = Int(((Application.Height / 2) + Application.Top) - (.Height / 2))
.Left = Int(((Application.Width / 2) + Application.Left) - (.Width / 2))
End With
End sub
Code in worksheet or Module:
Sub Open_Userform
UserForm1.Show False
End sub
Have a great day!
Related
I am totally new in macro's so I would like to know where did I make mistakes in my code.
I just want a simple IF - AND based on 2 different columns and the result should be in the third column
My code is:
Sub FTL_Customer3()
Lastrow = Sheets("Raw data").Range("D" & Rows.Count).End(xlUp).Row
For i = 1 To Lastrow
If Sheets("Raw data").Cells(i, 5) = "Postponed to next month" And Sheets("Raw
data").Cells(i, 13) = "ATP Ship date after EOM" Then
Cells(i, 7).Value = "OK"
End If
Next I
End Sub
Ensure the cells you will be testing do not contain an error:
Sub FTL_Customer3()
With Worksheets("Raw data")
Dim LastRow As Long
LastRow = .Range("D" & .Rows.Count).End(xlUp).Row
Dim i As Long
For i = 1 To LastRow
If Not IsError(.Cells(i, 5)) And Not IsError(.Cells(i, 13)) Then
If .Cells(i, 5) = "Postponed to next month" And _
.Cells(i, 13) = "ATP Ship date after EOM" Then
.Cells(i, 7).Value = "OK"
End If
End If
Next i
End With
End Sub
I am trying to convert a sub procedure to a function. This sub generate number based on some conditions. It's working as expected but when I try to convert it as a function, it only generate "0".
Can anyone please help? It will be great appreciated!
Here is my sub named "GenerateNumber"
Public Sub GenerateNumber()
Dim evenRange As Range
Dim oddRange As Range
Dim i As Integer
Set evenRange = Range("A2:Z2,A4:Z4,A6:Z6,A8:Z8,A10:Z10,A12:Z12,A14:Z14,A16:Z16,A18:Z18 ")
Set oddRange = Range("A1:Z1,A3:Z3,A5:Z5,A7:Z7,A9:Z9,A11:Z11,A13:Z13,A15:Z15,A17:Z17,A19:Z19")
If Not Intersect(Activecell, evenRange) Is Nothing Then
If Activecell.Interior.Color = Activecell.Offset(0, -1).Interior.Color Then
Activecell.Value = Activecell.Offset(0, -1).Value + 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, i).Value = Activecell.Value + i
Next i
Else
Activecell.Value = 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, i).Value = Activecell.Value + i
Next i
End If
'MsgBox "Active Cell In Even Range!"
Else
' MsgBox "Active Cell In Odd Range!"
If Activecell.Interior.Color = Activecell.Offset(0, 1).Interior.Color Then
Activecell.Value = Activecell.Offset(0, 1).Value + 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, -i).Value = Activecell.Value + i
Next i
Else
Activecell.Value = 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, -i).Value = Activecell.Value + i
Next i
End If
End If
End Sub
And my function code like this,
Public Function GenNumber() As Variant
Dim evenRange As Range
Dim oddRange As Range
Dim rng As Range
Dim i As Integer
Set evenRange = Range("A2:Z2,A4:Z4,A6:Z6,A8:Z8,A10:Z10,A12:Z12,A14:Z14,A16:Z16,A18:Z18 ")
Set oddRange = Range("A1:Z1,A3:Z3,A5:Z5,A7:Z7,A9:Z9,A11:Z11,A13:Z13,A15:Z15,A17:Z17,A19:Z19")
If Not Intersect(Activecell, evenRange) Is Nothing Then
If Activecell.Interior.Color = Activecell.Offset(0, -1).Interior.Color Then
Activecell.Value = Activecell.Offset(0, -1).Value + 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, i).Value = Activecell.Value + i
Next i
Else
Activecell.Value = 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, i).Value = Activecell.Value + i
Next i
End If
'MsgBox "Active Cell In Even Range!"
Else
' MsgBox "Active Cell In Odd Range!"
If Activecell.Interior.Color = Activecell.Offset(0, 1).Interior.Color Then
Activecell.Value = Activecell.Offset(0, 1).Value + 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, -i).Value = Activecell.Value + i
Next i
Else
Activecell.Value = 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, -i).Value = Activecell.Value + i
Next i
End If
End If
GenNumber = Activecell.Value
End Function
Can anyone say where is the wrong and how to solve it.
Thank you.
I would like to also use text values to populate textbox one to four right now only number values will fetch data in the textbox, I know I am overlooking a pretty simple thing but can't seem to find out what?
Private Sub ComboBox1_Change()
Dim i As Long, LastRow As Long, ws As Worksheet
Set ws = Sheets("Sheet1")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Val(Me.ComboBox1.Value) = ws.Cells(i, "A") Then
MsgBox Me.ComboBox1.Value
Me.TextBox1 = ws.Cells(i, "B").Value
Me.TextBox2 = ws.Cells(i, "C").Value
Me.TextBox3 = ws.Cells(i, "D").Value
Me.TextBox4 = ws.Cells(i, "E").Value
End If
Next i
End Sub
I fixed it myself with the possibility to reverse it.
FYI: sheet is renamed to "Control"
Private Sub UserForm_Initialize()
With Sheets("Control")
Me.ComboBox1.List = .Range("a2", .Range("a" & Rows.Count).End(xlUp)).Value
End With
End Sub
Private Sub ComboBox1_Change()
Dim i As Long
For i = 1 To 4
Me("textbox" & i).Value = ""
Next
If Me.ComboBox1.ListIndex = -1 Then Exit Sub
With Sheets("control")
For i = 1 To 4
Me("textbox" & i).Value = .Cells(Me.ComboBox1.ListIndex + 2, i + 1).Value
Next
End With
End Sub
Private Sub CommandButton2_Click()
If Me.ComboBox1.ListIndex = -1 Then Exit Sub
Sheets("control").Cells(Me.ComboBox1.ListIndex + 2, "b").Resize(, 4).Value = _
Array(Me.TextBox1.Value, Me.TextBox2.Value, Me.TextBox3.Value, Me.TextBox4.Value)
End Sub
I tried to make macro for my daily job, but i cannot use IF as formula due to so many item in my excel file, so solution is to convert formula to VBA code.
I need help to convert if formula to VBA code in excel as below:
=IF(J2<>J1,AD2-X2,AE1-X2).
Here is an answer to your question. However, it is limited to only work with OP information. Also, if the calculations are taking too long then, you should try setting your calculation to Manual (Formulas->Calculation Options->Manual).
Option Explicit
Public Sub RunIF()
Dim vntOut As Variant
Dim rngSame As Range
With ActiveSheet
Set rngSave = .Range("X2")
If (LCase(Trim(.Range("J2").Value)) <> LCase(Trim(.Range("J1").Value))) Then
vntOut = .Range("AD2").Value - rngSave.Value
Else
vntOut = .Range("AE1").Value - rngSave.Value
End If
.Range("AE2").value = vntOut
Set rngSave = Nothing
End With
End Sub
And here is your code converted to use Column J:
Private Sub CommandButton12_Click()
Dim x As Long
Dim LastRow As Long
Dim i as long
With Sheets("Shipping Schedule")
LastRow = .Cells(.Rows.Count, "J").End(xlUp).Row
For i = 2 to LastRow
set r = .Range("J" & I)
'For Each r In .Range("J2:J" & LastRow)
If LCase(Trim(r.Value)) <> LCase(Trim(r.Offset(-1, 0).Value)) Then
'ae2 = "AD2" - "x2"
r.Offset(0, 21).Value = r.Offset(0, 20).Value - r.Offset(0, 14).Value
Else
'ae2 = "AE1" - "x2"
r.Offset(0, 21).Value = r.Offset(-1, 21).Value - r.Offset(0, 14).Value
End If
set r = nothing
Next i
End With
End Sub
However, you should increment with I instead of for each as the cells are dependent on the previous row and excel may not loop through the range like you would prefer.
Excel Formula to VBA: Fill Column
Sub FillColumn()
Const cCol As Variant = "J" ' Last-Row-Column Letter/Number
Const cCol1 As Variant = "AD"
Const cCol2 As Variant = "X"
Const cCol3 As Variant = "AE"
Const cFirstR As Long = 1 ' First Row
Dim rng As Range ' Last Used Cell in Last-Row-Column
Dim i As Long ' Row Counter
Set rng = Columns(cCol).Find("*", , xlFormulas, , xlByColumns, xlPrevious)
If rng Is Nothing Then Exit Sub
For i = cFirstR To rng.Row - 1
If Cells(i + 1, cCol) <> Cells(i, cCol) Then
Cells(i + 1, cCol3) = Cells(i + 1, cCol1) - Cells(i + 1, cCol2)
Else
Cells(i + 1, cCol3) = Cells(i, cCol3) - Cells(i + 1, cCol2)
End If
Next
End Sub
Private Sub CommandButton12_Click()
Dim x As Long
Dim LastRow As Long
Sheets("Shipping Schedule").Select
With Sheets("Shipping Schedule")
LastRow = .Cells(.Rows.Count, "N").End(xlUp).Row
For Each r In .Range("N2:N" & LastRow)
If r.Value <> "" Then
r.Offset(0, 19).Value = ………………………………….
End if
Next r
End With
End Sub
Struggling a bit with this code, I haven't ever had to reference one column and copy and paste to another tab in VBA so here goes..
I have an excel document with a table on it similar to below:
I need my code to look in column A find the first name, in this case, Nicola. I then want it to look at column B and check to see if she has the word "Internet" appear in any of the records stored against her, as she does the code will ignore her and move down to the next name on the list, in this case, Graham. It will then look to column B and check if he has the word "Internet". As he doesn't, the code needs to copy the Information from column A & B in relation to this persons name and paste the information into another sheet in the workbook.
Sub Test3()
Dim x As String
Dim found As Boolean
Range("B2").Select
x = "Internet"
found = False
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value = x Then
found = True
Exit Do
End If
ActiveCell.Offset(1, 0).Select
Loop
If found = False Then
Sheets("Groupings").Activate
Sheets("Groupings").Range("A:B").Select
Selection.Copy
Sheets("Sheet1").Select
Sheets("Sheet1").Range("A:B").PasteSpecial
End If
End Sub
Any help would be greatly appreciated.
Thanks
Paula
Private Sub Test3()
Application.ScreenUpdating = False
Set sh1 = Sheets("Groupings") 'data sheet
Set sh2 = Sheets("Sheet1") 'paste sheet
myVar = sh1.Range("D1")
Lastrow = sh1.Range("B" & Rows.Count).End(xlUp).Row
For i = 2 To Lastrow '2 being the first row to test
If Len(sh1.Range("A" & i)) > 0 Then
Set myFind = Nothing
If WorksheetFunction.CountA(sh1.Range("A" & i, "A" & Lastrow)) > 1 Then
If Len(sh1.Range("A" & i + 1)) = 0 Then
nextrow = sh1.Range("A" & i).End(xlDown).Row - 1
Else
nextrow = nextrow + 1
End If
Set myFind = sh1.Range("B" & i, "B" & nextrow).Find(What:=myVar, LookIn:=xlFormulas, LookAt:=xlWhole)
Else
nextrow = Lastrow
Set myFind = sh1.Range("B" & i, "B" & nextrow).Find(What:=myVar, LookIn:=xlFormulas, LookAt:=xlWhole)
End If
If myFind Is Nothing Then
sh1.Range("A" & i, "B" & nextrow).Copy
sh2.Range("A" & sh2.Range("B" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
Application.CutCopyMode = False
End If
End If
Next
End Sub
I don't clearly see the structure of your data, but assuming the original data is in Worksheet Data, I think the following is going to do what you want (edited to search for two conditions).
Private Sub Test3()
Dim lLastRow as Long
Dim a as Integer
Dim i as Integer
Dim sText1 As String
Dim sText2 As String
sText1 = Worksheets("Data").Cells(1, 5).Value 'search text #1, typed in E1
sText2 = Worksheets("Data").Cells(2, 5).Value 'search text #2, typed in E2
lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
a = 1
For i = 2 To lLastRow
If (Worksheets("Data").Cells(i, 1).Value <> "") Then
If (Worksheets("Data").Cells(i, 2).Value <> sText1 And Worksheets("Data").Cells(i + 1, 2).Value <> sText1 And Worksheets("Data").Cells(i, 2).Value <> sText2 And Worksheets("Data").Cells(i + 1, 2).Value <> sText2) Then
Worksheets("Groupings").Cells(a, 1).Value = Worksheets("Data").Cells(i, 1).Value
Worksheets("Groupings").Cells(a, 2).Value = Worksheets("Data").Cells(i, 2).Value
Worksheets("Groupings").Cells(a, 3).Value = Worksheets("Data").Cells(i + 1, 2).Value
a = a + 1
End If
End If
Next
End Sub