'Object Required' Error VBA trying to save details - excel

Can someone please help me?
Basically, I have three textboxes in my Excel spreadsheet, and I have a button that I want to save the details into a table on another sheet.
Each row as an 'x' in it, so that the program can know where to save the next data.
When I click the save button, an error comes up saying 'Object required.'
I'm god awful at VBA, seriously I have no clue, so maybe try to keep the answer in stupid-man terms so I don't mess up? I'm probably doing something minor wrong but I'm not sure. Here's my code:
Public Function findrow(texttofind As String) As Integer
Dim i As Integer
Dim firstTime As Integer
Dim bNotFound As Boolean
i = 5
bNotFound = True
Do While bNotFound
If Cells(i, 1).Value = texttofind Then
firstTime = i
bNotFound = False
End If
i = i + 1
Loop
findrow = firstTime
End Function
Sub SaveCustomer()
Dim rowno As Integer
Worksheets("Customers").Select
rowno = findrow("x")
Range("C" & rowno).Value = Invoice.TextBox21.Text
Range("D" & rowno).Value = Invoice.TextBox22.Text
Range("E" & rowno).Value = Invoice.TextBox23.Text
Worksheets("Invoice").Select
End Sub
I'm really bad at VBA, and my teacher isn't great, I just need to fix this one thing so I can pass my assignment. I just want to save the information from 3 textboxes into a table in another sheet. Thanks for any help received!
I'm sorry if this is a duplicate, but because I'm so bad at VBA, answers to other questions don't really help me at all.

Replace
Range("C" & rowno).Value = Invoice.TextBox21.Text
Range("D" & rowno).Value = Invoice.TextBox22.Text
Range("E" & rowno).Value = Invoice.TextBox23.Text
with
Range("C" & rowno).Value = Worksheets("Invoice").TextBox21.Text
Range("D" & rowno).Value = Worksheets("Invoice").TextBox22.Text
Range("E" & rowno).Value = Worksheets("Invoice").TextBox23.Text

Related

How to Split and Copy its values

I am a VBA beginner, and am stunned with splitting and copying.
I have a workbook (book1) which contains some student's name in column A and Pay school fee Date in Column B. Some names have more than one date, while some have only one:
A...............B.
Kaka.........10/07/2018
Thy............12/05/2018, 15/08/2018, 15/08/2018
Then I want to search their name in another workbook and copy its entire row to new workbook. All help is welcome; thanks in advance.
You can perform operation this way although You had not show Your code but I have written my own code as my understand about code.
Sub transferData()
'Declare an array
Dim LArray() As String
'Start For loop
For i = 1 To Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Range("A" & i) = Sheets("Sheet1").Range("A" & i).Value
'Check Condition for multiple date exits or not
If (UBound(Split(Sheets("Sheet1").Range("B" & i).Value, ",")) > 0) Then
LArray = Split(Sheets("Sheet1").Range("B" & i).Value, ",")
'Split Date and store inside Array
For j = 0 To UBound(LArray)
Sheets("Sheet2").Cells(i, 2 + j) = LArray(j)
Next j
Else
Sheets("Sheet2").Range("B" & i) = Sheets("Sheet1").Range("B" & i).Value
End If
Next i
End Sub
If You find it helpful then pleasure for me and vote my answer or in case any doubt feel free to ask me.
thanks

VBA Number formatting not working

