VBA get the last invoice number and save the new invoice - vba

I have an invoice template sheet in invoice workbook that i want to save some of its fields in an other workbook "Excel data only", sheet "invoices". When creating an invoice i want to get the last invoice number from the "Excel data only", "Invoices" sheet column A and then add 1 to it.
Note that the invoice number has the format ST00001.
After that i want to save the invoice number in the workbook excel data only with its details: C10,C11 and C12 which represent the total.
I don't know how to do it since i am new in vba this is what i tried to do but not working.
Dim v As Workbook
Set v = Workbooks("Excel data only")
Range("I12").Value = v.Sheets("INVOICES").Cells(v.Sheets("INVOICES").Rows.Count, "A").End(xlUp).Row.Value + 1

Sounds like
Dim v As Workbook, cellValue As String
Set v = Workbooks("Excel data only")
With v.Worksheets("INVOICES")
cellValue = .Range("A" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
End With
ActiveSheet.Range("I12").Value = "ST" & Format$((Right$(cellValue, 5) + 1), "00000")

Sub test_1()
Dim v As Workbook, cellValue As String
With Worksheets("sheet2")
cellValue = .Range("A" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
End With
ActiveSheet.Range("B13").Value = "" & Format$((Right$(cellValue, 2) + 1), "")
End Sub
these codes increment by going to last row number, with increment can you write these codes that can search if any number missing in between invoice it should show that also. for example invooice from 1 to 10 and 7 number is missing so instead of last row number increment it should give me missing number.
thnx

Related

Print Excel sheets with interchangeable cell value from list and sheet number by copy number

I am trying to optimize paperwork and I can't figure out how to compile two separate macros to work as I need.
On Excel workbook, I have two sheets:
Sheet1 contains order form (invoice, etc.).
Sheet2 contains list of product names and quantities.
I need to create a macro so it would take the "Product name" from list (eg. Sheet2!A1 to Sheet1!N10) and change number interchangeable from 1 to "Quantity" (eg. Sheet2!B1 to Sheet1!R10) and print all generated copies through all the list.
For example: Sheet2!A1 cell contains the product "ALFA" and the corresponding quantity is "3" (cell B1). Next row - A2 contains "BETA" and B2 contains 2 (as quantity)
So the printout will be 5 pages "ALFA - 1", "ALFA - 2", 'ALFA - 3", "BETA - 1", "BETA - 2", and then follow to the next value in the list.
I have found a code (somewhere on internet) to print number of copies with interchangeable number using this code below:
Sub Technologiniu_korteliu_kiekis()
Dim CopiesCount As Long
Dim copynumber As Long
CopiesCount = Application.InputBox("How many copies do you want?", Type:=1)
'Now the program wants you to input how many pages you like to print.
'You can input 100 here.
For copynumber = 1 To CopiesCount
With ActiveSheet
.Range("L7").Value = copynumber 'I assume your invoice number is in cell E1.
.PrintOut 'Print the sheet
End With
Next copynumber
End Sub
I have found a solution. My macro was creating dublicates. though it wasn't what i need.
Sub Macro()
Dim i As Long, LastRow As Long
LastRow = Worksheets("Sheet2").Range("C65536").End(xlUp).Row
For i = 8 To LastRow
Worksheets("Sheet1").Range("G6").Value = Worksheets("Sheet2").Range("C" & i).Value
Worksheets("Sheet1").Range("J7").Value = Worksheets("Sheet2").Range("D" & i).Value
Dim CopiesCount As Long
Dim copynumber As Long
CopiesCount = Worksheets("Sheet2").Range("E" & i).Value
If Not Worksheets("Sheet2").Range("B" & i).Value = 1 Then
For copynumber = 1 To CopiesCount
With Worksheets("Sheet1")
.Range("L7").Value = copynumber
.PrintOut
End With
Next copynumber
End If
Next i
End Sub

Copy each value in a row to another workbook where a value matches?

I have two workbooks.
Workbook A
Supplier Name Date
Supplier A Jun-17
Supplier B Jun-17
Supplier C May-17
Workbook B
Supplier Name
Supplier A
Supplier B
I am trying to copy all supplier names where they match the date Jun-17.
Jun-17 is written on a sheet called (assets) in cell B1.
I am getting an error
Object doesn't support this property or method.
On this line:
If ThisWorkbook.Worksheets(1).Format(Range("O" & j).Value, "mmm") = ThisWorkbook.Worksheets("assets").Range("b1").Value Then
My code:
Sub Monthly()
On Error Resume Next
UserForm1.Show
If Format(Range("O211").Value, "mmm") = Worksheets("assets").Range("b1").Value Then
MsgBox "Yes"
End If
Dim WB As Workbook
On Error Resume Next
Set WB = Workbooks("PAI Monthly Audits.xlsx")
On Error GoTo 0
If WB Is Nothing Then
Set WB = Workbooks.Open("G:\QUALITY ASSURANCE\03_AUDITS\PAI\templates\PAI Monthly Audits.xlsx", UpdateLinks:=False)
End If
With ThisWorkbook.ActiveSheet
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
j = 6
For i = 2 To LastRow
If ThisWorkbook.Worksheets(1).Format(Range("O" & j).Value, "mmm") = ThisWorkbook.Worksheets("assets").Range("b1").Value Then
.Range("B" & i).Value = ThisWorkbook.Worksheets(1).Range("B" & j).Value
j = j + 1
End If
Next i
End With
End Sub
Please can someone show me where I'm going wrong?
Try it like this...
If Format(ThisWorkbook.Worksheets(1).Range("O" & j).Value, "mmm") = ThisWorkbook.Worksheets("assets").Range("b1").Value Then
The runtime error can be corrected with the suggestions already made (the ThisWorkbook.Worksheets(1) should used to identify the parent of Range("O" & j), not of Format) but that doesn't mean your code will work. As a true date, 17-Jun-2017 has a raw value of 42,903. Format(42903, "mmm") returns "Jun" as a text string. The text Jun does not equal 42903.
If both Assets!B1 and the dates in 'Workbook A' column O are true dates then you only have to compare their raw values 'to copy all supplier names where they match the date Jun-17'.
If ThisWorkbook.Worksheets(1).Range("O" & j).Value2 = ThisWorkbook.Worksheets("assets").Range("b1").Value2 Then
Beyond that, using AutoFilter to select all of the rows with 17-Jun-2017 dates would be more efficient. See Using cell values as a date range autofilter. Even using an INDEX/MATCH function pair to the closed workbook might be preferable.

Copy rows from Target sheet to oter sheets based on cell values

I am having some difficulty with (vba lookup) issue.
I Have a sheet (sheet3) which has multiple rows of data of different invoices (each row of data includes the invoice number it relates to)Data sheet
I have copied the unique invoice numbers into separate sheets, each invoice has its own sheet and the invoice number is in cell B1.invoice sheet
What I want to do is to copy all rows from the data sheet to the sheet with the matching invoice number.
all I have for my current code is this which My separate invoice pages link of rather than using Vba to create them as there will be various other formatting and Formulrs on the page so im pretty much starting from scratch on my issue!
Private Sub CommandButton1_Click()
Dim s1 As Worksheet, s2 As Worksheet
Set s1 = Sheets("sheet3")
Set s2 = Sheets("Bill Date")
s1.Range("F:G").Copy s2.Range("A:B")
s2.Range("A:B").RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
Your help will be appreciated
Thanks
In your VBA Macro, do this within a for loop:
Sub copyData()
Dim invNo As String
Dim lastRow As Integer
Dim sourceSht As Worksheet
Dim targSht As Worksheet
Set sourceSht = Worksheets("Sheet3")
'evaluates every data item from row 2 to last populated row
For Row = 2 To sourceSht.Cells(sourceSht.Rows.Count, 1).End(xlUp).Row
invNo = sourceSht.Range("F" & Row).Value
'if invNo blank, skip
If invNo <> "" Then
'try to find the sheet, make if does not exist
invNo = invNo & "_INV"
On Error Resume Next
Set targSht = Worksheets(invNo)
If targSht Is Nothing Then
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = invNo
Set targSht = Worksheets(invNo)
'SetHeader
End If
'find first empty row in targSht
lastRow = targSht.Cells(targSht.Rows.Count, 1).End(xlUp).Row + 1
'copy row of data
sourceSht.Range("A" & Row & ":L" & Row).Copy
targSht.Range("A" & lastRow & ":L" & lastRow).Select
targSht.Paste
'must do to make more sheets
Set targSht = Nothing
End If
Next
End Sub
I changed some of your specifications in favor of a simpler approach. I assumed the twelve columns you showed me are all you have. I added "_INV" to the end of the invoice sheets because purely numeric sheet names can cause errors. I am also pasting the row of data into the new sheet verbatim. If you keep your current header, you will need to change the order. You may consider changing your targSht header to make it easier. SetHeader is a placeholder for a block of code that sets up the header row in targSht however you want. Please mark correct if this solves your issue.
Demo (without invoice header):

Create a VBA macro that Find and Copy?

I need a little bit help with a macro of Excel.
I need to create a macro that automatically find users and copy the values that i have in an other Sheet:
I have one sheet with values that contains the Users and their Kills and Deaths, I create 3 sheets more (3 different groups of users), and I need that the macro copy values automatically finding the users and copying values.
Images to describe it better:
----(Copy this values on)----->
You don't need a macro for this, using the worksheetfunction VLOOKUP is sufficient.
As an example, if you have your headers in row 1 and users in column A, what you'd put into cell B2 (the number of kills for the first user) would be =VLOOKUP($A2;Values!$A$2:$C$9;2;FALSE) and C2 would be =VLOOKUP($A2;Values!$A$2:$C$9;3;FALSE).
The arguments for the function (which you can also find in the linked document) is:
First, the value you're looking for, in your case whatever is in A2
Next the array of values which you want to return a result from - vlookup will only look through the first column, but since you want to return results from the other columns we include columns A:C in the formula.
What column in the range you search to return the result from for kills it is column 2, for deaths column 3.
Finally whether you want to have an exact match (false) or if an approximate one is ok (true).
If I understand what you're after, you should be able to do this with VLOOKUPs
(No VBA necessary)
The following source code solve your problem.
Option Explicit
Dim MyResultWorkbook As Workbook
Dim ValuesWorksheet As Worksheet
Dim SniperWorksheet As Worksheet
Dim ARsWorksheet As Worksheet
Sub CopyResult()
Set MyResultWorkbook = ActiveWorkbook
Set ValuesWorksheet = MyResultWorkbook.Sheets("Values")
Set SniperWorksheet = MyResultWorkbook.Sheets("Sniper")
Set ARsWorksheet = MyResultWorkbook.Sheets("Ars")
Dim SniperLastRow As Long
Dim ARLastRow As Long
Dim RowPointer As Long
Dim ValuePointer As Long
ValuePointer = 2
'Update the Sniper worksheets
SniperLastRow = SniperWorksheet.Cells(SniperWorksheet.Rows.Count, "A").End(xlUp).Row
For RowPointer = 2 To SniperLastRow
Do While (SniperWorksheet.Range("A" & RowPointer).Value <> ValuesWorksheet.Range("A" & ValuePointer).Value)
ValuePointer = ValuePointer + 1
Loop
SniperWorksheet.Range("A" & RowPointer).Offset(0, 1).Value = ValuesWorksheet.Range("A" & ValuePointer).Offset(0, 1).Value 'copy kill
SniperWorksheet.Range("A" & RowPointer).Offset(0, 2).Value = ValuesWorksheet.Range("A" & ValuePointer).Offset(0, 2).Value 'copy death
ValuePointer = 2
Next
'Update the Ars worksheets
ARLastRow = ARsWorksheet.Cells(ARsWorksheet.Rows.Count, "A").End(xlUp).Row
For RowPointer = 2 To ARLastRow
Do While (ARsWorksheet.Range("A" & RowPointer).Value <> ValuesWorksheet.Range("A" & ValuePointer).Value)
ValuePointer = ValuePointer + 1
Loop
ARsWorksheet.Range("A" & RowPointer).Offset(0, 1).Value = ValuesWorksheet.Range("A" & ValuePointer).Offset(0, 1).Value 'copy kill
ARsWorksheet.Range("A" & RowPointer).Offset(0, 2).Value = ValuesWorksheet.Range("A" & ValuePointer).Offset(0, 2).Value 'copy death
ValuePointer = 2
Next
End Sub

Select sheet defined as date

I do have a workbook where multiple sheets are named based on date (in format MMDDD). This macro should loop trough all date sheet (like 01OCT, 02OCT, .... 30OCT) select range and copy it into new sheet.
Selecting cells, copying them and so is not really problem, and that is working perfectly. However I do have a problem defining sheet name. I would like user in the beginning define month and number of days in month and month using InputBox.
So if user select month = "FEB" and DaysMonth = 28, I would like macro to loop trough sheets named 01FEB, 02FEB, 03FEB, .... 28FEB.
Sub Merge_whole_month()
Application.ScreenUpdating = False
Dim month As String
month = InputBox(Prompt:="Please enter month in format MMM", _
Title:="Month")
Dim DaysMonth As Long
DaysMonth = InputBox(Prompt:="Please enter number of days in month", _
Title:="Days")
'create new sheet for results
Sheets.Add.Name = "Merge"
'loop
For i = 1 To DaysMonth
i = Format(i, "##")
Sheets(i & month).Activate 'here is the problem
'select cell G3, then all "non-empty" cells to the right and down and COPY
Range(Range("G3", Range("G3").End(xlToRight)), Range("G3", Range("G3").End(xlToRight)).End(xlDown)).Select
Selection.Copy
Sheets("Merge").Activate 'activate sheet where cells needs to be copied
'find last cell in 2nd row in sheet
lastCol = Cells(2, Columns.Count).End(xlToLeft).Column
lastCol = lastCol + 1
Cells(1, lastCol) = i & month 'log date and month in cell above
Cells(2, lastCol).Select
ActiveSheet.Paste 'Paste
Next i
Application.ScreenUpdating = True
End Sub
Many thanks in advance for any help!
The problem lies in the facto that i = Format(i, "##") does not make i less than 10 appear as 01 etc. To fix this i would do this:
Dim sDate As String
sDate = CStr(i)
If Len(sDate) < 2 Then
sDate = "0" & sDate
End If
Place that code within your for-loop before Sheets(i & month).Activate and remove i = Format(i, "##").
EDIT:
It also seems that for me using Format(i, "0#") gives the string you were looking for. However you will still need to assign this to a String variable or change Sheets(i & month).Activate to Sheets(Format(i, "0#") & month).Activate.
Here is the documentation on the Format() function. I suggest reading it.