I have a problem with number formatting in my code.
If I stop the macro at this subroutine and run it step by step (F8) it works fine, no issues. However, if it's run as a whole with the rest of the macro (won't post the whole thing as it's quite huge) it just doesn't apply the number formatting and I can't seem to figure out why.
Any help would be appreciated, thanks!
Code:
Private Sub VolumeIncluded(TWs As Worksheet, MWs2 As Worksheet, MLngth As Double, MFinal As String, TLnght As Double, TFinal As String)
Dim TFtLnght As Double
On Error GoTo NextSheet
TLnght = TWs.Range("$A$" & Rows.Count).End(xlUp).Offset(2).Row
TFinal = "$A$" & TLnght
TWs.Range(TFinal) = "Volume Included"
With TWs.Range(TFinal)
.Interior.Color = 12611584
.Font.ThemeColor = xlThemeColorDark1
.Font.Bold = True
End With
MLngth = MWs2.Range("$H$" & Rows.Count).End(xlUp).Row
MFinal = "$A$16:$H" & MLngth
MWs2.Range(MFinal).Copy
TLnght = TWs.Range("$A$" & Rows.Count).End(xlUp).Offset(1).Row
TFinal = "$A$" & TLnght
TFormat = "$H$" & TLnght
TWs.Range(TFinal).PasteSpecial xlPasteAllUsingSourceTheme
Application.CutCopyMode = False
'----------This piece doesn't work-----------
TFtLnght = TWs.Range("$H$" & Rows.Count).End(xlUp).Row
TFinal = "$H$" & TLnght & ":" & "$H$" & TFtLnght
Range(TFinal).NumberFormat = "#,##0"
NextSheet:
End Sub
TWs.Range(TFinal).NumberFormat = "#,##0"
When you call Range() it's always safe to reference from a worksheet object that you know, since the active worksheet is used by default.

Excel, VBA, .ClearContents with referenced range, error 1004

I'm trying to use .clearcontents on range that is referenced by with some .offset, and I'm having trouble
I know that this works
Sub clear1_1()
Workbooks("xyz").Sheets("abc").range("A2:A3").ClearContents
End Sub
but if I try this it does not
Sub clear2()
Dim region As range
Set region = range("S509:AD618")
Workbooks("xyz").Worksheets("abc").range(region).ClearContents
end sub
I do understand from other postings, that it has something to do with object defyining, but I have no idea where I do mistake, what I need to write.
Final macro is run from one workbook, and is supposed to .clearcontents in other not activated workbook.
My code looks like this
sub Macro()
..... ton of code
Dim filename as string
dim sheetname as string
dim address3, address4 as string
filename = "xyz"
sheetname = "abc" ' both variables that are loaded in other part
address3 and address4 loaded in other part
'here is where i get the error
sheets(sheetname).Range(Range(address3).Offset(0, 12).Address & ":" & Range(address4).Offset(-1, 23)).ClearContents
end sub
I can probably bypass it with .value=""
But I'm looking to learn. Thank you for any response in advance.
EDIT 1
Hi Scott, doesn't make it. Posting bigger part of my code
If mapanchorsuccess = True And map1success = True And map2success = True Then
If Workbooks(Filename).Sheets(startws).Range(address1).Offset(10, 13).HasFormula = True Then
With Workbooks(wbm).Sheets("Report") 'report
.Range("A" & reportrow).Value = runnumber
.Range("B" & reportrow).Value = Filename
.Range("C" & reportrow).Value = Workbooks(Filename).Sheets(startws).Name
.Hyperlinks.Add anchor:=.Range("D" & reportrow), Address:=FilePath & Filename, SubAddress:=Workbooks(Filename).Sheets(startws).Name & "!A1"
.Range("E" & reportrow).Value = "Error"
.Range("F" & reportrow).Value = "rolling probably done already in this sheet"
reportrow = reportrow + 1
End With
Else
With Workbooks(Filename).Sheets(startws)
.Range(Range(address1).Offset(0, 12).Address & ":" & Range(address2).Offset(0, 14).Address).Copy _
Range(Range(address1).Address & ":" & Range(address2).Offset(0, 2).Address)
Application.CutCopyMode = False
.Range(Range(address1).Offset(0, 16).Address & ":" & Range(address2).Offset(0, 16).Address).Copy _
Range(Range(address1).Offset(0, 3).Address & ":" & Range(address2).Offset(0, 23).Address)
Application.CutCopyMode = False
With Workbooks(wbm).Sheets("Report") 'report
.Range("A" & reportrow).Value = runnumber
.Range("B" & reportrow).Value = Filename
.Range("C" & reportrow).Value = Workbooks(Filename).Sheets(startws).Name
.Hyperlinks.Add anchor:=.Range("D" & reportrow), Address:=FilePath & Filename, SubAddress:=Workbooks(Filename).Sheets(startws).Name & "!A1"
.Range("E" & reportrow).Value = "Completed"
.Range("F" & reportrow).Value = "region1 rolled forward"
reportrow = reportrow + 1
End With
.Range(Range(address3).Offset(0, 12).Address & ":" & Range(address4).Offset(-1, 23).Address).Copy _
Range(Range(address3).Address & ":" & Range(address4).Offset(-1, 11).Address)
'///// here the error 1004 occurs
.Range(Range(address3).Offset(0, 12).Address & ":" & Range(address4).Offset(-1, 23).address).clearcontent
End With
End If
End If
The workbook and sheet need to be set with the variable.
Then when using it since it is a range itself just refer to it.
Sub clear2()
Dim region As range
Set region = Workbooks("xyz").Worksheets("abc")range("S509:AD618")
region.ClearContents
end sub
As to your next code; that is a different problem. The ranges inside the () need to allocated to the correct sheet parentage or it will use the active sheet.
The easiest is with a With block:
With sheets(sheetname)
.Range(.Range(address3).Offset(0, 12), .Range(address4).Offset(-1, 23)).ClearContents
End With
I had this same issue, but it turned out to be very simple. I had a row of cells merged together between columns E and F, so when I used this command I had to set the ClearContents from the top corner of my E column to the bottom row of my F column.
What did not work:
Range("E1:E10").Clear Contents
What did work:
Range("E1:F10").ClearContents
I can't believe such a simple thing left me so thwarted.

Macro Coding in Excel [duplicate]

This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed 6 years ago.
Hi I found this code on StackOverflow and I was wondering how I could modify it (see below code snipping...)
Sub TestMacro()
Dim dblRow As Double, dtDate As Date, strProject As String
Dim strFalut As String, strProb As String, strSol As String
dblRow = InputBox("Input on What Row?")
dtDate = InputBox("Date", , Date)
strProject = InputBox("Project #")
strFalut = InputBox("Fault")
strProb = InputBox("Problem")
strSol = InputBox("Solution")
Range("A" & dblRow).Value = dtDate
Range("B" & dblRow).Value = strProject
Range("C" & dblRow).Value = strFalut
Range("D" & dblRow).Value = strProb
Range("E" & dblRow).Value = strSol
End Sub
Its a good start to perform the function that I want it to perform...But I was wondering how I could modify it with a vLookUp to search the for the next empty row and begin entering the data there instead of having to manually define a row, which may lead to operator error as these rows become more populated...
You can get the first blank row like this:
Range("A" & Range("A" & Rows.count).end(xlup).row + 1).value = dtDate
Obviously change both instances of A for the column you are updating.
Alternatively update dblRow = InputBox("Input on What Row?") to this dblRow = range("A" & rows.count).end(xlup).row and leave the rest of the code as is.

Using the For loop on selected cells

This is Monique from Boston.
I have created a VBA Script that works good on one row. Now I want to make it happen on all selected rows.
E.g.
Sub AutoButton_click()
Dim a As Range, b As Range
Set a = Selection
For Each b In a.Rows
Dim SellPrice As Double, CostPrice As Double
CostPrice = Range("C" & a).Value
SellPrice = CostPrice + 20
Range("D" & a).Value = SellPrice
Next
End Sub
But I keep getting type mis match errors.
What am I doing wrong?
Please help me.
Thank you so much. x.
More directly
Sub AutoButton_click()
Dim rng1 As Range
For Each rng1 In Selection.Cells
Cells(rng1.Row, "D").Value = Cells(rng1.Row, "c") + 20
Next
End Sub
If speed is important do this with a variant array instead
Try changing this:
For Each b In a.Rows
'your code here
Next
to this:
For Each b In a
'your code here
Next
The reason you're getting a type mismatch is because a.Rows isn't proper syntax; the compiler doesn't know what you're referring to. Check out this article for more info on the Range.Rows property.
In addition to ARich's answer, change lines
CostPrice = Range("C" & a).Value
Range("D" & a).Value = SellPrice
to
CostPrice = Range("C" & b.Row).Value
Range("D" & b.Row).Value = SellPrice
remove this line Set a = Selection and add the below line
Set a = Selection.Cells
Hope it helps

Resources